Passed
Push — main ( 0e9e8b...8256cc )
by Jeroen
02:08
created

ReflectorCommandHandlerRegistry::initialize()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 0
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gember\EventSourcing\Registry\CommandHandler\Reflector;
6
7
use Gember\EventSourcing\Registry\CommandHandler\CommandHandlerNotRegisteredException;
8
use Gember\EventSourcing\Registry\CommandHandler\CommandHandlerRegistry;
9
use Gember\EventSourcing\Resolver\UseCase\CommandHandlerDefinition;
0 ignored issues
show
Bug introduced by
The type Gember\EventSourcing\Res...ommandHandlerDefinition 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...
10
use Gember\EventSourcing\Resolver\UseCase\UseCaseResolver;
11
use Gember\EventSourcing\UseCase\EventSourcedUseCase;
12
use Gember\EventSourcing\Util\File\Finder\Finder;
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 ReflectorCommandHandlerRegistry implements CommandHandlerRegistry
17
{
18
    /**
19
     * @var array<class-string, array{class-string<EventSourcedUseCase>, CommandHandlerDefinition}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, arra...mandHandlerDefinition}> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, array{class-string<EventSourcedUseCase>, CommandHandlerDefinition}>.
Loading history...
20
     */
21
    private array $definitions = [];
22
23 7
    public function __construct(
24
        private readonly Finder $finder,
25
        private readonly Reflector $reflector,
26
        private readonly UseCaseResolver $useCaseResolver,
27
        private readonly string $path,
28 7
    ) {}
29
30 6
    #[Override]
31
    public function retrieve(string $commandName): array
32
    {
33 6
        $this->initialize();
34
35 6
        if (!array_key_exists($commandName, $this->definitions)) {
36 1
            throw CommandHandlerNotRegisteredException::withCommandName($commandName);
37
        }
38
39 5
        return $this->definitions[$commandName];
40
    }
41
42 6
    private function initialize(): void
43
    {
44 6
        if ($this->definitions !== []) {
45
            return;
46
        }
47
48 6
        $files = $this->finder->getFiles($this->path);
49
50 6
        foreach ($files as $file) {
51 5
            if ($file === '') {
52 2
                continue;
53
            }
54
55 5
            $reflectionClass = $this->reflector->reflectClassFromFile($file);
56
57
            /** @var class-string<EventSourcedUseCase> $useCaseClassName */
58 5
            $useCaseClassName = $reflectionClass->getName();
59
60 5
            $useCaseDefinition = $this->useCaseResolver->resolve($useCaseClassName);
61
62 5
            foreach ($useCaseDefinition->commandHandlers as $commandHandlerDefinition) {
63 5
                $this->definitions[$commandHandlerDefinition->commandClassName] = [
64 5
                    $useCaseClassName,
65 5
                    $commandHandlerDefinition,
66 5
                ];
67
            }
68
        }
69
    }
70
}
71