Passed
Pull Request — master (#1059)
by Maxim
12:16
created

AttributesBootloader::initReader()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.6675

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 30
ccs 9
cts 17
cp 0.5294
rs 9.7998
c 0
b 0
f 0
cc 4
nc 6
nop 3
crap 5.6675
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Bootloader\Attributes;
6
7
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\AnnotationReader 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...
8
use Doctrine\Common\Annotations\Reader as DoctrineReaderInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\Reader 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...
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 391
    public function __construct(
32
        private readonly ConfiguratorInterface $config,
33
    ) {
34 391
    }
35
36 391
    public function init(EnvironmentInterface $env): void
37
    {
38 391
        $this->config->setDefaults(
39 391
            AttributesConfig::CONFIG,
40 391
            [
41 391
                'annotations' => [
42 391
                    'support' => $env->get('SUPPORT_ANNOTATIONS', false),
43 391
                ],
44 391
                'cache' => [
45 391
                    'storage' => $env->get('ATTRIBUTES_CACHE_STORAGE', null),
46 391
                    'enabled' => $env->get('ATTRIBUTES_CACHE_ENABLED', false),
47 391
                ],
48 391
            ],
49 391
        );
50
    }
51
52 391
    private function initInstantiator(AttributesConfig $config): InstantiatorInterface
53
    {
54 391
        if ($config->isAnnotationsReaderEnabled()) {
55
            return new Facade();
56
        }
57
58
        /** @psalm-suppress InternalClass */
59 391
        return new NamedArgumentsInstantiator();
60
    }
61
62 391
    private function initReader(
63
        ContainerInterface $container,
64
        InstantiatorInterface $instantiator,
65
        AttributesConfig $config,
66
    ): ReaderInterface {
67 391
        $reader = new AttributeReader($instantiator);
68
69 391
        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 391
        $supportAnnotations = $config->isAnnotationsReaderEnabled();
77
78 391
        if ($supportAnnotations) {
79
            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
            $reader = new SelectiveReader([
86
                $reader,
87
                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
            ]);
89
        }
90
91 391
        return $reader;
92
    }
93
}
94