Completed
Push — master ( 948520...165aa8 )
by Aurimas
14:17
created

AdvanceEventEmitterTrait::getListeners()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Thruster\Component\EventEmitter;
4
5
/**
6
 * Trait AdvanceEventEmitterTrait
7
 *
8
 * @package Thruster\Component\EventEmitter
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
trait AdvanceEventEmitterTrait
12
{
13
    /**
14
     * @var callable[]
15
     */
16
    protected $listeners = [];
17
18
    /**
19
     * @param string   $eventName
20
     * @param callable $listener
21
     *
22
     * @return $this
23
     */
24 14
    public function on(string $eventName, callable $listener) : self
25
    {
26 14
        $this->listeners[$eventName][] = $listener;
27
28 14
        return $this;
29
    }
30
31
    /**
32
     * @param string   $eventName
33
     * @param callable $listener
34
     *
35
     * @return $this
36
     */
37 View Code Duplication
    public function once(string $eventName, callable $listener) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39 2
        $onceListener = function (EventInterface $event) use (&$onceListener, $eventName, $listener) {
40 2
            $this->removeListener($eventName, $onceListener);
41
42 2
            call_user_func($listener, $event);
43 2
        };
44
45 2
        $this->on($eventName, $onceListener);
46
47 2
        return $this;
48
    }
49
50
    /**
51
     * @param string   $eventName
52
     * @param callable $listener
53
     *
54
     * @return $this
55
     */
56 4 View Code Duplication
    public function removeListener(string $eventName, callable $listener) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 4
        if (array_key_exists($eventName, $this->listeners)) {
59 3
            $index = array_search($listener, $this->listeners[$eventName], true);
60
61 3
            if (false !== $index) {
62 3
                unset($this->listeners[$eventName][$index]);
63
            }
64
        }
65
66 4
        return $this;
67
    }
68
69
    /**
70
     * @param string $eventName
71
     *
72
     * @return $this
73
     */
74 3
    public function removeListeners(string $eventName = null) : self
75
    {
76 3
        if (null !== $eventName) {
77 2
            unset($this->listeners[$eventName]);
78
        } else {
79 1
            $this->listeners = [];
80
        }
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * @param string $eventName
87
     *
88
     * @return array|callable[]
89
     */
90 12
    public function getListeners(string $eventName) : array
91
    {
92 12
        return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : [];
93
    }
94
95
    /**
96
     * @param string         $eventName
97
     * @param EventInterface $event
98
     *
99
     * @return $this
100
     */
101 12
    public function emit(string $eventName, EventInterface $event = null) : self
102
    {
103 12
        $event = $event ?? new Event();
104
105 12
        foreach ($this->getListeners($eventName) as $listener) {
106 8
            call_user_func($listener, $event);
107
108 8
            if (true == $event->stopPropagation()) {
109 8
                break;
110
            }
111
        }
112
113 12
        return $this;
114
    }
115
}
116