Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
55.56% |
10 / 18 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
MediaRelationListener | |
55.56% |
10 / 18 |
|
50.00% |
4 / 8 |
21.62 | |
0.00% |
0 / 1 |
whenItemIsUpdated | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
whenInstanceCopiedEvent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
whenItemIsDuplicated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
whenResourceIsRemoved | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
whenItemIsRemoved | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
whenMediaIsRemoved | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
whenMediaIsSaved | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
process | |
54.55% |
6 / 11 |
|
0.00% |
0 / 1 |
5.50 |
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 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoMediaManager\model\relation\event; |
24 | |
25 | use oat\oatbox\event\Event; |
26 | use oat\oatbox\log\LoggerAwareTrait; |
27 | use oat\oatbox\service\ConfigurableService; |
28 | use oat\oatbox\service\ServiceNotFoundException; |
29 | use oat\taoMediaManager\model\relation\event\processor\EventInstanceCopiedProcessor; |
30 | use oat\taoMediaManager\model\relation\event\processor\EventProcessorInterface; |
31 | use oat\taoMediaManager\model\relation\event\processor\ItemRemovedEventProcessor; |
32 | use oat\taoMediaManager\model\relation\event\processor\ItemDuplicationEventProcessor; |
33 | use oat\taoMediaManager\model\relation\event\processor\ItemUpdatedEventProcessor; |
34 | use oat\taoMediaManager\model\relation\event\processor\MediaRemovedEventProcessor; |
35 | use oat\taoMediaManager\model\relation\event\processor\MediaSavedEventProcessor; |
36 | use oat\taoMediaManager\model\relation\event\processor\ResourceDeleteEventProcessor; |
37 | use Throwable; |
38 | |
39 | class MediaRelationListener extends ConfigurableService |
40 | { |
41 | use LoggerAwareTrait; |
42 | |
43 | public function whenItemIsUpdated(Event $event): void |
44 | { |
45 | $this->process(ItemUpdatedEventProcessor::class, $event); |
46 | } |
47 | |
48 | public function whenInstanceCopiedEvent(Event $event): void |
49 | { |
50 | $this->process(EventInstanceCopiedProcessor::class, $event); |
51 | } |
52 | |
53 | public function whenItemIsDuplicated(Event $event) |
54 | { |
55 | $this->process(ItemDuplicationEventProcessor::class, $event); |
56 | } |
57 | |
58 | public function whenResourceIsRemoved(Event $event): void |
59 | { |
60 | $this->process(ResourceDeleteEventProcessor::class, $event); |
61 | } |
62 | |
63 | public function whenItemIsRemoved(Event $event): void |
64 | { |
65 | $this->process(ItemRemovedEventProcessor::class, $event); |
66 | } |
67 | |
68 | public function whenMediaIsRemoved(Event $event): void |
69 | { |
70 | $this->process(MediaRemovedEventProcessor::class, $event); |
71 | } |
72 | |
73 | public function whenMediaIsSaved(Event $event): void |
74 | { |
75 | $this->process(MediaSavedEventProcessor::class, $event); |
76 | } |
77 | |
78 | private function process(string $processor, Event $event): void |
79 | { |
80 | try { |
81 | $this->logDebug(sprintf('Processing event %s', get_class($event))); |
82 | |
83 | /** @var EventProcessorInterface $processor */ |
84 | try { |
85 | $processor = $this->getServiceLocator()->get($processor); |
86 | } catch (ServiceNotFoundException $exception) { |
87 | // Fallback |
88 | $processor = $this->getServiceManager()->getContainer()->get($processor); |
89 | } catch (Throwable $exception) { |
90 | $this->logError(sprintf('Error getting processor %s: %s', $processor, $exception->getMessage())); |
91 | return; |
92 | } |
93 | $processor->process($event); |
94 | |
95 | $this->logDebug(sprintf('Event %s processed', get_class($event))); |
96 | } catch (Throwable $exception) { |
97 | $this->logError(sprintf('Error processing event %s: %s', get_class($event), $exception->getMessage())); |
98 | } |
99 | } |
100 | } |