These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace League\Event; |
||
4 | |||
5 | use Psr\EventDispatcher\EventDispatcherInterface; |
||
6 | use Psr\EventDispatcher\ListenerProviderInterface; |
||
7 | use Psr\EventDispatcher\StoppableEventInterface; |
||
8 | |||
9 | class EventDispatcher implements EventDispatcherInterface, ListenerAcceptor |
||
10 | { |
||
11 | /** |
||
12 | * @var ListenerProviderInterface |
||
13 | */ |
||
14 | protected $listenerProvider; |
||
15 | |||
16 | public function __construct(ListenerProviderInterface $listenerProvider = null) |
||
17 | { |
||
18 | $this->listenerProvider = $listenerProvider instanceof ListenerProviderInterface |
||
19 | ? $listenerProvider |
||
20 | : new PrioritizedListenerCollection(); |
||
21 | } |
||
22 | |||
23 | public function dispatch(object $event): void |
||
24 | { |
||
25 | $listeners = $this->listenerProvider->getListenersForEvent($event); |
||
26 | |||
27 | $event instanceof StoppableEventInterface |
||
28 | ? $this->dispatchStoppableEvent($listeners, $event) |
||
29 | : $this->dispatchUnstoppableEvent($listeners, $event); |
||
30 | } |
||
31 | |||
32 | public function dispatchGeneratedEvents(EventGenerator $generator): void |
||
33 | { |
||
34 | foreach ($generator->releaseEvents() as $event) { |
||
35 | $this->dispatch($event); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | private function dispatchStoppableEvent(iterable $listeners, StoppableEventInterface $event): void |
||
40 | { |
||
41 | foreach ($listeners as $listener) { |
||
42 | if ($event->isPropagationStopped()) { |
||
43 | break; |
||
44 | } |
||
45 | |||
46 | $listener($event); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | private function dispatchUnstoppableEvent(iterable $listeners, $event): void |
||
51 | { |
||
52 | foreach ($listeners as $listener) { |
||
53 | $listener($event); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | View Code Duplication | public function subscribeTo(string $event, callable $listener, int $priority = self::P_NORMAL): void |
|
0 ignored issues
–
show
|
|||
58 | { |
||
59 | if ( ! $this->listenerProvider instanceof ListenerAcceptor) { |
||
60 | throw UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($this->listenerProvider); |
||
61 | } |
||
62 | |||
63 | $this->listenerProvider->subscribeTo($event, $listener, $priority); |
||
64 | } |
||
65 | |||
66 | View Code Duplication | public function subscribeOnceTo(string $event, callable $listener, int $priority = self::P_NORMAL): void |
|
0 ignored issues
–
show
This method seems to be duplicated in 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...
|
|||
67 | { |
||
68 | if ( ! $this->listenerProvider instanceof ListenerAcceptor) { |
||
69 | throw UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($this->listenerProvider); |
||
70 | } |
||
71 | |||
72 | $this->listenerProvider->subscribeOnceTo($event, $listener, $priority); |
||
73 | } |
||
74 | |||
75 | public function subscribeListenersFrom(ListenerSubscriber $subscriber): void |
||
76 | { |
||
77 | if ( ! $this->listenerProvider instanceof ListenerAcceptor) { |
||
78 | throw UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($this->listenerProvider); |
||
79 | } |
||
80 | |||
81 | $this->listenerProvider->subscribeListenersFrom($subscriber); |
||
82 | } |
||
83 | } |
||
84 |
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.