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

SignatureSource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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\Signature;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Compiler;
17
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
18
use Jose\Component\Signature\JWSBuilderFactory;
19
use Jose\Component\Signature\JWSVerifierFactory;
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 SignatureSource.
28
 */
29
final class SignatureSource implements Source
30
{
31
    /**
32
     * @var Source[]
33
     */
34
    private $sources;
35
36
    /**
37
     * SignatureSource constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->sources = [
42
            new JWSBuilder(),
43
            new JWSVerifier(),
44
            new JWSSerializer(),
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function name(): string
52
    {
53
        return 'jws';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function load(array $configs, ContainerBuilder $container)
60
    {
61
        if (!$this->isEnabled()) {
62
            return;
63
        }
64
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
65
        $loader->load('jws_services.yml');
66
        $loader->load('jws_serializers.yml');
67
        $loader->load('signature_algorithms.yml');
68
69
        foreach ($this->sources as $source) {
70
            $source->load($configs['jws'], $container);
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getNodeDefinition(ArrayNodeDefinition $node)
78
    {
79
        if (!$this->isEnabled()) {
80
            return;
81
        }
82
        $childNode = $node
83
            ->children()
84
            ->arrayNode($this->name());
85
86
        foreach ($this->sources as $source) {
87
            $source->getNodeDefinition($childNode);
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function prepend(ContainerBuilder $container, array $config): array
95
    {
96
        if (!$this->isEnabled()) {
97
            return [];
98
        }
99
        $result = [];
100
        foreach ($this->sources as $source) {
101
            $prepend = $source->prepend($container, $config);
102
            if (!empty($prepend)) {
103
                $result[$source->name()] = $prepend;
104
            }
105
        }
106
107
        return $result;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    private function isEnabled(): bool
114
    {
115
        return class_exists(JWSBuilderFactory::class) && class_exists(JWSVerifierFactory::class);
116
    }
117
118
    /**
119
     * @return CompilerPassInterface[]
120
     */
121
    public function getCompilerPasses(): array
122
    {
123
        return [
124
            new Compiler\SignatureSerializerCompilerPass(),
125
        ];
126
    }
127
}
128