Completed
Push — master ( ae6a92...7b43ed )
by Frank
01:13
created

BufferedEventDispatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 16
loc 56
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dispatch() 0 4 1
A dispatchBufferedEvents() 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
declare(strict_types=1);
4
5
namespace League\Event;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
9
class BufferedEventDispatcher implements EventDispatcherInterface, ListenerAcceptor
10
{
11
    use EventGeneratorBehavior {
12
        recordEvent as protected;
13
        releaseEvents as protected;
14
    }
15
16
    /**
17
     * @var EventDispatcherInterface
18
     */
19
    private $dispatcher;
20
21
    public function __construct(EventDispatcherInterface $dispatcher)
22
    {
23
        $this->dispatcher = $dispatcher;
24
    }
25
26
    public function dispatch(object $event): void
27
    {
28
        $this->recordEvent($event);
29
    }
30
31
    public function dispatchBufferedEvents(): void
32
    {
33
        foreach ($this->releaseEvents() as $event) {
34
            $this->dispatcher->dispatch($event);
35
        }
36
    }
37
38 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...
39
    {
40
        if ( ! $this->dispatcher instanceof ListenerAcceptor) {
41
            throw UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($this->dispatcher);
42
        }
43
44
        $this->dispatcher->subscribeTo($event, $listener, $priority);
45
    }
46
47 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...
48
    {
49
        if ( ! $this->dispatcher instanceof ListenerAcceptor) {
50
            throw UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($this->dispatcher);
51
        }
52
53
        $this->dispatcher->subscribeOnceTo($event, $listener, $priority);
54
    }
55
56
    public function subscribeListenersFrom(ListenerSubscriber $subscriber): void
57
    {
58
        if ( ! $this->dispatcher instanceof ListenerAcceptor) {
59
            throw UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($this->dispatcher);
60
        }
61
62
        $this->dispatcher->subscribeListenersFrom($subscriber);
63
    }
64
}
65