Completed
Push — master ( 82b030...f3e878 )
by Frank
01:03
created

EventDispatcher   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 75
Duplicated Lines 21.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A dispatch() 0 8 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): 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
Duplication introduced by Frank de Jonge
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...
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
Duplication introduced by Frank de Jonge
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