Passed
Pull Request — master (#39)
by Alexander
02:28
created

DeferredDispatcher::flush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Yiisoft\EventDispatcher\Dispatcher;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
7
final class DeferredDispatcher implements EventDispatcherInterface
8
{
9
    private EventDispatcherInterface $dispatcher;
10
    private array $events = [];
11
12 2
    public function __construct(EventDispatcherInterface $dispatcher)
13
    {
14 2
        $this->dispatcher = $dispatcher;
15 2
    }
16
17 2
    public function dispatch(object $event)
18
    {
19 2
        $this->events[] = $event;
20 2
        return $event;
21
    }
22
23 1
    public function flush(): array
24
    {
25 1
        $dispatchedEvents = [];
26 1
        foreach ($this->events as $event) {
27 1
            $dispatchedEvents[] = $this->dispatcher->dispatch($event);
28
        }
29
30 1
        $this->events = [];
31 1
        return $dispatchedEvents;
32
    }
33
}
34