Completed
Push — master ( d8f60e...a72876 )
by Changwan
03:40
created

Dispatcher   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 30
cts 39
cp 0.7692
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A on() 0 7 2
A off() 0 8 2
A trigger() 0 18 4
B executeListeners() 0 21 6
1
<?php
2
namespace Wandu\Event;
3
4
use Interop\Container\ContainerInterface;
5
use Wandu\Q\Queue;
6
7
class Dispatcher implements DispatcherInterface
8
{
9
    /** @var \Interop\Container\ContainerInterface */
10
    protected $container;
11
12
    /** @var array|\Wandu\Event\ListenerInterface[][]|callable[][] */
13
    protected $listeners = [];
14
15 20
    public function __construct(ContainerInterface $container)
16
    {
17 20
        $this->container = $container;
18 20
    }
19
    
20
    /**
21
     * {@inheritdoc}
22
     */
23 6
    public function on(string $event, $listener)
24
    {
25 6
        if (!array_key_exists($event, $this->listeners)) {
26 6
            $this->listeners[$event] = [];
27
        }
28 6
        $this->listeners[$event][] = $listener;
29 6
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function off(string $event, $listener = null)
35
    {
36
        if ($listener) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
37
            // @todo
38
        } else {
39
            $this->listeners[$event] = [];
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 20
    public function trigger(EventInterface $event)
47
    {
48 20
        if (!count($this->listeners)) {
49 18
            return;
50
        }
51 6
        if ($event instanceof ViaQueue) {
52 1
            if (!$this->container->has(Queue::class)) {
53
                // @todo fix error message
54
                throw new \InvalidArgumentException('Cannot load queue.');
55
            }
56 1
            $this->container->get(Queue::class)->enqueue([
57 1
                'method' => 'event:execute',
58 1
                'event' => $event,
59
            ]);
60
        } else {
61 5
            $this->executeListeners($event);
62
        }
63 6
    }
64
65
    /**
66
     * @param \Wandu\Event\EventInterface $event
67
     * @return array
68
     */
69 5
    public function executeListeners(EventInterface $event)
70
    {
71 5
        $executedListeners = [];
72 5
        $eventName = get_class($event);
73 5
        if (!isset($this->listeners[$eventName])) {
74
            return [];
75
        }
76 5
        foreach ($this->listeners[$eventName] as $listener) {
77 5
            if (is_callable($listener)) {
78
                call_user_func($listener, $event);
79
                $executedListeners[] = 'callable';
80 5
            } elseif (is_string($listener)) {
81 1
                $this->container->get($listener)->call($event);
82 1
                $executedListeners[] = $listener;
83
            } elseif ($listener instanceof ListenerInterface) {
84 4
                $listener->call($event);
85 5
                $executedListeners[] = get_class($listener);
86
            }
87
        }
88 5
        return $executedListeners;
89
    }
90
}
91