Completed
Push — master ( 7b43ed...6cb3bb )
by Frank
01:43
created

EventDispatcher::dispatchUnstoppableEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
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): object
24
    {
25
        $listeners = $this->listenerProvider->getListenersForEvent($event);
26
27
        $event instanceof StoppableEventInterface
28
            ? $this->dispatchStoppableEvent($listeners, $event)
29
            : $this->dispatchUnstoppableEvent($listeners, $event);
30
31
        return $event;
32
    }
33
34
    public function dispatchGeneratedEvents(EventGenerator $generator): void
35
    {
36
        foreach ($generator->releaseEvents() as $event) {
37
            $this->dispatch($event);
38
        }
39
    }
40
41
    private function dispatchStoppableEvent(iterable $listeners, StoppableEventInterface $event): void
42
    {
43
        foreach ($listeners as $listener) {
44
            if ($event->isPropagationStopped()) {
45
                break;
46
            }
47
48
            $listener($event);
49
        }
50
    }
51
52
    private function dispatchUnstoppableEvent(iterable $listeners, object $event): void
53
    {
54
        foreach ($listeners as $listener) {
55
            $listener($event);
56
        }
57
    }
58
59 View Code Duplication
    public function subscribeTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void
0 ignored issues
show
Duplication introduced by
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...
60
    {
61
        if ( ! $this->listenerProvider instanceof ListenerAcceptor) {
62
            throw UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($this->listenerProvider);
63
        }
64
65
        $this->listenerProvider->subscribeTo($event, $listener, $priority);
66
    }
67
68 View Code Duplication
    public function subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void
0 ignored issues
show
Duplication introduced by
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...
69
    {
70
        if ( ! $this->listenerProvider instanceof ListenerAcceptor) {
71
            throw UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($this->listenerProvider);
72
        }
73
74
        $this->listenerProvider->subscribeOnceTo($event, $listener, $priority);
75
    }
76
77
    public function subscribeListenersFrom(ListenerSubscriber $subscriber): void
78
    {
79
        if ( ! $this->listenerProvider instanceof ListenerAcceptor) {
80
            throw UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($this->listenerProvider);
81
        }
82
83
        $this->listenerProvider->subscribeListenersFrom($subscriber);
84
    }
85
}
86