EventEmitterTrait::once()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 12
loc 12
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thruster\Component\EventEmitter;
4
5
/**
6
 * Trait EventEmitterTrait
7
 *
8
 * @package Thruster\Component\EventEmitter
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
trait EventEmitterTrait
12
{
13
    /**
14
     * @var callable[]
15
     */
16
    protected $listeners = [];
17
18
    /**
19
     * @param string   $event
20
     * @param callable $listener
21
     *
22
     * @return $this
23
     */
24 14
    public function on(string $event, callable $listener) : self
25
    {
26 14
        $this->listeners[$event][] = $listener;
27
28 14
        return $this;
29
    }
30
31
    /**
32
     * @param string   $event
33
     * @param callable $listener
34
     *
35
     * @return $this
36
     */
37 View Code Duplication
    public function once(string $event, 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 () use (&$onceListener, $event, $listener) {
40 2
            $this->removeListener($event, $onceListener);
41
42 2
            call_user_func_array($listener, func_get_args());
43 2
        };
44
45 2
        $this->on($event, $onceListener);
46
47 2
        return $this;
48
    }
49
50
    /**
51
     * @param string   $event
52
     * @param callable $listener
53
     *
54
     * @return $this
55
     */
56 4 View Code Duplication
    public function removeListener(string $event, 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($event, $this->listeners)) {
59 3
            $index = array_search($listener, $this->listeners[$event], true);
60
61 3
            if (false !== $index) {
62 3
                unset($this->listeners[$event][$index]);
63
            }
64
        }
65
66 4
        return $this;
67
    }
68
69
    /**
70
     * @param string $event
71
     *
72
     * @return $this
73
     */
74 3
    public function removeListeners(string $event = null) : self
75
    {
76 3
        if (null !== $event) {
77 2
            unset($this->listeners[$event]);
78
        } else {
79 1
            $this->listeners = [];
80
        }
81
82 3
        return $this;
83
    }
84
85
    /**
86
     * @param string $event
87
     *
88
     * @return array|callable[]
89
     */
90 12
    public function getListeners(string $event) : array
91
    {
92 12
        return isset($this->listeners[$event]) ? $this->listeners[$event] : [];
93
    }
94
95
    /**
96
     * @param string $event
97
     * @param array  $arguments
98
     *
99
     * @return $this
100
     */
101 12
    public function emit(string $event, array $arguments = []) : self
102
    {
103 12
        foreach ($this->getListeners($event) as $listener) {
104 8
            call_user_func_array($listener, $arguments);
105
        }
106
107 12
        return $this;
108
    }
109
}
110