Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ItemUpdatedEventProcessor
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 process
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 mustUpdateItemRelation
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getAggregatedMediaIds
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getItemRelationUpdateService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIdDiscoverService
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) 2020 (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\oatbox\service\ConfigurableService;
27use oat\taoItems\model\event\ItemUpdatedEvent;
28use oat\taoMediaManager\model\relation\service\IdDiscoverService;
29use oat\taoMediaManager\model\relation\service\update\ItemRelationUpdateService;
30
31class ItemUpdatedEventProcessor extends ConfigurableService implements EventProcessorInterface
32{
33    private const INCLUDE_ELEMENT_REFERENCES_KEY = 'includeElementReferences';
34    private const OBJECT_ELEMENT_REFERENCES_KEY = 'objectElementReferences';
35    private const IMG_ELEMENT_REFERENCES_KEY = 'imgElementReferences';
36
37    /**
38     * @inheritDoc
39     */
40    public function process(Event $event): void
41    {
42        if (!$event instanceof ItemUpdatedEvent) {
43            throw new InvalidEventException($event);
44        }
45
46        $data = (array)$event->getData();
47
48        if ($this->mustUpdateItemRelation($data)) {
49            $this->getItemRelationUpdateService()
50                ->updateByTargetId($event->getItemUri(), $this->getAggregatedMediaIds($data));
51        }
52    }
53
54    private function mustUpdateItemRelation(array $data): bool
55    {
56        return array_key_exists(self::INCLUDE_ELEMENT_REFERENCES_KEY, $data)
57            || array_key_exists(self::OBJECT_ELEMENT_REFERENCES_KEY, $data)
58            || array_key_exists(self::IMG_ELEMENT_REFERENCES_KEY, $data);
59    }
60
61    private function getAggregatedMediaIds(array $data): array
62    {
63        return $this->getIdDiscoverService()->discover(
64            array_merge(
65                $data[self::INCLUDE_ELEMENT_REFERENCES_KEY] ?? [],
66                $data[self::OBJECT_ELEMENT_REFERENCES_KEY] ?? [],
67                $data[self::IMG_ELEMENT_REFERENCES_KEY] ?? []
68            )
69        );
70    }
71
72    private function getItemRelationUpdateService(): ItemRelationUpdateService
73    {
74        return $this->getServiceLocator()->get(ItemRelationUpdateService::class);
75    }
76
77    private function getIdDiscoverService(): IdDiscoverService
78    {
79        return $this->getServiceLocator()->get(IdDiscoverService::class);
80    }
81}