Passed
Pull Request — master (#36)
by Thierry
03:02
created

SapientExtension::loadGuzzleMiddlewareCapability()   C

Complexity

Conditions 12
Paths 75

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 20
nc 75
nop 3
dl 0
loc 37
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace lepiaf\SapientBundle\DependencyInjection;
5
6
use lepiaf\SapientBundle\Exception\ConfigurationRequiredException;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Extension\Extension;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\Config\FileLocator;
11
12
class SapientExtension extends Extension
13
{
14
    public function load(array $configs, ContainerBuilder $container)
15
    {
16
        $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__) . '/Resources/config'));
17
        $loader->load('console.yml');
18
        $loader->load('service.yml');
19
20
        $configuration = $this->getConfiguration($configs, $container);
21
        $config = $this->processConfiguration($configuration, $configs);
22
23
        $container->setParameter('sapient.sealing_public_keys', $config['sealing_public_keys']);
24
        $container->setParameter('sapient.verifying_public_keys', $config['verifying_public_keys']);
25
26
        $this->loadSignCapability($config, $loader, $container);
27
        $this->loadSealCapability($config, $loader, $container);
28
        $this->loadGuzzleMiddlewareCapability($config, $loader, $container);
29
        $this->loadVerifyRequestCapability($config, $loader);
30
        $this->loadUnsealRequestCapability($config, $loader);
31
    }
32
33
    private function loadSignCapability(array $config, YamlFileLoader $loader, ContainerBuilder $container): void
34
    {
35
        if ($config['sign']['enabled']) {
36
            $container->setParameter('sapient.sign.public', $config['sign']['public']);
37
            $container->setParameter('sapient.sign.private', $config['sign']['private']);
38
            $container->setParameter('sapient.sign.host', $config['sign']['host']);
39
40
            if ($config['sign']['response']) {
41
                $loader->load('sign.yml');
42
            }
43
        }
44
    }
45
46
    private function loadSealCapability(array $config, YamlFileLoader $loader, ContainerBuilder $container): void
47
    {
48
        if ($config['seal']['enabled']) {
49
            $container->setParameter('sapient.seal.public', $config['seal']['public']);
50
            $container->setParameter('sapient.seal.private', $config['seal']['private']);
51
            if ($config['seal']['response'] && (!$config['sign']['enabled'] || !$config['sign']['response'])) {
52
                throw new ConfigurationRequiredException('You must enable "sign" option with "sign.response" as true before using "seal.response" feature.');
53
            }
54
55
            $loader->load('seal.yml');
56
        }
57
    }
58
59
    private function loadGuzzleMiddlewareCapability(array $config, YamlFileLoader $loader, ContainerBuilder $container): void
60
    {
61
        if ($config['guzzle_middleware']['enabled']) {
62
            if ($config['guzzle_middleware']['verify']) {
63
                $loader->load('guzzle_middleware/verify_response.yml');
64
            }
65
66
            if ($config['guzzle_middleware']['unseal']) {
67
                if (!$config['seal']['enabled'] || !$config['seal']['private']) {
68
                    throw new ConfigurationRequiredException('You must enable "seal" option and configure a "seal.private" key before using "guzzle_middleware.unseal" feature.');
69
                }
70
71
                $loader->load('guzzle_middleware/unseal_response.yml');
72
            }
73
74
            if ($config['guzzle_middleware']['requester_host']) {
75
                $container->setParameter('sapient.guzzle_middleware.requester_host', $config['guzzle_middleware']['requester_host']);
76
                $loader->load('guzzle_middleware/requester_header.yml');
77
            }
78
79
            if ($config['guzzle_middleware']['sign_request']) {
80
                if (!$config['sign']['enabled']) {
81
                    throw new ConfigurationRequiredException('You must enable "sign" option and configure a "sign.private" key before using "guzzle_middleware.sign_request" feature.');
82
                }
83
84
                $loader->load('guzzle_middleware/sign_request.yml');
85
            }
86
87
            if ($config['guzzle_middleware']['seal_request']) {
88
                if (!$config['seal']['enabled']) {
89
                    throw new ConfigurationRequiredException('You must enable "seal" option and configure a "seal.private" key before using "guzzle_middleware.seal_request" feature.');
90
                }
91
92
                if (!$config['guzzle_middleware']['sign_request']) {
93
                    throw new ConfigurationRequiredException('You must enable "guzzle_middleware.sign_request" option before using "guzzle_middleware.seal_request" feature.');
94
                }
95
                $loader->load('guzzle_middleware/seal_request.yml');
96
            }
97
        }
98
    }
99
100
    private function loadUnsealRequestCapability(array $config, YamlFileLoader $loader): void
101
    {
102
        if ($config['unseal_request']) {
103
            if (!$config['seal']['enabled']) {
104
                throw new ConfigurationRequiredException('You must enable "seal" option before using "unseal_request" feature.');
105
            }
106
107
            $loader->load('unseal_request.yml');
108
        }
109
    }
110
111
    private function loadVerifyRequestCapability(array $config, YamlFileLoader $loader): void
112
    {
113
        if ($config['verify_request']) {
114
            $loader->load('verify_request.yml');
115
        }
116
    }
117
}
118