Failed Conditions
Push — develop ( b8268a...b3fdf9 )
by Maxime
19:19
created

TaggableEmitter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 11
c 4
b 1
f 2
lcom 2
cbo 3
dl 0
loc 92
ccs 32
cts 34
cp 0.9412
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTag() 0 4 1
A setTag() 0 4 1
A onTag() 0 7 1
A removeListenersWithTag() 0 13 3
A getSortedListeners() 0 9 3
A ensureListener() 0 11 2
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
     * {@inheritdoc}
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
     * {@inheritdoc}
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