CheckerSource   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 0
loc 89
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 15 4
A getNodeDefinition() 0 17 3
A prepend() 0 15 4
A getCompilerPasses() 0 7 1
A isEnabled() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\Bundle\JoseFramework\DependencyInjection\Source\SourceWithCompilerPasses;
19
use Jose\Component\Checker\ClaimCheckerManagerFactory;
20
use Jose\Component\Checker\HeaderCheckerManagerFactory;
21
use Jose\Component\Checker\TokenTypeSupport;
22
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
23
use Symfony\Component\Config\FileLocator;
24
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
27
28
class CheckerSource implements SourceWithCompilerPasses
29
{
30
    /**
31
     * @var Source[]
32
     */
33
    private $sources;
34
35
    /**
36
     * CheckerSource constructor.
37
     */
38
    public function __construct()
39
    {
40
        $this->sources = [
41
            new ClaimChecker(),
42
            new HeaderChecker(),
43
        ];
44
    }
45
46
    public function name(): string
47
    {
48
        return 'checkers';
49
    }
50
51
    public function load(array $configs, ContainerBuilder $container): void
52
    {
53
        if (!$this->isEnabled()) {
54
            return;
55
        }
56
        $container->registerForAutoconfiguration(TokenTypeSupport::class)->addTag('jose.checker.token_type');
57
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
58
        $loader->load('checkers.php');
59
60
        if (\array_key_exists('checkers', $configs)) {
61
            foreach ($this->sources as $source) {
62
                $source->load($configs['checkers'], $container);
63
            }
64
        }
65
    }
66
67
    public function getNodeDefinition(NodeDefinition $node): void
68
    {
69
        if (!$this->isEnabled()) {
70
            return;
71
        }
72
        $childNode = $node
73
            ->children()
74
            ->arrayNode($this->name())
75
            ->addDefaultsIfNotSet()
76
            ->treatFalseLike([])
77
            ->treatNullLike([])
78
        ;
79
80
        foreach ($this->sources as $source) {
81
            $source->getNodeDefinition($childNode);
82
        }
83
    }
84
85
    public function prepend(ContainerBuilder $container, array $config): array
86
    {
87
        if (!$this->isEnabled()) {
88
            return [];
89
        }
90
        $result = [];
91
        foreach ($this->sources as $source) {
92
            $prepend = $source->prepend($container, $config);
93
            if (0 !== \count($prepend)) {
94
                $result[$source->name()] = $prepend;
95
            }
96
        }
97
98
        return $result;
99
    }
100
101
    /**
102
     * @return CompilerPassInterface[]
103
     */
104
    public function getCompilerPasses(): array
105
    {
106
        return [
107
            new Compiler\ClaimCheckerCompilerPass(),
108
            new Compiler\HeaderCheckerCompilerPass(),
109
        ];
110
    }
111
112
    private function isEnabled(): bool
113
    {
114
        return class_exists(HeaderCheckerManagerFactory::class) && class_exists(ClaimCheckerManagerFactory::class);
115
    }
116
}
117