Passed
Push — main ( e2fa46...fe29e0 )
by Jeroen
02:39 queued 37s
created

ReflectorSagaRegistry::initialize()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 0
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
crap 6.105
rs 9.2222
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\SagaRegistry;
8
use Gember\EventSourcing\Resolver\Saga\Default\EventSubscriber\SagaEventSubscriberResolver;
9
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...
10
use Gember\EventSourcing\Resolver\Saga\SagaResolver;
11
use Gember\EventSourcing\Util\File\Finder\Finder;
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 ReflectorSagaRegistry implements SagaRegistry
16
{
17
    /**
18
     * @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...
19
     */
20
    private array $definitions = [];
21
22 6
    public function __construct(
23
        private readonly Finder $finder,
24
        private readonly Reflector $reflector,
25
        private readonly SagaResolver $sagaResolver,
26
        private readonly SagaEventSubscriberResolver $sagaEventSubscriberResolver,
27
        private readonly string $path,
28 6
    ) {}
29
30 5
    #[Override]
31
    public function retrieve(string $sagaIdName): array
32
    {
33 5
        $this->initialize();
34
35 5
        if (!array_key_exists($sagaIdName, $this->definitions)) {
36 1
            return [];
37
        }
38
39 4
        return $this->definitions[$sagaIdName];
40
    }
41
42 5
    private function initialize(): void
43
    {
44 5
        if ($this->definitions !== []) {
45
            return;
46
        }
47
48 5
        $files = $this->finder->getFiles($this->path);
49
50 5
        foreach ($files as $file) {
51 5
            if ($file === '') {
52
                continue;
53
            }
54
55 5
            $reflectionClass = $this->reflector->reflectClassFromFile($file);
56
57
            // A saga must have subscribers
58 5
            $subscribers = $this->sagaEventSubscriberResolver->resolve($reflectionClass->getName());
59
60 5
            if ($subscribers === []) {
61 5
                continue;
62
            }
63
64 5
            $definition = $this->sagaResolver->resolve($reflectionClass->getName());
65
66 5
            foreach ($definition->sagaIds as $sagaIdDefinition) {
67 5
                $this->definitions[$sagaIdDefinition->sagaIdName][] = $definition;
68
            }
69
        }
70
    }
71
}
72