Completed
Push — master ( 8155ff...9fff48 )
by Changwan
03:00
created

Dispatcher::on()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Event;
3
4
use Psr\Container\ContainerInterface;
5
use Wandu\Q\Queue;
6
use RuntimeException;
7
8
class Dispatcher implements DispatcherInterface
9
{
10
    /** @var \Psr\Container\ContainerInterface */
11
    protected $container;
12
13
    /** @var array|\Wandu\Event\ListenerInterface[][]|callable[][] */
14
    protected $listeners = [];
15
16 20
    public function __construct(ContainerInterface $container)
17
    {
18 20
        $this->container = $container;
19 20
    }
20
    
21
    /**
22
     * {@inheritdoc}
23
     */
24 6
    public function on(string $event, $listener)
25
    {
26 6
        if (!array_key_exists($event, $this->listeners)) {
27 6
            $this->listeners[$event] = [];
28
        }
29 6
        $this->listeners[$event][] = $listener;
30 6
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function off(string $event, $listener = null)
36
    {
37
        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...
38
            // @todo
39
        } else {
40
            $this->listeners[$event] = [];
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 20
    public function trigger(EventInterface $event)
48
    {
49 20
        if (!count($this->listeners)) {
50 18
            return;
51
        }
52 6
        if ($event instanceof ViaQueue) {
53 1
            if (!$this->container->has(Queue::class)) {
54
                // @todo fix error message
55
                throw new RuntimeException('cannot load queue.');
56
            }
57 1
            $this->container->get(Queue::class)->enqueue([
58 1
                'method' => 'event:execute',
59 1
                'event' => $event,
60
            ]);
61
        } else {
62 5
            $this->executeListeners($event);
63
        }
64 6
    }
65
66
    /**
67
     * @param \Wandu\Event\EventInterface $event
68
     * @return array
69
     */
70 5
    public function executeListeners(EventInterface $event)
71
    {
72 5
        $executedListeners = [];
73 5
        $eventName = get_class($event);
74 5
        if (!isset($this->listeners[$eventName])) {
75
            return [];
76
        }
77 5
        foreach ($this->listeners[$eventName] as $listener) {
78 5
            if (is_callable($listener)) {
79
                call_user_func($listener, $event);
80
                $executedListeners[] = 'callable';
81 5
            } elseif (is_string($listener)) {
82 1
                $this->container->get($listener)->call($event);
83 1
                $executedListeners[] = $listener;
84 4
            } elseif ($listener instanceof ListenerInterface) {
85 4
                $listener->call($event);
86 5
                $executedListeners[] = get_class($listener);
87
            }
88
        }
89 5
        return $executedListeners;
90
    }
91
}
92