ReflectorEventRegistry   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 53
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 10 2
A __construct() 0 6 1
A initialize() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gember\EventSourcing\Registry\Reflector;
6
7
use Gember\EventSourcing\Registry\EventNotRegisteredException;
8
use Gember\EventSourcing\Registry\EventRegistry;
9
use Gember\EventSourcing\Resolver\DomainEvent\NormalizedEventName\NormalizedEventNameResolver;
10
use Gember\EventSourcing\Resolver\DomainEvent\NormalizedEventName\UnresolvableEventNameException;
11
use Gember\EventSourcing\Util\File\Finder\Finder;
12
use Gember\EventSourcing\Util\File\Reflector\ReflectionFailedException;
13
use Gember\EventSourcing\Util\File\Reflector\Reflector;
14
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
final class ReflectorEventRegistry implements EventRegistry
17
{
18
    /**
19
     * @var array<string, class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
20
     */
21
    private array $events = [];
22
23 4
    public function __construct(
24
        private readonly Finder $finder,
25
        private readonly Reflector $reflector,
26
        private readonly NormalizedEventNameResolver $eventNameResolver,
27
        private readonly string $path,
28 4
    ) {}
29
30
    /**
31
     * @throws EventNotRegisteredException
32
     * @throws ReflectionFailedException
33
     * @throws UnresolvableEventNameException
34
     */
35 3
    #[Override]
36
    public function retrieve(string $eventName): string
37
    {
38 3
        $this->initialize();
39
40 3
        if (!array_key_exists($eventName, $this->events)) {
41 1
            throw EventNotRegisteredException::withEventName($eventName);
42
        }
43
44 2
        return $this->events[$eventName];
45
    }
46
47
    /**
48
     * @throws ReflectionFailedException
49
     * @throws UnresolvableEventNameException
50
     */
51 3
    private function initialize(): void
52
    {
53 3
        if ($this->events !== []) {
54
            return;
55
        }
56
57 3
        $files = $this->finder->getFiles($this->path);
58
59 3
        foreach ($files as $file) {
60 2
            if ($file === '') {
61 2
                continue;
62
            }
63
64 2
            $reflectionClass = $this->reflector->reflectClassFromFile($file);
65
66 2
            $eventName = $this->eventNameResolver->resolve($reflectionClass->getName());
67
68 2
            $this->events[$eventName] = $reflectionClass->getName();
69
        }
70
    }
71
}
72