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

EventDispatcher   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 77
Duplicated Lines 20.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 16
loc 77
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A dispatch() 0 10 2
A dispatchGeneratedEvents() 0 6 2
A dispatchStoppableEvent() 0 10 3
A dispatchUnstoppableEvent() 0 6 2
A subscribeTo() 8 8 2
A subscribeOnceTo() 8 8 2
A subscribeListenersFrom() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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