EventRouter::getLog()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Smoren\EventRouter\Components;
4
5
use Smoren\EventRouter\Interfaces\EventConfigInterface;
6
use Smoren\EventRouter\Interfaces\EventInterface;
7
use Smoren\EventRouter\Interfaces\EventRouterInterface;
8
use Smoren\EventRouter\Interfaces\EventRouteRuleInterface;
9
use Smoren\EventRouter\Interfaces\LoggerInterface;
10
use Smoren\EventRouter\Exceptions\EventRouterException;
11
12
/**
13
 * Event router
14
 */
15
class EventRouter implements EventRouterInterface
16
{
17
    /**
18
     * @var EventRouterMap map for storing event route rules
19
     */
20
    protected EventRouterMap $map;
21
    /**
22
     * @var int|null max recursion level of handling events
23
     */
24
    protected ?int $maxDepthLevelCount;
25
    /**
26
     * @var LoggerInterface logger
27
     */
28
    protected LoggerInterface $logger;
29
30
    /**
31
     * EventRouter constructor
32
     * @param int|null $maxDepthLevelCount
33
     * @param LoggerInterface $logger
34
     */
35 11
    public function __construct(?int $maxDepthLevelCount, LoggerInterface $logger)
36
    {
37 11
        $this->maxDepthLevelCount = $maxDepthLevelCount;
38 11
        $this->map = new EventRouterMap();
39 11
        $this->logger = $logger;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 11
    public function on(EventConfigInterface $config, callable $handler): self
46
    {
47 11
        $this->map->add($config, $handler);
48 11
        return $this;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 2
    public function register(EventRouteRuleInterface $routeRule): self
55
    {
56 2
        return $this->on($routeRule->getConfig(), $routeRule->getHandler());
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 11
    public function send(EventInterface $event): self
63
    {
64 11
        $this->_handle($event);
65 9
        return $this;
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71 3
    public function getLog(): array
72
    {
73 3
        return $this->logger->getLog();
74
    }
75
76
    /**
77
     * Internal method for recursive event handling
78
     * @param EventInterface $event event to handle
79
     * @param int $depthLevelCount current recursion level
80
     * @return void
81
     * @throws EventRouterException
82
     */
83 11
    protected function _handle(EventInterface $event, int $depthLevelCount = 0)
84
    {
85 11
        if($this->maxDepthLevelCount !== null && $depthLevelCount >= $this->maxDepthLevelCount) {
86 2
            throw new EventRouterException(
87 2
                'max depth level reached',
88 2
                EventRouterException::MAX_DEPTH_LEVEL_REACHED,
89 2
                null,
90 2
                ['max_depth_level_count' => $this->maxDepthLevelCount]
91 2
            );
92
        }
93
94 11
        foreach($this->map->get($event) as $handler) {
95 11
            $result = $handler($event);
96 11
            $this->logger->append($event);
97
98 11
            if($result instanceof EventInterface) {
99 3
                $this->_handle($result, ++$depthLevelCount);
100 11
            } elseif(is_array($result)) {
101 5
                foreach($result as $item) {
102 3
                    if($item instanceof EventInterface) {
103 3
                        $this->_handle($item, ++$depthLevelCount);
104
                    }
105
                }
106
            }
107
        }
108
    }
109
}
110