Passed
Push — main ( 623de2...d133ef )
by Jeroen
25:04 queued 22:36
created

ReflectorSagaRegistry::retrieve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gember\EventSourcing\Registry\Saga\Reflector;
6
7
use Gember\EventSourcing\Registry\Saga\SagaNotRegisteredException;
8
use Gember\EventSourcing\Registry\Saga\SagaRegistry;
9
use Gember\EventSourcing\Resolver\Saga\Default\EventSubscriber\SagaEventSubscriberResolver;
10
use Gember\EventSourcing\Resolver\Saga\SagaDefinition;
0 ignored issues
show
Bug introduced by
The type Gember\EventSourcing\Resolver\Saga\SagaDefinition 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...
11
use Gember\EventSourcing\Resolver\Saga\SagaResolver;
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 ReflectorSagaRegistry implements SagaRegistry
17
{
18
    /**
19
     * @var array<string, list<SagaDefinition>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, list<SagaDefinition>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
20
     */
21
    private array $definitions = [];
22
23 6
    public function __construct(
24
        private readonly Finder $finder,
25
        private readonly Reflector $reflector,
26
        private readonly SagaResolver $sagaResolver,
27
        private readonly SagaEventSubscriberResolver $sagaEventSubscriberResolver,
28
        private readonly string $path,
29 6
    ) {}
30
31 5
    #[Override]
32
    public function retrieve(string $sagaIdName): array
33
    {
34 5
        $this->initialize();
35
36 5
        if (!array_key_exists($sagaIdName, $this->definitions)) {
37 1
            throw SagaNotRegisteredException::withSagaIdName($sagaIdName);
38
        }
39
40 4
        return $this->definitions[$sagaIdName];
41
    }
42
43 5
    private function initialize(): void
44
    {
45 5
        if ($this->definitions !== []) {
46
            return;
47
        }
48
49 5
        $files = $this->finder->getFiles($this->path);
50
51 5
        foreach ($files as $file) {
52 5
            if ($file === '') {
53
                continue;
54
            }
55
56 5
            $reflectionClass = $this->reflector->reflectClassFromFile($file);
57
58
            // A saga must have subscribers
59 5
            $subscribers = $this->sagaEventSubscriberResolver->resolve($reflectionClass->getName());
60
61 5
            if ($subscribers === []) {
62 5
                continue;
63
            }
64
65 5
            $definition = $this->sagaResolver->resolve($reflectionClass->getName());
66
67 5
            $this->definitions[$definition->sagaId->sagaIdName][] = $definition;
68
        }
69
    }
70
}
71