Passed
Push — main ( 5a9217...d80414 )
by Jeroen
02:29
created

ReflectorCommandHandlerRegistry   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 46
ccs 17
cts 18
cp 0.9444
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 20 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gember\EventSourcing\Registry\CommandHandler\Reflector;
6
7
use Gember\EventSourcing\Resolver\UseCase\CommandHandlers\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...
8
use Gember\EventSourcing\Registry\CommandHandler\CommandHandlerNotRegisteredException;
9
use Gember\EventSourcing\Registry\CommandHandler\CommandHandlerRegistry;
10
use Gember\EventSourcing\Resolver\UseCase\CommandHandlers\CommandHandlersResolver;
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, CommandHandlerDefinition>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string, CommandHandlerDefinition> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, 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 CommandHandlersResolver $commandHandlersResolver,
27
        private readonly string $path,
28 7
    ) {}
29
30 6
    #[Override]
31
    public function retrieve(string $commandName): CommandHandlerDefinition
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
            foreach ($this->commandHandlersResolver->resolve($useCaseClassName) as $definition) {
61 5
                $this->definitions[$definition->commandName] = $definition;
62
            }
63
        }
64
    }
65
}
66