CompositeDispatcher::attach()   A
last analyzed

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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