Failed Conditions
Pull Request — master (#47)
by Florent
06:42
created

CheckerSource   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 9
dl 0
loc 97
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A name() 0 4 1
A load() 0 12 3
A getNodeDefinition() 0 13 3
A prepend() 0 15 4
A isEnabled() 0 4 2
A getCompilerPasses() 0 7 1
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