Passed
Push — master ( 477224...3259b8 )
by Aleksei
06:12 queued 18s
created

AttributeResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Boot\BootloadManager;
6
7
use Spiral\Boot\Attribute\AbstractMethod;
8
use Spiral\Boot\Attribute\BindMethod;
9
use Spiral\Boot\Attribute\InjectorMethod;
10
use Spiral\Boot\Attribute\SingletonMethod;
11
use Spiral\Boot\Bootloader\BootloaderInterface;
12
use Spiral\Core\Attribute\Singleton;
13
use Spiral\Core\Container;
14
15
/**
16
 * @internal
17
 * @template TAttribute of AbstractMethod
18
 * @template TBootloader of BootloaderInterface
19
 * @implements AttributeResolverInterface<TAttribute, TBootloader>
20
 * @implements AttributeResolverRegistryInterface<TAttribute>
21
 */
22
#[Singleton]
23
final class AttributeResolver implements AttributeResolverInterface, AttributeResolverRegistryInterface
24
{
25
    /**
26
     * @var array<class-string<TAttribute>, AttributeResolverInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<TAttr...ibuteResolverInterface> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<TAttribute>, AttributeResolverInterface>.
Loading history...
27
     */
28
    private array $resolvers = [];
29
30 19
    public function __construct(Container $container)
31
    {
32
        /** @psalm-suppress InvalidArgument */
33 19
        $this->register(SingletonMethod::class, $container->get(AttributeResolver\SingletonMethodResolver::class));
34
        /** @psalm-suppress InvalidArgument */
35 19
        $this->register(BindMethod::class, $container->get(AttributeResolver\BindMethodResolver::class));
36
        /** @psalm-suppress InvalidArgument */
37 19
        $this->register(InjectorMethod::class, $container->get(AttributeResolver\InjectorMethodResolver::class));
38
    }
39
40 19
    public function register(string $attribute, AttributeResolverInterface $resolver): void
41
    {
42 19
        $this->resolvers[$attribute] = $resolver;
43
    }
44
45
    /**
46
     * @return class-string<TAttribute>[]
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TAttribute>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TAttribute>[].
Loading history...
47
     */
48 18
    public function getResolvers(): array
49
    {
50 18
        return \array_keys($this->resolvers);
51
    }
52
53 12
    public function resolve(object $attribute, object $service, \ReflectionMethod $method): void
54
    {
55 12
        $attributeClass = $attribute::class;
56 12
        if (!isset($this->resolvers[$attributeClass])) {
57
            throw new \RuntimeException("No resolver for attribute {$attributeClass}");
58
        }
59
60 12
        $this->resolvers[$attributeClass]->resolve($attribute, $service, $method);
61
    }
62
}
63