Failed Conditions
Push — master ( 58ba10...7c4f18 )
by Florent
06:41 queued 04:14
created

CheckerSource::prepend()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\JoseFramework\DependencyInjection\Source\Checker;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Compiler;
17
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
18
use Jose\Component\Checker\ClaimCheckerManagerFactory;
19
use Jose\Component\Checker\HeaderCheckerManagerFactory;
20
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
25
26
/**
27
 * Class CheckerSource.
28
 */
29
final class CheckerSource implements Source
30
{
31
    /**
32
     * @var Source[]
33
     */
34
    private $sources;
35
36
    /**
37
     * CheckerSource constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->sources = [
42
            new ClaimChecker(),
43
            new HeaderChecker(),
44
        ];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function name(): string
51
    {
52
        return 'checkers';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function load(array $configs, ContainerBuilder $container)
59
    {
60
        if (!$this->isEnabled()) {
61
            return;
62
        }
63
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
64
        $loader->load('checkers.yml');
65
66
        foreach ($this->sources as $source) {
67
            $source->load($configs['checkers'], $container);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getNodeDefinition(ArrayNodeDefinition $node)
75
    {
76
        if (!$this->isEnabled()) {
77
            return;
78
        }
79
        $childNode = $node
80
            ->children()
81
                ->arrayNode($this->name());
82
83
        foreach ($this->sources as $source) {
84
            $source->getNodeDefinition($childNode);
85
        }
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function prepend(ContainerBuilder $container, array $config): array
92
    {
93
        if (!$this->isEnabled()) {
94
            return [];
95
        }
96
        $result = [];
97
        foreach ($this->sources as $source) {
98
            $prepend = $source->prepend($container, $config);
99
            if (!empty($prepend)) {
100
                $result[$source->name()] = $prepend;
101
            }
102
        }
103
104
        return $result;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    private function isEnabled(): bool
111
    {
112
        return class_exists(HeaderCheckerManagerFactory::class) && class_exists(ClaimCheckerManagerFactory::class);
113
    }
114
115
    /**
116
     * @return CompilerPassInterface[]
117
     */
118
    public function getCompilerPasses(): array
119
    {
120
        return [
121
            new Compiler\ClaimCheckerCompilerPass(),
122
            new Compiler\HeaderCheckerCompilerPass(),
123
        ];
124
    }
125
}
126