Failed Conditions
Pull Request — master (#47)
by Florent
04:28
created

KeyManagementSource   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A name() 0 4 1
A load() 0 14 3
A getNodeDefinition() 0 9 3
A prepend() 0 15 4
A isEnabled() 0 4 1
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\KeyManagement;
15
16
use Http\HttplugBundle\HttplugBundle;
17
use Jose\Bundle\JoseFramework\DependencyInjection\Compiler;
18
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
19
use Jose\Component\KeyManagement\JWKFactory;
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 KeyManagementSource.
28
 */
29
final class KeyManagementSource implements Source
30
{
31
    /**
32
     * @var Source[]
33
     */
34
    private $sources;
35
36
    /**
37
     * KeyManagementSource constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->sources = [
42
            new JWKSetSource(),
43
            new JWKSource(),
44
            new JWKUriSource(),
45
        ];
46
        if (class_exists(HttplugBundle::class)) {
47
            $this->sources[] = new JKUSource();
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function name(): string
55
    {
56
        return 'key_mgmt';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function load(array $configs, ContainerBuilder $container)
63
    {
64
        if (!$this->isEnabled()) {
65
            return;
66
        }
67
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
68
        $loader->load('analyzers.yml');
69
        $loader->load('jwk_factory.yml');
70
        $loader->load('jwk_services.yml');
71
72
        foreach ($this->sources as $source) {
73
            $source->load($configs, $container);
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getNodeDefinition(ArrayNodeDefinition $node)
81
    {
82
        if (!$this->isEnabled()) {
83
            return;
84
        }
85
        foreach ($this->sources as $source) {
86
            $source->getNodeDefinition($node);
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function prepend(ContainerBuilder $container, array $config): array
94
    {
95
        if (!$this->isEnabled()) {
96
            return [];
97
        }
98
        $result = [];
99
        foreach ($this->sources as $source) {
100
            $prepend = $source->prepend($container, $config);
101
            if (!empty($prepend)) {
102
                $result[$source->name()] = $prepend;
103
            }
104
        }
105
106
        return $result;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    private function isEnabled(): bool
113
    {
114
        return class_exists(JWKFactory::class);
115
    }
116
117
    /**
118
     * @return CompilerPassInterface[]
119
     */
120
    public function getCompilerPasses(): array
121
    {
122
        return [
123
            new Compiler\KeyAnalyzerCompilerPass(),
124
            new Compiler\KeySetControllerCompilerPass(),
125
        ];
126
    }
127
}
128