Completed
Branch master (282aeb)
by Andrey
03:39 queued 01:40
created

EventManager::detach()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace Event;
4
5
use Psr\EventManager\EventManagerInterface;
6
use Psr\EventManager\EventInterface;
7
use Event\ListenerQueue;
8
9
class EventManager implements EventManagerInterface
10
{
11
    private $events = [];
12
    /**
13
     * Attaches a listener to an event
14
     *
15
     * @param string $event the event to attach too
16
     * @param callable $callback a callable function
17
     * @param int $priority the priority at which the $callback executed
18
     * @return bool true on success false on failure
19
     */
20
    public function attach($event, $callback, $priority = 0)
21
    {
22
        if (!is_string($event) 
23
        && !is_callable($callback) 
24
        && !is_integer($priority)) {
25
            return false;
26
        }
27
28
        if (!array_key_exists($event, $this->events)) {
29
            $this->events[$event] = new ListenerQueue;
30
        }
31
32
        $this->events[$event]->add($callback, $priority);
33
    }
34
35
    /**
36
     * Detaches a listener from an event
37
     *
38
     * @param string $event the event to attach too
39
     * @param callable $callback a callable function
40
     * @return bool true on success false on failure
41
     */
42
    public function detach($event, $callback)
43
    {
44
        if (!is_string($event) && !is_callable($callback)) {
45
            return false;
46
        }
47
48
        if (array_key_exists($event, $this->events)) {
49
            $this->events[$event]->eject($callback);
50
        }
51
52
        return true;
53
    }
54
55
    /**
56
     * Clear all listeners for a given event
57
     *
58
     * @param  string $event
59
     * @return bool true on success false on failure
60
     */
61
    public function clearListeners($event)
62
    {
63
        if (!is_string($event)) {
64
            return false;
65
        }
66
67
        $this->events[$event]->clear();
68
    }
69
70
    /**
71
     * Trigger an event
72
     *
73
     * Can accept an EventInterface or will create one if not passed
74
     *
75
     * @param  string|EventInterface $event
76
     * @param  object|string $target
77
     * @param  array|object $argv
78
     * @return mixed
79
     */
80
    public function trigger($event, $target = null, $argv = [])
81
    {
82
        if (!is_string($event) && !is_object($event)
83
        && !is_string($target) && !is_object($target)
84
        && !is_array($argv) && !is_object($argv)) {
85
            return false;
86
        }
87
88
        if ($event instanceof EventInterface) {
89
            $event = $event->getName();
90
        }
91
92
        $event = $this->events[$event];
93
94
        while ($event->valid())
95
        {
96
            call_user_func_array(
97
                $event->top(), 
98
                $argv
99
            );
100
        }
101
    }
102
}