Passed
Push — master ( 92bcfa...82ece5 )
by butschster
17:23 queued 05:25
created

AttributesBootloader::initInstantiator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 400
    public function __construct(
32
        private readonly ConfiguratorInterface $config,
33
    ) {
34 400
    }
35
36 400
    public function init(EnvironmentInterface $env): void
37
    {
38 400
        $this->config->setDefaults(
39 400
            AttributesConfig::CONFIG,
40 400
            [
41 400
                'annotations' => [
42 400
                    'support' => $env->get('SUPPORT_ANNOTATIONS', \interface_exists(DoctrineReaderInterface::class)),
43 400
                ],
44 400
                'cache' => [
45 400
                    'storage' => $env->get('ATTRIBUTES_CACHE_STORAGE', null),
46 400
                    'enabled' => $env->get('ATTRIBUTES_CACHE_ENABLED', false),
47 400
                ],
48 400
            ],
49 400
        );
50
    }
51
52 400
    private function initInstantiator(AttributesConfig $config): InstantiatorInterface
53
    {
54 400
        if ($config->isAnnotationsReaderEnabled()) {
55 398
            return new Facade();
56
        }
57
58
        /** @psalm-suppress InternalClass */
59 2
        return new NamedArgumentsInstantiator();
60
    }
61
62 400
    private function initReader(
63
        ContainerInterface $container,
64
        InstantiatorInterface $instantiator,
65
        AttributesConfig $config,
66
    ): ReaderInterface {
67 400
        $reader = new AttributeReader($instantiator);
68
69 400
        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 400
        $supportAnnotations = $config->isAnnotationsReaderEnabled();
77
78 400
        if ($supportAnnotations) {
79 398
            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 398
            $reader = new SelectiveReader([
86 398
                $reader,
87 398
                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 398
            ]);
89
        }
90
91 400
        return $reader;
92
    }
93
}
94