Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventInstanceCopiedProcessor
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
1 / 2
5.05
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
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
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\tao\model\resources\Event\InstanceCopiedEvent;
27use oat\taoMediaManager\model\relation\MediaRelation;
28use oat\taoMediaManager\model\relation\repository\MediaRelationRepositoryInterface;
29use oat\taoMediaManager\model\relation\service\ItemMediaCollector;
30
31class EventInstanceCopiedProcessor implements EventProcessorInterface
32{
33    private MediaRelationRepositoryInterface $mediaRelationRepository;
34    private ItemMediaCollector $itemMediaCollector;
35
36    public function __construct(
37        MediaRelationRepositoryInterface $mediaRelationRepository,
38        ItemMediaCollector $itemMediaCollector
39    ) {
40        $this->mediaRelationRepository = $mediaRelationRepository;
41        $this->itemMediaCollector = $itemMediaCollector;
42    }
43
44    /**
45     * @param InstanceCopiedEvent $event
46     */
47    public function process(Event $event): void
48    {
49        if (!$event instanceof InstanceCopiedEvent && $event->getOriginInstanceUri() === null) {
50            throw new InvalidEventException($event);
51        }
52
53        foreach ($this->itemMediaCollector->getItemMediaResources($event->getOriginInstanceUri()) as $mediaUri) {
54            $mediaRelation = new MediaRelation(MediaRelation::ITEM_TYPE, $event->getInstanceUri());
55            $mediaRelation->withSourceId($mediaUri);
56            $this->mediaRelationRepository->save($mediaRelation);
57        }
58    }
59}