Failure conditions met
Pushb8268a...b3fdf9
failed — Build
created

TaggableEmitter::removeListenersWithTag()   A

↳ Parent: TaggableEmitter

Complexity

Conditions 3
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 13
Code Lines 7

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Events;
14
15
use League\Event\Emitter;
16
use Rocketeer\Services\Events\Listeners\TaggableListenerInterface;
17
use Rocketeer\Services\Events\Listeners\TaggedListener;
18
19
/**
20
 * Assigns tags to listeners to allow
21
 * batch operations on subsets of them.
22
 */
23
class TaggableEmitter extends Emitter
24
{
25
    /**
26
     * The currently active tag.
27
     *
28
     * @var string
29
     */
30
    protected $tag = '*';
31
32
    ////////////////////////////////////////////////////////////////////////////////
33
    //////////////////////////////////// TAGS //////////////////////////////////////
34
    ////////////////////////////////////////////////////////////////////////////////
35
36
    /**
37
     * @return string
38
     */
39
    public function getTag()
40
    {
41
        return $this->tag;
42
    }
43
44
    /**
45
     * @param string $tag
46
     */
47 440
    public function setTag($tag)
48
    {
49 440
        $this->tag = $tag;
50 440
    }
51
52
    /**
53
     * @param string   $tags
54
     * @param callable $callable
55
     */
56 440
    public function onTag($tags, callable $callable)
57
    {
58 440
        $previous = $this->tag;
59 440
        $this->setTag($tags);
60 440
        $callable();
61 440
        $this->setTag($previous);
62 440
    }
63
64
    /**
65
     * @param string $tag
66
     *
67
     * @return $this
68
     */
69 440
    public function removeListenersWithTag($tag)
70
    {
71 440
        foreach ($this->listeners as $event => $priorities) {
72 42
            $this->clearSortedListeners($event);
73 42
            foreach ($priorities as $priority => $listeners) {
74
                $this->listeners[$event][$priority] = array_filter($listeners, function (TaggableListenerInterface $listener) use ($tag) {
75 42
                    return $listener->getTag() !== $tag;
76 42
                });
77 42
            }
78 440
        }
79
80 440
        return $this;
81
    }
82
83
    ////////////////////////////////////////////////////////////////////////////////
84
    ////////////////////////////////// OVERRIDES ///////////////////////////////////
85
    ////////////////////////////////////////////////////////////////////////////////
86
87
    /**
88
     * [email protected]}
89
     */
90 316
    protected function getSortedListeners($event)
91
    {
92 316
        $listeners = parent::getSortedListeners($event);
93 316
        $listeners = array_filter($listeners, function (TaggableListenerInterface $listener) {
94 45
            return $listener->getTag() === $this->tag || $listener->getTag() === '*' || $this->tag === '*';
95 316
        });
96
97 316
        return $listeners;
98
    }
99
100
    /**
101
     * [email protected]}
102
     */
103 105
    protected function ensureListener($listener)
104
    {
105 105
        $listener = parent::ensureListener($listener);
106 105
        if (!$listener instanceof TaggableListenerInterface) {
107 6
            $listener = new TaggedListener($listener);
108 6
        }
109
110 105
        $listener->setTag($this->tag);
111
112 105
        return $listener;
113 1
    }
114
}
115