Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
24 / 32
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemCreationListener
75.00% covered (warning)
75.00%
24 / 32
33.33% covered (danger)
33.33%
1 / 3
15.64
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 populateUniqueId
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
7.01
 getEventItem
22.22% covered (danger)
22.22%
2 / 9
0.00% covered (danger)
0.00%
0 / 1
16.76
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) 2024 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\UniqueId\Listener;
24
25use core_kernel_classes_Resource;
26use InvalidArgumentException;
27use oat\generis\model\data\Ontology;
28use oat\oatbox\event\Event;
29use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
30use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface;
31use oat\tao\model\resources\Event\InstanceCopiedEvent;
32use oat\tao\model\TaoOntology;
33use oat\tao\model\Translation\Service\AbstractQtiIdentifierSetter;
34use oat\taoItems\model\event\ItemCreatedEvent;
35use oat\taoItems\model\event\ItemDuplicatedEvent;
36use oat\taoQtiItem\model\event\ItemImported;
37use oat\taoQtiItem\model\qti\Identifier\Service\QtiIdentifierSetter;
38
39class ItemCreationListener
40{
41    private FeatureFlagCheckerInterface $featureFlagChecker;
42    private Ontology $ontology;
43    private IdentifierGeneratorInterface $identifierGenerator;
44    private QtiIdentifierSetter $qtiIdentifierSetter;
45
46    public function __construct(
47        FeatureFlagCheckerInterface $featureFlagChecker,
48        Ontology $ontology,
49        IdentifierGeneratorInterface $identifierGenerator,
50        QtiIdentifierSetter $qtiIdentifierSetter
51    ) {
52        $this->featureFlagChecker = $featureFlagChecker;
53        $this->ontology = $ontology;
54        $this->identifierGenerator = $identifierGenerator;
55        $this->qtiIdentifierSetter = $qtiIdentifierSetter;
56    }
57
58    public function populateUniqueId(Event $event): void
59    {
60        if (
61            !$event instanceof ItemCreatedEvent
62            && !$event instanceof ItemImported
63            && !$event instanceof ItemDuplicatedEvent
64            && !$event instanceof InstanceCopiedEvent
65        ) {
66            return;
67        }
68
69        if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_UNIQUE_NUMERIC_QTI_IDENTIFIER')) {
70            return;
71        }
72
73        $item = $this->getEventItem($event);
74
75        // We are not going to populate Unique ID for translations
76        if ($item->getRootId() !== TaoOntology::CLASS_URI_ITEM) {
77            return;
78        }
79
80        $identifier = $this->identifierGenerator->generate([IdentifierGeneratorInterface::OPTION_RESOURCE => $item]);
81        $item->editPropertyValues(
82            $this->ontology->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER),
83            $identifier
84        );
85
86        $this->qtiIdentifierSetter->set([
87            AbstractQtiIdentifierSetter::OPTION_RESOURCE => $item,
88            AbstractQtiIdentifierSetter::OPTION_IDENTIFIER => $identifier,
89        ]);
90    }
91
92    private function getEventItem(Event $event): core_kernel_classes_Resource
93    {
94        if ($event instanceof ItemCreatedEvent) {
95            return $this->ontology->getResource($event->getItemUri());
96        }
97
98        if ($event instanceof ItemImported) {
99            return $event->getRdfItem();
100        }
101
102        if ($event instanceof ItemDuplicatedEvent) {
103            return $this->ontology->getResource($event->getCloneUri());
104        }
105
106        if ($event instanceof InstanceCopiedEvent) {
107            return $this->ontology->getResource($event->getInstanceUri());
108        }
109
110        throw new InvalidArgumentException('Cannot retrieve event item: event %s is not supported', get_class($event));
111    }
112}