Passed
Branch master (c65ffc)
by Dāvis
03:08
created

Captcha::getConfigurator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\DependencyInjection\Component;
4
5
use Sludio\HelperBundle\Captcha\Configurator;
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
9
use Symfony\Component\Config\Definition\Processor;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
12
class Captcha implements Extensionable
13
{
14
    protected $type;
15
16
    /**
17
     * List of available Oauth providers
18
     * @var array
19
     */
20
    protected static $supportedTypes = [
21
        'recaptcha_v2' => Configurator\ReCaptchaConfigurator::class,
22
        'custom' => Configurator\CustomCaptchaConfigurator::class,
23
    ];
24
25
    protected $usedTypes = [];
26
    public $configurators = [];
27
28 View Code Duplication
    public function getConfigurator($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        if (!isset($this->configurators[$type])) {
31
            $class = self::$supportedTypes[$type];
32
33
            $this->configurators[$type] = new $class();
34
        }
35
36
        return $this->configurators[$type];
37
    }
38
39
    public function configure(ContainerBuilder &$container)
40
    {
41
        $clientConfigurations = $container->getParameter('sludio_helper.captcha.clients');
42
        foreach ($clientConfigurations as $key => $clientConfig) {
43
            $tree = new TreeBuilder();
44
            $processor = new Processor();
45
46
            if (!isset($clientConfig['type'])) {
47
                throw new InvalidConfigurationException(sprintf('sludio_helper_captcha_client.clients.%s config entry is missing the "type" key.', $key));
48
            }
49
50
            $this->type = $clientConfig['type'];
51
            unset($clientConfig['type']);
52 View Code Duplication
            if (!isset(self::$supportedTypes[$this->type])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                $supportedKeys = array_keys(self::$supportedTypes);
54
                sort($supportedKeys);
55
                throw new InvalidConfigurationException(sprintf('sludio_helper_captcha_client.clients config "type" key "%s" is not supported. Supported: %s', $this->type, implode(', ', $supportedKeys)));
56
            }
57
58
            if (!in_array($this->type, $this->usedTypes)) {
59
                $this->usedTypes[] = $this->type;
60
            } else {
61
                throw new InvalidConfigurationException(sprintf('sludio_helper_captcha_client.clients config "type" key "%s" is already in use. Only one occurence by type is allowed', $this->type));
62
            }
63
64
            $node = $tree->root('sludio_helper_captcha_client/clients/'.$key);
65
            $this->buildClientConfiguration($node);
66
            $config = $processor->process($tree->buildTree(), [$clientConfig]);
67
            $clientServiceKey = 'sludio_helper.captcha.client.'.$key;
68
            foreach ($config as $ckey => $cvalue) {
69
                $container->setParameter($clientServiceKey.'.'.$ckey, $cvalue);
70
            }
71
            $this->configureClient($container, $clientServiceKey);
72
        }
73
    }
74
75
    public function buildClientConfiguration(NodeDefinition &$node)
76
    {
77
        $optionsNode = $node->children();
78
        $this->getConfigurator($this->getType())->buildConfiguration($optionsNode);
79
        $optionsNode->end();
80
    }
81
82
    public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = [])
83
    {
84
        $this->getConfigurator($this->getType())->configureClient($container, $clientServiceKey, $options);
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90
    public function getType()
91
    {
92
        return $this->type;
93
    }
94
95
    /**
96
     * @param $type
97
     *
98
     * @return $this
99
     */
100
    public function setType($type)
101
    {
102
        $this->type = $type;
103
104
        return $this;
105
    }
106
107
}