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

DeferredDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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