Completed
Push — master ( f18251...40c839 )
by Flo
15s
created

Observer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A instance() 0 13 2
B trigger() 0 26 5
1
<?php
2
3
namespace Faulancer\Event;
4
5
use Faulancer\Service\Config;
6
use Faulancer\ServiceLocator\ServiceLocator;
7
8
/**
9
 * Class Observer
10
 * @package Faulancer\Event
11
 * @author  Florian Knapp <[email protected]>
12
 */
13
class Observer
14
{
15
16
    /** @var self */
17
    protected static $instance;
18
19
    /** @var AbstractListener[] */
20
    protected static $listener = [];
21
22
    /**
23
     * Observer constructor (private).
24
     */
25
    private function __construct() {}
26
27
    /**
28
     * Yeah... singleton... i know
29
     *
30
     * @return self
31
     */
32
    public static function instance()
33
    {
34
        if (!self::$instance) {
35
36
            /** @var Config $config */
37
            $config = ServiceLocator::instance()->get(Config::class);
38
            self::$listener = $config->get('eventListener');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('eventListener') of type * is incompatible with the declared type array<integer,object<Fau...vent\AbstractListener>> of property $listener.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
            self::$instance = new self();
40
41
        }
42
43
        return self::$instance;
44
    }
45
46
    /**
47
     * Trigger listeners if registered for the type
48
     *
49
     * @param AbstractEvent $event
50
     */
51
    public function trigger(AbstractEvent $event)
52
    {
53
        foreach (self::$listener as $typeName => $listenerList) {
54
55
            /** @var AbstractListener[] $listenerList */
56
            foreach ($listenerList as $listener) {
57
58
                if ($typeName === $event::NAME) {
59
60
                    if (!class_exists($listener)) {
61
                        continue;
62
                    }
63
64
                    /** @var AbstractListener $listener */
65
                    $listener = new $listener();
66
                    $listener->create();
67
                    $callback = $listener->getCallback();
68
                    $callback->execute($event);
0 ignored issues
show
Bug introduced by
The method execute cannot be called on $callback (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
69
70
                }
71
72
            }
73
74
        }
75
76
    }
77
78
}