Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
39.39% covered (danger)
39.39%
13 / 33
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeliverySyncTask
39.39% covered (danger)
39.39%
13 / 33
40.00% covered (danger)
40.00%
2 / 5
17.91
0.00% covered (danger)
0.00%
0 / 1
 __invoke
55.00% covered (warning)
55.00%
11 / 20
0.00% covered (danger)
0.00%
0 / 1
3.82
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 requeueTask
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getQueueDispatcher
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPersistDataService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2021-2022 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoDeliveryRdf\model\DataStore;
24
25use JsonSerializable;
26use oat\generis\model\OntologyAwareTrait;
27use oat\oatbox\extension\AbstractAction;
28use oat\oatbox\reporting\Report;
29use oat\oatbox\service\exception\InvalidServiceManagerException;
30use oat\tao\model\taskQueue\QueueDispatcher;
31use Throwable;
32
33class DeliverySyncTask extends AbstractAction implements JsonSerializable
34{
35    use OntologyAwareTrait;
36
37    /**
38     * @throws InvalidServiceManagerException
39     */
40    public function __invoke($params)
41    {
42        $report = new Report(Report::TYPE_SUCCESS);
43        $resourceSyncDTO = new ResourceSyncDTO(...array_values($params[0]));
44        $tryNumber = $params[1];
45
46        if ($tryNumber < $resourceSyncDTO->getMaxTries()) {
47            $tryNumber++;
48            try {
49                $this->getPersistDataService()->persist($resourceSyncDTO);
50                $report->setMessage(sprintf(
51                    'Success MetaData syncing for delivery: %s',
52                    $resourceSyncDTO->getResourceId()
53                ));
54            } catch (Throwable $exception) {
55                $this->logError(sprintf(
56                    'Failing MetaData syncing for delivery: %s with message: %s',
57                    $resourceSyncDTO->getResourceId(),
58                    $exception->getMessage()
59                ));
60
61                $report->setType(Report::TYPE_ERROR);
62                $report->setMessage($exception->getMessage());
63                $this->requeueTask($resourceSyncDTO, $tryNumber);
64            }
65        }
66
67        return $report;
68    }
69
70    public function jsonSerialize(): string
71    {
72        return self::class;
73    }
74
75    /**
76     * @throws InvalidServiceManagerException
77     */
78    private function requeueTask(ResourceSyncDTO $resourceSyncDTO, int $tryNumber): void
79    {
80        $queueDispatcher = $this->getQueueDispatcher();
81        $queueDispatcher->createTask(
82            $this,
83            [$resourceSyncDTO, $tryNumber],
84            __(
85                'DataStore sync retry number "%s" for delivery with id: "%s".',
86                $tryNumber,
87                $resourceSyncDTO->getResourceId()
88            )
89        );
90    }
91
92    /**
93     * @throws InvalidServiceManagerException
94     */
95    private function getQueueDispatcher(): QueueDispatcher
96    {
97        return $this->getServiceLocator()->get(QueueDispatcher::SERVICE_ID);
98    }
99
100    /**
101     * @throws InvalidServiceManagerException
102     */
103    private function getPersistDataService(): PersistDataService
104    {
105        return $this->getServiceLocator()->get(PersistDataService::class);
106    }
107}