loadGuzzleMiddlewareConfiguration()   B
last analyzed

Complexity

Conditions 11
Paths 75

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 75
nop 3
dl 0
loc 37
ccs 21
cts 21
cp 1
crap 11
rs 7.3166
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 17
    public function load(array $configs, ContainerBuilder $container)
15
    {
16 17
        $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__) . '/Resources/config'));
17 17
        $loader->load('console.yml');
18 17
        $loader->load('service.yml');
19
20 17
        $configuration = $this->getConfiguration($configs, $container);
21 17
        $config = $this->processConfiguration($configuration, $configs);
22
23 17
        $container->setParameter('sapient.sealing_public_keys', $config['sealing_public_keys']);
24 17
        $container->setParameter('sapient.verifying_public_keys', $config['verifying_public_keys']);
25
26 17
        $this->loadSignConfiguration($config, $loader, $container);
27 17
        $this->loadSealConfiguration($config, $loader, $container);
28 16
        $this->loadGuzzleMiddlewareConfiguration($config, $loader, $container);
29 12
        $this->loadVerifyRequestConfiguration($config, $loader);
30 12
        $this->loadUnsealRequestConfiguration($config, $loader);
31 11
    }
32
33 17
    private function loadSignConfiguration(array $config, YamlFileLoader $loader, ContainerBuilder $container): void
34
    {
35 17
        if ($config['sign']['enabled']) {
36 13
            $container->setParameter('sapient.sign.public', $config['sign']['public']);
37 13
            $container->setParameter('sapient.sign.private', $config['sign']['private']);
38 13
            $container->setParameter('sapient.sign.host', $config['sign']['host']);
39
40 13
            if ($config['sign']['response']) {
41 2
                $loader->load('sign.yml');
42
            }
43
        }
44 17
    }
45
46 17
    private function loadSealConfiguration(array $config, YamlFileLoader $loader, ContainerBuilder $container): void
47
    {
48 17
        if ($config['seal']['enabled']) {
49 10
            $container->setParameter('sapient.seal.public', $config['seal']['public']);
50 10
            $container->setParameter('sapient.seal.private', $config['seal']['private']);
51
52 10
            if (!$config['seal']['response']) {
53 8
                return;
54
            }
55
56 2
            if (!$config['sign']['enabled'] || !$config['sign']['response']) {
57 1
                throw new ConfigurationRequiredException('You must enable "sign" option with "sign.response" as true before using "seal.response" feature.');
58
            }
59
60 1
            $loader->load('seal.yml');
61
        }
62 8
    }
63
64 16
    private function loadGuzzleMiddlewareConfiguration(array $config, YamlFileLoader $loader, ContainerBuilder $container): void
65
    {
66 16
        if ($config['guzzle_middleware']['enabled']) {
67 9
            if ($config['guzzle_middleware']['verify']) {
68 1
                $loader->load('guzzle_middleware/verify_response.yml');
69
            }
70
71 9
            if ($config['guzzle_middleware']['unseal']) {
72 2
                if (!$config['seal']['enabled']) {
73 1
                    throw new ConfigurationRequiredException('You must enable "seal" option and configure a "seal.private" key before using "guzzle_middleware.unseal" feature.');
74
                }
75
76 1
                $loader->load('guzzle_middleware/unseal_response.yml');
77
            }
78
79 8
            if ($config['guzzle_middleware']['requester_host']) {
80 1
                $container->setParameter('sapient.guzzle_middleware.requester_host', $config['guzzle_middleware']['requester_host']);
81 1
                $loader->load('guzzle_middleware/requester_header.yml');
82
            }
83
84 8
            if ($config['guzzle_middleware']['sign_request']) {
85 4
                if (!$config['sign']['enabled']) {
86 1
                    throw new ConfigurationRequiredException('You must enable "sign" option and configure a "sign.private" key before using "guzzle_middleware.sign_request" feature.');
87
                }
88
89 3
                $loader->load('guzzle_middleware/sign_request.yml');
90
            }
91
92 7
            if ($config['guzzle_middleware']['seal_request']) {
93 3
                if (!$config['seal']['enabled']) {
94 1
                    throw new ConfigurationRequiredException('You must enable "seal" option and configure a "seal.private" key before using "guzzle_middleware.seal_request" feature.');
95
                }
96
97 2
                if (!$config['guzzle_middleware']['sign_request']) {
98 1
                    throw new ConfigurationRequiredException('You must enable "guzzle_middleware.sign_request" option before using "guzzle_middleware.seal_request" feature.');
99
                }
100 1
                $loader->load('guzzle_middleware/seal_request.yml');
101
            }
102
        }
103 12
    }
104
105 12
    private function loadUnsealRequestConfiguration(array $config, YamlFileLoader $loader): void
106
    {
107 12
        if ($config['unseal_request']) {
108 2
            if (!$config['seal']['enabled']) {
109 1
                throw new ConfigurationRequiredException('You must enable "seal" option before using "unseal_request" feature.');
110
            }
111
112 1
            $loader->load('unseal_request.yml');
113
        }
114 11
    }
115
116 12
    private function loadVerifyRequestConfiguration(array $config, YamlFileLoader $loader): void
117
    {
118 12
        if ($config['verify_request']) {
119 1
            $loader->load('verify_request.yml');
120
        }
121 12
    }
122
}
123