BufferedEventDispatcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatchBufferedEvents() 0 12 2
A __construct() 0 6 1
A dispatch() 0 13 5
1
<?php
2
3
/*
4
 * (c) Olivier Laviale <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace olvlvl\EventDispatcher;
11
12
use Psr\EventDispatcher\EventDispatcherInterface;
13
use Psr\EventDispatcher\StoppableEventInterface;
14
15
/**
16
 * Decorates an Event Dispatcher to buffer events.
17
 *
18
 * Careful using this type of Dispatcher! Because the dispatching is delayed, it will cause issues for users that
19
 * expect Events to be modified.
20
 */
21
final class BufferedEventDispatcher implements BufferedEventDispatcherInterface
22
{
23
    /**
24
     * @var EventDispatcherInterface
25
     */
26
    private $decorated;
27
28
    /**
29
     * @var callable|null
30
     * @phpstan-var callable(object):bool|null
31
     */
32
    private $discriminator;
33
34
    /**
35
     * @var object[]
36
     */
37
    private $buffer = [];
38
39
    /**
40
     * @param EventDispatcherInterface $decorated
41
     * @param callable|null $discriminator Return `true` if the event should be buffered, `false` otherwise.
42
     *
43
     * @phpstan-param callable(object):bool|null $discriminator
44
     */
45
    public function __construct(
46
        EventDispatcherInterface $decorated,
47
        callable $discriminator = null
48
    ) {
49
        $this->decorated = $decorated;
50
        $this->discriminator = $discriminator;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function dispatch(object $event): object
57
    {
58
        if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
59
            return $event;
60
        }
61
62
        if ($this->discriminator && !($this->discriminator)($event)) {
63
            return $this->decorated->dispatch($event);
64
        }
65
66
        $this->buffer[] = $event;
67
68
        return $event;
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74
    public function dispatchBufferedEvents(): array
75
    {
76
        $buffer = $this->buffer;
77
        $this->buffer = [];
78
79
        foreach ($buffer as $event) {
80
            $this->decorated->dispatch($event);
81
        }
82
83
        // Since a Dispatcher MUST return the same Event object it was passed after it is done invoking Listeners,
84
        // the buffer can be returned as is.
85
        return $buffer;
86
    }
87
}
88