RegisterAsDecoratorAttributesPass::register()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\DependencyInjection\Compiler;
17
18
use FRZB\Component\DependencyInjection\Attribute\AsDecorator;
19
use FRZB\Component\DependencyInjection\Helper\EnvironmentHelper;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
23
/**
24
 * @internal
25
 *
26
 * Register #[AsDecorator] attribute on definition that is autowired
27
 *
28
 * @author Mykhailo Shtanko <[email protected]>
29
 */
30
final class RegisterAsDecoratorAttributesPass extends AbstractRegisterAttributePass
31
{
32
    public function __construct()
33
    {
34
        parent::__construct(AsDecorator::class);
35
    }
36
37
    public function register(ContainerBuilder $container, \ReflectionClass $reflectionClass, AsDecorator $attribute): void
38
    {
39
        if (!EnvironmentHelper::isPermittedEnvironment($container, $reflectionClass)) {
40
            return;
41
        }
42
43
        $container->getDefinition($reflectionClass->getName())
44
            ->setDecoratedService($attribute->decorates, $attribute->innerName, $attribute->priority, $attribute->onInvalid->value)
45
        ;
46
    }
47
48
    protected function accept(Definition $definition): bool
49
    {
50
        return $definition->isAutowired() && $this->isAttributesIgnored($definition);
51
    }
52
}
53