Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemDuplicationEventProcessor
93.33% covered (success)
93.33%
14 / 15
66.67% covered (warning)
66.67%
2 / 3
8.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 process
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 isValidEventPayload
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
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) 2025 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoMediaManager\model\relation\event\processor;
24
25use oat\oatbox\event\Event;
26use oat\taoMediaManager\model\relation\MediaRelation;
27use oat\taoMediaManager\model\relation\repository\MediaRelationRepositoryInterface;
28use oat\taoMediaManager\model\relation\service\ItemMediaCollector;
29
30class ItemDuplicationEventProcessor implements EventProcessorInterface
31{
32    private MediaRelationRepositoryInterface $mediaRelationRepository;
33    private ItemMediaCollector $itemMediaCollector;
34
35    public function __construct(
36        MediaRelationRepositoryInterface $mediaRelationRepository,
37        ItemMediaCollector $itemMediaCollector
38    ) {
39        $this->mediaRelationRepository = $mediaRelationRepository;
40        $this->itemMediaCollector = $itemMediaCollector;
41    }
42
43    public function process(Event $event): void
44    {
45        $eventPayload = $event->jsonSerialize();
46        if (!$this->isValidEventPayload($eventPayload)) {
47            throw new InvalidEventException($event, 'Missing itemUri or cloneUri');
48        }
49
50        $itemUri = $eventPayload['itemUri'];
51        $cloneUri = $eventPayload['cloneUri'];
52
53        foreach ($this->itemMediaCollector->getItemMediaResources($itemUri) as $mediaUri) {
54            $mediaRelation = new MediaRelation(MediaRelation::ITEM_TYPE, $cloneUri);
55            $mediaRelation->withSourceId($mediaUri);
56            $this->mediaRelationRepository->save($mediaRelation);
57        }
58    }
59
60    private function isValidEventPayload($eventPayload): bool
61    {
62        return is_array($eventPayload)
63            && isset($eventPayload['itemUri'], $eventPayload['cloneUri'])
64            && is_string($eventPayload['itemUri'])
65            && is_string($eventPayload['cloneUri']);
66    }
67}