Notifier::call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPExtra\EventManager;
6
7
/**
8
 * @internal This class is for internal use only. You should not depend on it in your code.
9
 */
10
final class Notifier
11
{
12
    public function notify(Listener $listener, Event $event): void
13
    {
14
        $eventClass = get_class($event);
15
        $reflection = new \ReflectionObject($listener);
16
17
        foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
18
            if ($this->supports($method, $eventClass)) {
19
                $this->call($listener, $method->getName(), $event);
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
20
            }
21
        }
22
    }
23
24
    private function supports(\ReflectionMethod $method, string $eventClass): bool
25
    {
26
        if ($method->getNumberOfParameters() !== 1) {
27
            return false;
28
        }
29
30
        $paramClass = $method->getParameters()[0]->getClass();
31
32
        if ($paramClass === null) {
33
            return false;
34
        }
35
36
        if (!is_a($eventClass, $paramClass->getName(), true)) {
37
            return false;
38
        }
39
40
        return true;
41
    }
42
43
    private function call(Listener $listener, string $method, Event $event): void
44
    {
45
        $listener->{$method}($event);
46
    }
47
}
48