EventBus::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
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
namespace Cubiche\Core\EventBus\Event;
13
14
use Cubiche\Core\Bus\Bus;
15
use Cubiche\Core\Bus\Exception\NotFoundException;
16
use Cubiche\Core\Bus\MessageInterface;
17
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware;
18
use Cubiche\Core\EventBus\Middlewares\EventDispatcher\EventDispatcherMiddleware;
19
use Cubiche\Core\EventDispatcher\EventDispatcher;
20
21
/**
22
 * EventBus class.
23
 *
24
 * @author Ivannis Suárez Jerez <[email protected]>
25
 */
26
class EventBus extends Bus
27
{
28
    /**
29
     * @var EventDispatcherMiddleware
30
     */
31
    protected $dispatcherMiddleware;
32
33
    /**
34
     * @return EventBus
35
     */
36
    public static function create()
37
    {
38
        return new static([
39
            250 => new LockingMiddleware(),
40
            100 => new EventDispatcherMiddleware(new EventDispatcher()),
41
        ]);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function dispatch(MessageInterface $event)
48
    {
49 View Code Duplication
        if (!$event instanceof EventInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
            throw new \InvalidArgumentException(
51
                sprintf(
52
                    'The object must be an instance of %s. Instance of %s given',
53
                    EventInterface::class,
54
                    get_class($event)
55
                )
56
            );
57
        }
58
59
        $this->ensureEventDispatcherMiddleware();
60
61
        parent::dispatch($event);
62
    }
63
64
    /**
65
     * Ensure that exists an dispatcher middleware.
66
     *
67
     * @throws InvalidArgumentException
68
     */
69
    protected function ensureEventDispatcherMiddleware()
70
    {
71
        if ($this->dispatcherMiddleware !== null) {
72
            return;
73
        }
74
75
        foreach ($this->middlewares as $priority => $collection) {
76
            foreach ($collection as $middleware) {
77
                if ($middleware instanceof EventDispatcherMiddleware) {
78
                    $this->dispatcherMiddleware = $middleware;
79
80
                    return;
81
                }
82
            }
83
        }
84
85
        throw NotFoundException::middlewareOfType(EventDispatcherMiddleware::class);
86
    }
87
88
    /**
89
     * @return EventDispatcherMiddleware
90
     */
91
    public function dispatcherMiddleware()
92
    {
93
        $this->ensureEventDispatcherMiddleware();
94
95
        return $this->dispatcherMiddleware;
96
    }
97
98
    /**
99
     * Adds an event listener that listens on the specified events. The higher priority value, the earlier an event
100
     * listener will be triggered in the chain (defaults to 0).
101
     *
102
     * @param string   $eventName
103
     * @param callable $listener
104
     * @param int      $priority
105
     *
106
     * @return $this
107
     */
108
    public function addListener($eventName, callable $listener, $priority = 0)
109
    {
110
        $this->ensureEventDispatcherMiddleware();
111
112
        $this->dispatcherMiddleware->dispatcher()->addListener($eventName, $listener, $priority);
113
    }
114
115
    /**
116
     * Removes an event listener from the specified events.
117
     *
118
     * @param string   $eventName
119
     * @param callable $listener
120
     *
121
     * @return $this
122
     */
123
    public function removeListener($eventName, callable $listener)
124
    {
125
        $this->ensureEventDispatcherMiddleware();
126
127
        $this->dispatcherMiddleware->dispatcher()->removeListener($eventName, $listener);
128
    }
129
130
    /**
131
     * Adds an event subscriber. The subscriber is asked for all the events he is
132
     * interested in and added as a listener for these events.
133
     *
134
     * @param EventSubscriberInterface $subscriber
135
     *
136
     * @return $this
137
     */
138
    public function addSubscriber(EventSubscriberInterface $subscriber)
139
    {
140
        $this->ensureEventDispatcherMiddleware();
141
142
        $this->dispatcherMiddleware->dispatcher()->addSubscriber($subscriber);
143
    }
144
145
    /**
146
     * Removes an event subscriber.
147
     *
148
     * @param EventSubscriberInterface $subscriber
149
     *
150
     * @return $this
151
     */
152
    public function removeSubscriber(EventSubscriberInterface $subscriber)
153
    {
154
        $this->ensureEventDispatcherMiddleware();
155
156
        $this->dispatcherMiddleware->dispatcher()->removeSubscriber($subscriber);
157
    }
158
159
    /**
160
     * Gets the list of event listeners.
161
     *
162
     * @return array
163
     */
164
    public function listeners()
165
    {
166
        $this->ensureEventDispatcherMiddleware();
167
168
        return $this->dispatcherMiddleware->dispatcher()->listeners();
169
    }
170
171
    /**
172
     * Checks whether an event has any registered listeners.
173
     *
174
     * @param string $eventName
175
     *
176
     * @return bool
177
     */
178
    public function hasEventListeners($eventName)
179
    {
180
        $this->ensureEventDispatcherMiddleware();
181
182
        return $this->dispatcherMiddleware->dispatcher()->hasEventListeners($eventName);
183
    }
184
185
    /**
186
     * Checks whether has any registered listener.
187
     *
188
     * @return bool
189
     */
190
    public function hasListeners()
191
    {
192
        $this->ensureEventDispatcherMiddleware();
193
194
        return $this->dispatcherMiddleware->dispatcher()->hasListeners();
195
    }
196
}
197