Test Failed
Pull Request — master (#19)
by Flo
02:33
created

Observer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 62
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 22 4
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 AbstractEventType $eventType
50
     */
51
    public function trigger(AbstractEventType $eventType)
52
    {
53
        foreach (self::$listener as $typeName => $listenerList) {
54
55
            /** @var AbstractListener[] $listenerList */
56
            foreach ($listenerList as $listener) {
57
58
                if ($typeName === $eventType::EVENT_TYPE) {
59
60
                    /** @var AbstractListener $listener */
61
                    $listener = new $listener($eventType);
62
                    $listener->create()->getCallback()->execute($eventType);
0 ignored issues
show
Bug introduced by
The method execute cannot be called on $listener->create()->getCallback() (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...
63
64
                }
65
66
            }
67
68
69
70
        }
71
72
    }
73
74
}