Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.57% covered (warning)
67.57%
25 / 37
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeliveryMetadataListener
67.57% covered (warning)
67.57%
25 / 37
66.67% covered (warning)
66.67%
4 / 6
11.76
0.00% covered (danger)
0.00%
0 / 1
 whenDeliveryIsPublished
63.16% covered (warning)
63.16%
12 / 19
0.00% covered (danger)
0.00%
0 / 1
3.45
 getQueueDispatcher
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkEventType
16.67% covered (danger)
16.67%
1 / 6
0.00% covered (danger)
0.00%
0 / 1
4.31
 triggerSyncTask
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getFeatureFlag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPrepareDataService
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 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoDeliveryRdf\model\DataStore;
24
25use oat\oatbox\event\Event;
26use oat\oatbox\log\LoggerAwareTrait;
27use oat\oatbox\service\ConfigurableService;
28use oat\oatbox\service\exception\InvalidServiceManagerException;
29use oat\tao\model\featureFlag\FeatureFlagChecker;
30use oat\tao\model\taskQueue\QueueDispatcher;
31use oat\taoDeliveryRdf\model\event\AbstractDeliveryEvent;
32use RuntimeException;
33use Throwable;
34
35class DeliveryMetadataListener extends ConfigurableService
36{
37    use LoggerAwareTrait;
38
39    public const SERVICE_ID = 'taoDeliveryRdf/DeliveryMetadataListener';
40
41    private const FILE_SYSTEM_ID = 'dataStore';
42    private const MAX_TRIES_DEFAULT = 10;
43    private const MAX_TRIES_OPTION_NAME = 'max_tries';
44
45    public function whenDeliveryIsPublished(Event $event): void
46    {
47        $featureFlag = $this->getFeatureFlag();
48        if (!$featureFlag->isEnabled('FEATURE_FLAG_ENABLE_DATA_STORE_STORAGE')) {
49            return;
50        }
51        try {
52            $this->logDebug(sprintf('Processing MetaData event for %s', get_class($event)));
53            $this->checkEventType($event);
54
55            $resourceSyncDTO = $this->getPrepareDataService()->getResourceSyncData(
56                $event->getDeliveryUri(),
57                $this->getOption(self::MAX_TRIES_OPTION_NAME, self::MAX_TRIES_DEFAULT),
58                true,
59                self::FILE_SYSTEM_ID
60            );
61
62            $this->triggerSyncTask($resourceSyncDTO);
63            $this->logDebug(sprintf('Event %s processed', get_class($event)));
64        } catch (Throwable $exception) {
65            $this->logError(sprintf(
66                'Error processing event %s: %s',
67                get_class($event),
68                $exception->getMessage()
69            ));
70        }
71    }
72
73    /**
74     * @throws InvalidServiceManagerException
75     */
76    private function getQueueDispatcher(): ConfigurableService
77    {
78        return $this->getServiceLocator()->get(QueueDispatcher::SERVICE_ID);
79    }
80
81    /**
82     * @throws RuntimeException
83     */
84    private function checkEventType(Event $event): void
85    {
86        if (!$event instanceof AbstractDeliveryEvent) {
87            throw new RuntimeException(sprintf(
88                "Wrong event type. Required instance of %s, %s given",
89                AbstractDeliveryEvent::class,
90                get_class($event)
91            ));
92        }
93    }
94
95    private function triggerSyncTask(ResourceSyncDTO $resourceSyncDTO): void
96    {
97        /** @var QueueDispatcher $queueDispatcher */
98        $queueDispatcher = $this->getQueueDispatcher();
99        $queueDispatcher->createTask(
100            new DeliverySyncTask(),
101            [$resourceSyncDTO, 0],
102            __(
103                'Syncing data of a delivery "%s".',
104                $resourceSyncDTO->getResourceId()
105            )
106        );
107    }
108
109    private function getFeatureFlag(): FeatureFlagChecker
110    {
111        return $this->getServiceLocator()->get(FeatureFlagChecker::class);
112    }
113
114    private function getPrepareDataService(): PrepareDataService
115    {
116        return $this->getServiceLocator()->get(PrepareDataService::class);
117    }
118}