Failed Conditions
Push — master ( e08481...7ad838 )
by Florent
03:52 queued 01:57
created

NestedToken::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-2018 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\Encryption;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
17
use Jose\Component\Checker\HeaderCheckerManagerFactory;
18
use Jose\Component\Encryption\JWEDecrypterFactory;
19
use Jose\Component\Signature\JWSVerifierFactory;
20
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
24
25
class NestedToken implements Source
26
{
27
    /**
28
     * @var Source[]
29
     */
30
    private $sources;
31
32
    /**
33
     * EncryptionSource constructor.
34
     */
35
    public function __construct()
36
    {
37
        $this->sources = [
38
            new NestedTokenLoader(),
39
            new NestedTokenBuilder(),
40
        ];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function name(): string
47
    {
48
        return 'nested_token';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function load(array $configs, ContainerBuilder $container)
55
    {
56
        if (!$this->isEnabled()) {
57
            return;
58
        }
59
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
60
        $loader->load('nested_token.yml');
61
62
        if (array_key_exists('nested_token', $configs)) {
63
            foreach ($this->sources as $source) {
64
                $source->load($configs['nested_token'], $container);
65
            }
66
        }
67
    }
68
69
    public function getNodeDefinition(NodeDefinition $node)
70
    {
71
        if (!$this->isEnabled()) {
72
            return;
73
        }
74
        $childNode = $node->children()
75
            ->arrayNode($this->name())
76
                ->treatNullLike([])
77
                ->treatFalseLike([]);
78
79
        foreach ($this->sources as $source) {
80
            $source->getNodeDefinition($childNode);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function prepend(ContainerBuilder $container, array $config): array
88
    {
89
        if (!$this->isEnabled()) {
90
            return [];
91
        }
92
        $result = [];
93
        foreach ($this->sources as $source) {
94
            $prepend = $source->prepend($container, $config);
95
            if (!empty($prepend)) {
96
                $result[$source->name()] = $prepend;
97
            }
98
        }
99
100
        return $result;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    private function isEnabled(): bool
107
    {
108
        return class_exists(JWEDecrypterFactory::class)
109
            && class_exists(JWSVerifierFactory::class)
110
            && class_exists(HeaderCheckerManagerFactory::class);
111
    }
112
}
113