Passed
Pull Request — master (#1087)
by Maxim
21:35
created

AttributesBootloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 2
b 0
f 0
dl 0
loc 68
ccs 33
cts 36
cp 0.9167
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A initInstantiator() 0 8 2
A init() 0 11 1
A initReader() 0 30 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Bootloader\Attributes;
6
7
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
8
use Doctrine\Common\Annotations\Reader as DoctrineReaderInterface;
9
use Psr\Container\ContainerInterface;
10
use Spiral\Attributes\AnnotationReader;
11
use Spiral\Attributes\AttributeReader;
12
use Spiral\Attributes\Composite\SelectiveReader;
13
use Spiral\Attributes\Exception\InitializationException;
14
use Spiral\Attributes\Internal\Instantiator\Facade;
15
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface;
16
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator;
17
use Spiral\Attributes\Psr16CachedReader;
18
use Spiral\Attributes\ReaderInterface;
19
use Spiral\Boot\Bootloader\Bootloader;
20
use Spiral\Boot\EnvironmentInterface;
21
use Spiral\Cache\CacheStorageProviderInterface;
22
use Spiral\Config\ConfiguratorInterface;
23
24
class AttributesBootloader extends Bootloader
25
{
26
    protected const SINGLETONS = [
27
        ReaderInterface::class => [self::class, 'initReader'],
28
        InstantiatorInterface::class => [self::class, 'initInstantiator'],
29
    ];
30
31 393
    public function __construct(
32
        private readonly ConfiguratorInterface $config,
33
    ) {
34 393
    }
35
36 393
    public function init(EnvironmentInterface $env): void
37
    {
38 393
        $this->config->setDefaults(
39 393
            AttributesConfig::CONFIG,
40 393
            [
41 393
                'annotations' => [
42 393
                    'support' => $env->get('SUPPORT_ANNOTATIONS', \interface_exists(DoctrineReaderInterface::class)),
43 393
                ],
44 393
                'cache' => [
45 393
                    'storage' => $env->get('ATTRIBUTES_CACHE_STORAGE', null),
46 393
                    'enabled' => $env->get('ATTRIBUTES_CACHE_ENABLED', false),
47 393
                ],
48 393
            ],
49 393
        );
50
    }
51
52 393
    private function initInstantiator(AttributesConfig $config): InstantiatorInterface
53
    {
54 393
        if ($config->isAnnotationsReaderEnabled()) {
55 391
            return new Facade();
56
        }
57
58
        /** @psalm-suppress InternalClass */
59 2
        return new NamedArgumentsInstantiator();
60
    }
61
62 393
    private function initReader(
63
        ContainerInterface $container,
64
        InstantiatorInterface $instantiator,
65
        AttributesConfig $config,
66
    ): ReaderInterface {
67 393
        $reader = new AttributeReader($instantiator);
68
69 393
        if ($config->isCacheEnabled()) {
70 1
            $provider = $container->get(CacheStorageProviderInterface::class);
71 1
            \assert($provider instanceof CacheStorageProviderInterface);
72
73 1
            $reader = new Psr16CachedReader($reader, $provider->storage($config->getCacheStorage()));
74
        }
75
76 393
        $supportAnnotations = $config->isAnnotationsReaderEnabled();
77
78 393
        if ($supportAnnotations) {
79 391
            if (!\interface_exists(DoctrineReaderInterface::class)) {
80
                throw new InitializationException(
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Attributes\Except...InitializationException has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
                throw /** @scrutinizer ignore-deprecated */ new InitializationException(
Loading history...
81
                    'Doctrine annotations reader is not available, please install "doctrine/annotations" package',
82
                );
83
            }
84
85 391
            $reader = new SelectiveReader([
86 391
                $reader,
87 391
                new AnnotationReader(new DoctrineAnnotationReader()),
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Attributes\AnnotationReader has been deprecated: Use {@see AttributeReader} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
                /** @scrutinizer ignore-deprecated */ new AnnotationReader(new DoctrineAnnotationReader()),
Loading history...
88 391
            ]);
89
        }
90
91 393
        return $reader;
92
    }
93
}
94