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

DeferredDispatcher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 2
b 0
f 0
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 4 1
A flush() 0 9 2
A __construct() 0 3 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