Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractDavPushJobHandler
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
4
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
 executePush
n/a
0 / 0
n/a
0 / 0
0
 formatSuccessMessage
n/a
0 / 0
n/a
0 / 0
0
 handle
100.00% covered (success)
100.00%
11 / 11
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\Handler;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Modules\Automation\Queue\Application\Handler\JobHandlerInterface;
12use App\Modules\Automation\Queue\Domain\Model\QueueJob;
13use App\Modules\Automation\Queue\Domain\Repository\QueueRepositoryInterface;
14use App\Modules\Dav\Application\Service\DavAccountResolver;
15use App\Modules\Dav\Domain\Model\DavAccount;
16use DateTimeImmutable;
17
18/**
19 * Base abstract queue job handler for DAV synchronization push jobs.
20 *
21 * @package App\Modules\Dav\Application\Handler
22 */
23abstract readonly class AbstractDavPushJobHandler implements JobHandlerInterface
24{
25    /**
26     * AbstractDavPushJobHandler constructor.
27     *
28     * @param DavAccountResolver $accountResolver User DAV account resolver.
29     */
30    public function __construct(
31        protected DavAccountResolver $accountResolver
32    ) {
33    }
34
35    /**
36     * Executes the synchronized entity push to the remote DAV server.
37     *
38     * @param int                  $entityId Entity identifier.
39     * @param string               $action   Sync action ('create', 'update', 'delete').
40     * @param DavAccount           $account  Configured user DAV account.
41     * @param array<string, mixed> $snapshot Entity snapshot data.
42     */
43    abstract protected function executePush(
44        int $entityId,
45        string $action,
46        DavAccount $account,
47        array $snapshot
48    ): void;
49
50    /**
51     * Formats the queue job completion report string.
52     *
53     * @param int    $entityId Entity identifier.
54     * @param string $action   Executed action.
55     * @return string Report string.
56     */
57    abstract protected function formatSuccessMessage(int $entityId, string $action): string;
58
59    /**
60     * {@inheritdoc}
61     */
62    public function handle(QueueJob $job, QueueRepositoryInterface $queueRepository): string
63    {
64        $payload = $job->payload;
65        $userId = (int) ($payload['user_id'] ?? 1);
66        $account = $this->accountResolver->resolveByUserId($userId);
67
68        if ($account === null) {
69            return 'No DAV account configured for user. Skipped.';
70        }
71
72        $entityId = (int) ($payload['entity_id'] ?? 0);
73        $action = (string) ($payload['action'] ?? 'update');
74        $snapshot = is_array($payload['snapshot'] ?? null) ? $payload['snapshot'] : [];
75
76        $this->executePush($entityId, $action, $account, $snapshot);
77        $queueRepository->updateProgress($job->id, 1, 1, new DateTimeImmutable());
78
79        return $this->formatSuccessMessage($entityId, $action);
80    }
81}