Completed
Push — master ( 36e003...6d6d88 )
by Frank
01:09
created

src/BufferedEventDispatcher.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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, ListenerRegistry
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): object
27
    {
28
        $this->recordEvent($event);
29
30
        return $event;
31
    }
32
33
    /**
34
     * @return object[]
35
     */
36
    public function dispatchBufferedEvents(): array
37
    {
38
        $events = [];
39
40
        foreach ($this->releaseEvents() as $event) {
41
            $events[] = $this->dispatcher->dispatch($event);
42
        }
43
44
        return $events;
45
    }
46
47 View Code Duplication
    public function subscribeTo(string $event, callable $listener, int $priority = ListenerPriority::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...
48
    {
49
        if ( ! $this->dispatcher instanceof ListenerRegistry) {
50
            throw UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($this->dispatcher);
51
        }
52
53
        $this->dispatcher->subscribeTo($event, $listener, $priority);
54
    }
55
56 View Code Duplication
    public function subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::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...
57
    {
58
        if ( ! $this->dispatcher instanceof ListenerRegistry) {
59
            throw UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($this->dispatcher);
60
        }
61
62
        $this->dispatcher->subscribeOnceTo($event, $listener, $priority);
63
    }
64
65
    public function subscribeListenersFrom(ListenerSubscriber $subscriber): void
66
    {
67
        if ( ! $this->dispatcher instanceof ListenerRegistry) {
68
            throw UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($this->dispatcher);
69
        }
70
71
        $this->dispatcher->subscribeListenersFrom($subscriber);
72
    }
73
}
74