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

EncryptionSource::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-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\Encryption;
15
16
use Jose\Bundle\JoseFramework\DependencyInjection\Compiler;
17
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
18
use Jose\Component\Encryption\JWEBuilderFactory;
19
use Jose\Component\Encryption\JWEDecrypterFactory;
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 EncryptionSource.
28
 */
29
final class EncryptionSource implements Source
30
{
31
    /**
32
     * @var Source[]
33
     */
34
    private $sources;
35
36
    /**
37
     * EncryptionSource constructor.
38
     */
39
    public function __construct()
40
    {
41
        $this->sources = [
42
            new JWEBuilder(),
43
            new JWEDecrypter(),
44
            new JWESerializer(),
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function name(): string
52
    {
53
        return 'jwe';
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('jwe_services.yml');
66
        $loader->load('encryption_algorithms.yml');
67
        $loader->load('jwe_serializers.yml');
68
        $loader->load('compression_methods.yml');
69
70
        foreach ($this->sources as $source) {
71
            $source->load($configs['jwe'], $container);
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getNodeDefinition(ArrayNodeDefinition $node)
79
    {
80
        if (!$this->isEnabled()) {
81
            return;
82
        }
83
        $childNode = $node
84
            ->children()
85
            ->arrayNode($this->name());
86
87
        foreach ($this->sources as $source) {
88
            $source->getNodeDefinition($childNode);
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function prepend(ContainerBuilder $container, array $config): array
96
    {
97
        if (!$this->isEnabled()) {
98
            return [];
99
        }
100
        $result = [];
101
        foreach ($this->sources as $source) {
102
            $prepend = $source->prepend($container, $config);
103
            if (!empty($prepend)) {
104
                $result[$source->name()] = $prepend;
105
            }
106
        }
107
108
        return $result;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    private function isEnabled(): bool
115
    {
116
        return class_exists(JWEBuilderFactory::class) && class_exists(JWEDecrypterFactory::class);
117
    }
118
119
    /**
120
     * @return CompilerPassInterface[]
121
     */
122
    public function getCompilerPasses(): array
123
    {
124
        return [
125
            new Compiler\EncryptionSerializerCompilerPass(),
126
            new Compiler\CompressionMethodCompilerPass(),
127
        ];
128
    }
129
}
130