Passed
Push — master ( 670096...d7e64a )
by Andreas
22:23
created

watcher::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.events
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\events;
10
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
use midcom_core_dbaobject;
13
use midcom_helper__componentloader;
14
15
/**
16
 * midcom DBA subscriber
17
 *
18
 * @package midcom.events
19
 */
20
class watcher implements EventSubscriberInterface
21
{
22
    private $watches;
23
24
    private $map = [
25
        dbaevent::CREATE => \MIDCOM_OPERATION_DBA_CREATE,
26
        dbaevent::UPDATE => \MIDCOM_OPERATION_DBA_UPDATE,
27
        dbaevent::DELETE => \MIDCOM_OPERATION_DBA_DELETE,
28
        dbaevent::IMPORT => \MIDCOM_OPERATION_DBA_IMPORT,
29
    ];
30
31
    /**
32
     * @var midcom_helper__componentloader
33
     */
34
    private $loader;
35
36
    public function __construct(midcom_helper__componentloader $loader, array $watches)
37
    {
38
        $this->loader = $loader;
39
        $this->watches = $watches;
40
    }
41
42
    public static function getSubscribedEvents()
43
    {
44
        return [
45
            dbaevent::CREATE => ['handle_event'],
46
            dbaevent::UPDATE => ['handle_event'],
47
            dbaevent::DELETE => ['handle_event'],
48
            dbaevent::IMPORT => ['handle_event'],
49
        ];
50
    }
51
52 289
    private function check_class(midcom_core_dbaobject $object, array $classes) : bool
53
    {
54 289
        $found = empty($classes);
55 289
        foreach ($classes as $classname) {
56 289
            if (is_a($object, $classname)) {
57 68
                return true;
58
            }
59
        }
60 289
        return $found;
61
    }
62
63 289
    public function handle_event(dbaevent $event, $name)
64
    {
65 289
        $object = $event->get_object();
66 289
        $operation = $this->map[$name];
67 289
        foreach ($this->watches[$operation] as $watch) {
68 289
            $classes = current($watch);
69 289
            if (!$this->check_class($object, $classes)) {
70 289
                continue;
71
            }
72 185
            $component = key($watch);
73
            try {
74 185
                $interface = $this->loader->get_interface_class($component);
75
            } catch (\midcom_error $e) {
76
                debug_add("Failed to load the component {$component}: " . $e->getMessage(), MIDCOM_LOG_INFO);
77
                continue;
78
            }
79 185
            debug_add("Calling [{$component}]_interface->trigger_watch({$operation}, \$object)");
80
81 185
            $interface->trigger_watch($operation, $object);
82
        }
83 289
    }
84
}
85