Passed
Push — master ( 239fe0...528f7d )
by Kacper
05:10
created

BetterEmitter::getConditionalCallable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 0
loc 13
ccs 3
cts 5
cp 0.6
crap 3.576
rs 9.4285
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\Exception\InvalidArgumentException;
19
use Kadet\Xmpp\Utils\filter as with;
20
use Evenement\EventEmitterTrait;
21
22
trait BetterEmitter
23
{
24
    /**
25
     * @var PriorityCollection[]
26
     */
27
    protected $listeners;
28
29
    use EventEmitterTrait;
30
31 11
    public function on($event, callable $listener, $condition = null, int $priority = 0)
32
    {
33 11
        $this->addListener($event, $this->getConditionalCallable($listener, $condition), $priority);
34 11
    }
35
36
    public function once($event, callable $listener, $condition = null, int $priority = 0)
37
    {
38
        $this->on($event, $this->getOnceCallable($listener, $event), $condition, $priority);
39
    }
40
41
    public function removeListener($event, callable $listener)
42
    {
43
        if(!isset($this->listeners[$event])) {
44
            return false;
45
        }
46
47
        $this->listeners[$event]->remove($listener);
48
        return true;
49
    }
50
51 11
    public function emit($event, array $arguments = [])
52
    {
53 11
        foreach ($this->listeners($event) as $listener) {
54 11
            if ($listener(...$arguments) === false) {
55 11
                return false;
56
            }
57
        }
58
59 11
        return true;
60
    }
61
62
    public function reference(callable $callable, int $position = 0) : callable
63
    {
64
        return \Kadet\Xmpp\Utils\helper\partial($callable, $this, $position);
65
    }
66
67 11
    private function addListener($event, callable $listener, int $priority = 0)
68
    {
69 11
        if(!isset($this->listeners[$event])) {
70 11
            $this->listeners[$event] = new PriorityCollection();
71
        }
72
73 11
        $this->listeners[$event]->insert($listener, $priority);
74 11
    }
75
76 11
    private function getConditionalCallable(callable $listener, $condition) : callable
77
    {
78 11
        if ($condition === null) {
79 11
            return $listener;
80
        }
81
82
        $condition = $this->emitterResolveCondition($condition);
83
        return function (...$arguments) use ($listener, $condition) {
84
            if ($condition(...$arguments)) {
85
                $listener(...$arguments);
86
            }
87
        };
88
    }
89
90
    private function getOnceCallable(callable $listener, $event) : callable
91
    {
92
        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...
93
            $this->removeListener($event, $onceListener);
94
95
            $listener(...$arguments);
96
        };
97
    }
98
99
    protected function emitterResolveCondition($condition) : callable
100
    {
101
        if (is_callable($condition)) {
102
            return $condition;
103
        } elseif (class_exists($condition)) {
104
            return with\ofType($condition);
105
        } else {
106
            throw new InvalidArgumentException('$condition must be either class-name or callable, ' . helper\typeof($condition) . ' given.');
107
        }
108
    }
109
}
110