Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
DavQueueDispatcher
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dispatchCalendarPush
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 dispatchContactPush
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 enqueueJob
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Modules\Dav\Application\Service;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use PDO;
12
13/**
14 * DAV Queue Job Dispatcher.
15 *
16 * Enqueues asynchronous push jobs into the background task queue table.
17 *
18 * @package App\Modules\Dav\Application\Service
19 */
20final readonly class DavQueueDispatcher
21{
22    public const string JOB_CALENDAR_PUSH = 'dav_push_calendar';
23    public const string JOB_CONTACT_PUSH = 'dav_push_contact';
24
25    /**
26     * DavQueueDispatcher constructor.
27     *
28     * @param PDO $pdo PDO database connection instance.
29     * @param string $tablePrefix Optional database table prefix.
30     */
31    public function __construct(
32        private PDO $pdo,
33        private string $tablePrefix = 'a_'
34    ) {
35    }
36
37    /**
38     * Dispatches a calendar event push job to the queue.
39     *
40     * @param int $calendarId Record ID in c_mod_calendar_records.
41     * @param string $action Action type: create, update, or delete.
42     * @param int $userId ID of user performing the action.
43     * @param array<string, mixed> $snapshot Optional snapshot data for delete actions.
44     * @return int Created queue job identifier.
45     */
46    public function dispatchCalendarPush(
47        int $calendarId,
48        string $action,
49        int $userId = 1,
50        array $snapshot = []
51    ): int {
52        $label = sprintf('DAV Calendar Push [%s] #%d', strtoupper($action), $calendarId);
53        $payload = [
54            'entity_id' => $calendarId,
55            'action' => $action,
56            'module' => 'calendar',
57            'user_id' => $userId,
58            'snapshot' => $snapshot,
59        ];
60
61        return $this->enqueueJob(self::JOB_CALENDAR_PUSH, $label, $payload, $userId);
62    }
63
64    /**
65     * Dispatches a contact push job to the queue.
66     *
67     * @param int $contactId Record ID in c_mod_contacts_records.
68     * @param string $action Action type: create, update, or delete.
69     * @param int $userId ID of user performing the action.
70     * @param array<string, mixed> $snapshot Optional snapshot data for delete actions.
71     * @return int Created queue job identifier.
72     */
73    public function dispatchContactPush(
74        int $contactId,
75        string $action,
76        int $userId = 1,
77        array $snapshot = []
78    ): int {
79        $label = sprintf('DAV Contact Push [%s] #%d', strtoupper($action), $contactId);
80        $payload = [
81            'entity_id' => $contactId,
82            'action' => $action,
83            'module' => 'contacts',
84            'user_id' => $userId,
85            'snapshot' => $snapshot,
86        ];
87
88        return $this->enqueueJob(self::JOB_CONTACT_PUSH, $label, $payload, $userId);
89    }
90
91    /**
92     * Inserts a new job row into the queue table.
93     *
94     * @param string $jobType Identifier of the job type.
95     * @param string $label Human-readable title of the job.
96     * @param array<string, mixed> $payload Job payload array.
97     * @param int $userId User ID owner.
98     * @return int Generated job ID.
99     */
100    private function enqueueJob(string $jobType, string $label, array $payload, int $userId): int
101    {
102        $table = $this->tablePrefix . 'mod_queue_records';
103        $sql = sprintf(
104            'INSERT INTO `%s` (`job_type`, `label`, `status`, `payload`, `total_items`, `created_by`, `owner`) '
105            . 'VALUES (:job_type, :label, \'pending\', :payload, 1, :created_by, :owner)',
106            $table
107        );
108
109        $stmt = $this->pdo->prepare($sql);
110        $stmt->execute([
111            ':job_type' => $jobType,
112            ':label' => $label,
113            ':payload' => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
114            ':created_by' => $userId > 0 ? $userId : 1,
115            ':owner' => $userId > 0 ? $userId : 1,
116        ]);
117
118        return (int) $this->pdo->lastInsertId();
119    }
120}