ReflectorEventRegistry::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gember\EventSourcing\Registry\Event\Reflector;
6
7
use Gember\EventSourcing\Registry\Event\EventNotRegisteredException;
8
use Gember\EventSourcing\Registry\Event\EventRegistry;
9
use Gember\EventSourcing\Resolver\DomainEvent\DomainEventResolver;
10
use Gember\EventSourcing\Util\File\Finder\Finder;
11
use Gember\EventSourcing\Util\File\Reflector\ReflectionFailedException;
12
use Gember\EventSourcing\Util\File\Reflector\Reflector;
13
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...
14
15
final class ReflectorEventRegistry implements EventRegistry
16
{
17
    /**
18
     * @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...
19
     */
20
    private array $events = [];
21
22 4
    public function __construct(
23
        private readonly Finder $finder,
24
        private readonly Reflector $reflector,
25
        private readonly DomainEventResolver $domainEventResolver,
26
        private readonly string $path,
27 4
    ) {}
28
29
    /**
30
     * @throws EventNotRegisteredException
31
     * @throws ReflectionFailedException
32
     */
33 3
    #[Override]
34
    public function retrieve(string $eventName): string
35
    {
36 3
        $this->initialize();
37
38 3
        if (!array_key_exists($eventName, $this->events)) {
39 1
            throw EventNotRegisteredException::withEventName($eventName);
40
        }
41
42 2
        return $this->events[$eventName];
43
    }
44
45
    /**
46
     * @throws ReflectionFailedException
47
     */
48 3
    private function initialize(): void
49
    {
50 3
        if ($this->events !== []) {
51
            return;
52
        }
53
54 3
        $files = $this->finder->getFiles($this->path);
55
56 3
        foreach ($files as $file) {
57 2
            if ($file === '') {
58 2
                continue;
59
            }
60
61 2
            $reflectionClass = $this->reflector->reflectClassFromFile($file);
62
63 2
            $eventName = $this->domainEventResolver->resolve($reflectionClass->getName())->eventName;
64
65 2
            $this->events[$eventName] = $reflectionClass->getName();
66
        }
67
    }
68
}
69