Completed
Push — master ( 528f7d...f4b721 )
by Kacper
04:19
created

BetterEmitter::reference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils;
17
18
use Kadet\Xmpp\Utils\filter as with;
19
use Evenement\EventEmitterTrait;
20
21
trait BetterEmitter
22
{
23
    /**
24
     * @var PriorityCollection[]
25
     */
26
    protected $listeners;
27
28
    use EventEmitterTrait;
29
30 11
    public function on($event, callable $listener, $condition = null, int $priority = 0)
31
    {
32 11
        $this->addListener($event, $this->getConditionalCallable($listener, $condition), $priority);
33 11
    }
34
35
    public function once($event, callable $listener, $condition = null, int $priority = 0)
36
    {
37
        $this->on($event, $this->getOnceCallable($listener, $event), $condition, $priority);
38
    }
39
40
    public function removeListener($event, callable $listener)
41
    {
42
        if(!isset($this->listeners[$event])) {
43
            return false;
44
        }
45
46
        $this->listeners[$event]->remove($listener);
47
        return true;
48
    }
49
50 11
    public function emit($event, array $arguments = [])
51
    {
52 11
        foreach ($this->listeners($event) as $listener) {
53 11
            if ($listener(...$arguments) === false) {
54 11
                return false;
55
            }
56
        }
57
58 11
        return true;
59
    }
60
61
    public function reference(callable $callable, int $position = 0) : callable
62
    {
63
        return \Kadet\Xmpp\Utils\helper\partial($callable, $this, $position);
64
    }
65
66 11
    private function addListener($event, callable $listener, int $priority = 0)
67
    {
68 11
        if(!isset($this->listeners[$event])) {
69 11
            $this->listeners[$event] = new PriorityCollection();
70
        }
71
72 11
        $this->listeners[$event]->insert($listener, $priority);
73 11
    }
74
75 11
    private function getConditionalCallable(callable $listener, $condition) : callable
76
    {
77 11
        if ($condition === null) {
78 11
            return $listener;
79
        }
80
81
        $condition = with\predicate($condition);
82
        return function (...$arguments) use ($listener, $condition) {
83
            if ($condition(...$arguments)) {
84
                $listener(...$arguments);
85
            }
86
        };
87
    }
88
89
    private function getOnceCallable(callable $listener, $event) : callable
90
    {
91
        return $onceListener = function (...$arguments) use (&$onceListener, $event, $listener) {
0 ignored issues
show
Unused Code introduced by
$onceListener is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
            $this->removeListener($event, $onceListener);
93
94
            $listener(...$arguments);
95
        };
96
    }
97
}
98