Passed
Pull Request — master (#23)
by Sergei
08:07
created

SimpleEventDispatcher::hasEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Test\Support\EventDispatcher;
6
7
use Closure;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Psr\EventDispatcher\StoppableEventInterface;
10
11
use function get_class;
12
13
final class SimpleEventDispatcher implements EventDispatcherInterface
14
{
15
    /** @var array<int, Closure> */
16
    private array $listeners;
17
18
    /** @var object[] */
19
    private array $events = [];
20
21
    /**
22
     * @param Closure ...$listeners Functions that will handle each event.
23
     */
24 17
    public function __construct(Closure ...$listeners)
25
    {
26 17
        $this->listeners = $listeners;
27 17
    }
28
29 15
    public function dispatch(object $event): object
30
    {
31 15
        $this->events[] = $event;
32 15
        foreach ($this->listeners as $listener) {
33 4
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
34 2
                return $event;
35
            }
36 3
            $listener($event);
37
        }
38 12
        return $event;
39
    }
40
41 3
    public function getEvents(): array
42
    {
43 3
        return $this->events;
44
    }
45
46 1
    public function clearEvents(): void
47
    {
48 1
        $this->events = [];
49 1
    }
50
51 3
    public function isObjectTriggered(object $event, int $times = null): bool
52
    {
53 3
        return $this->processBoolResult(static fn (object $e): bool => $e === $event, $times);
54
    }
55
56 4
    public function isClassTriggered(string $class, int $times = null): bool
57
    {
58 4
        return $this->processBoolResult(static fn (object $event): bool => get_class($event) === $class, $times);
59
    }
60
61 3
    public function isInstanceOfTriggered(string $class, int $times = null): bool
62
    {
63 3
        return $this->processBoolResult(static fn (object $event): bool => $event instanceof $class, $times);
64
    }
65
66 10
    private function processBoolResult(Closure $closure, ?int $times): bool
67
    {
68 10
        if ($times < 0) {
69 1
            throw new \InvalidArgumentException('The $times argument cannot be less than zero.');
70
        }
71 9
        if ($times === null) {
72 3
            return $this->hasEvent($closure);
73
        }
74 6
        return $times === $this->calcEvent($closure);
75
    }
76
77 3
    private function hasEvent(Closure $closure): bool
78
    {
79 3
        foreach ($this->events as $event) {
80 3
            if ($closure($event)) {
81 3
                return true;
82
            }
83
        }
84 3
        return false;
85
    }
86
87 6
    private function calcEvent(Closure $closure): int
88
    {
89 6
        $count = 0;
90 6
        foreach ($this->events as $event) {
91 6
            if ($closure($event)) {
92 6
                ++$count;
93
            }
94
        }
95 6
        return $count;
96
    }
97
}
98