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 ExtensionInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* List of available Oauth providers |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected static $supportedTypes = [ |
19
|
|
|
'recaptcha_v2' => Configurator\ReCaptchaConfigurator::class, |
20
|
|
|
'custom' => Configurator\CustomCaptchaConfigurator::class, |
21
|
|
|
]; |
22
|
|
|
public $configurators = []; |
23
|
|
|
protected $type; |
24
|
|
|
protected $alias; |
25
|
|
|
protected $usedTypes = []; |
26
|
|
|
|
27
|
|
|
public function configure(ContainerBuilder $container, $alias) |
28
|
|
|
{ |
29
|
|
|
$this->alias = $alias.'.captcha'; |
30
|
|
|
$clientConfigurations = $container->getParameter($this->alias.'.clients'); |
31
|
|
|
/** @var $clientConfigurations array */ |
32
|
|
|
foreach ($clientConfigurations as $key => $clientConfig) { |
33
|
|
|
$tree = new TreeBuilder(); |
34
|
|
|
$processor = new Processor(); |
35
|
|
|
|
36
|
|
|
if (!isset($clientConfig['type'])) { |
37
|
|
|
throw new InvalidConfigurationException(sprintf('sludio_helper_captcha_client.clients.%s config entry is missing the "type" key.', $key)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->type = $clientConfig['type']; |
41
|
|
|
unset($clientConfig['type']); |
42
|
|
|
if (!isset(self::$supportedTypes[$this->type])) { |
43
|
|
|
$supportedKeys = array_keys(self::$supportedTypes); |
44
|
|
|
sort($supportedKeys); |
45
|
|
|
throw new InvalidConfigurationException(sprintf('sludio_helper_captcha_client.clients config "type" key "%s" is not supported. Supported: %s', $this->type, implode(', ', $supportedKeys))); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!\in_array($this->type, $this->usedTypes, true)) { |
49
|
|
|
$this->usedTypes[] = $this->type; |
50
|
|
|
} else { |
51
|
|
|
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)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$node = $tree->root('sludio_helper_captcha_client/clients/'.$key); |
55
|
|
|
$this->buildClientConfiguration($node); |
56
|
|
|
$config = $processor->process($tree->buildTree(), [$clientConfig]); |
57
|
|
|
$clientServiceKey = $this->alias.'.client.'.$key; |
58
|
|
|
foreach ($config as $ckey => $cvalue) { |
59
|
|
|
$container->setParameter($clientServiceKey.'.'.$ckey, $cvalue); |
60
|
|
|
} |
61
|
|
|
$this->configureClient($container, $clientServiceKey); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function buildClientConfiguration(NodeDefinition $node) |
66
|
|
|
{ |
67
|
|
|
$node->addDefaultsIfNotSet(); |
|
|
|
|
68
|
|
|
$optionsNode = $node->children(); |
69
|
|
|
$this->getConfigurator($this->getType())->buildConfiguration($optionsNode); |
70
|
|
|
$optionsNode->end(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getConfigurator($type) |
74
|
|
|
{ |
75
|
|
|
if (!isset($this->configurators[$type])) { |
76
|
|
|
$class = self::$supportedTypes[$type]; |
77
|
|
|
|
78
|
|
|
$this->configurators[$type] = new $class(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->configurators[$type]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function getType() |
88
|
|
|
{ |
89
|
|
|
return $this->type; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $type |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setType($type) |
98
|
|
|
{ |
99
|
|
|
$this->type = $type; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = []) |
105
|
|
|
{ |
106
|
|
|
$this->getConfigurator($this->getType())->configureClient($container, $clientServiceKey, $options); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|