CompositeDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\EventDispatcher\Dispatcher;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\EventDispatcher\StoppableEventInterface;
9
10
/**
11
 * Delegates dispatching an event to one or more dispatchers.
12
 */
13
final class CompositeDispatcher implements EventDispatcherInterface
14
{
15
    /**
16
     * @var EventDispatcherInterface[]
17
     */
18
    private array $dispatchers = [];
19
20 2
    public function dispatch(object $event)
21
    {
22 2
        foreach ($this->dispatchers as $dispatcher) {
23 2
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
24 1
                return $event;
25
            }
26 2
            $event = $dispatcher->dispatch($event);
27
        }
28
29 1
        return $event;
30
    }
31
32 2
    public function attach(EventDispatcherInterface $dispatcher): void
33
    {
34 2
        $this->dispatchers[] = $dispatcher;
35
    }
36
}
37