1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Nexylan packages. |
5
|
|
|
* |
6
|
|
|
* (c) Nexylan SAS <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Nexy\PayboxDirect\Bridge\Symfony\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Nexy\PayboxDirect\Enum\Activity; |
15
|
|
|
use Nexy\PayboxDirect\Enum\Currency; |
16
|
|
|
use Nexy\PayboxDirect\Enum\Version; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
20
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Sullivan Senechal <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class NexyPayboxDirectExtension extends Extension |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function load(array $configs, ContainerBuilder $container) |
31
|
|
|
{ |
32
|
|
|
$configuration = new Configuration(); |
33
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
34
|
|
|
|
35
|
|
|
$this->processOptions($config, $container); |
36
|
|
|
|
37
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
38
|
|
|
$loader->load('sdk.xml'); |
39
|
|
|
$loader->load('http_clients.xml'); |
40
|
|
|
|
41
|
|
|
$this->processClient($config, $container); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $config |
46
|
|
|
* @param ContainerBuilder $container |
47
|
|
|
*/ |
48
|
|
|
private function processOptions(array $config, ContainerBuilder $container) |
49
|
|
|
{ |
50
|
|
|
// Start with client option |
51
|
|
|
$options = $config['options']; |
52
|
|
|
|
53
|
|
|
// Paybox version and default_currency special hack: Get the number. |
54
|
|
|
$options['paybox_version'] = constant(Version::class.'::'.strtoupper($config['paybox']['version'])); |
55
|
|
|
unset($config['paybox']['version']); |
56
|
|
View Code Duplication |
if (array_key_exists('default_currency', $config['paybox'])) { |
|
|
|
|
57
|
|
|
$options['paybox_default_currency'] = constant(Currency::class.'::'.strtoupper($config['paybox']['default_currency'])); |
58
|
|
|
unset($config['paybox']['default_currency']); |
59
|
|
|
} |
60
|
|
View Code Duplication |
if (array_key_exists('default_activity', $config['paybox'])) { |
|
|
|
|
61
|
|
|
$options['paybox_default_activity'] = constant(Activity::class.'::'.strtoupper($config['paybox']['default_activity'])); |
62
|
|
|
unset($config['paybox']['default_activity']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Convert paybox option format |
66
|
|
|
foreach ($config['paybox'] as $key => $value) { |
67
|
|
|
$options['paybox_'.$key] = (string) $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$container->setParameter('nexy_paybox_direct.options', $options); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $config |
75
|
|
|
* @param ContainerBuilder $container |
76
|
|
|
*/ |
77
|
|
|
private function processClient(array $config, ContainerBuilder $container) |
78
|
|
|
{ |
79
|
|
|
if (null === $config['client']) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$container->findDefinition('nexy_paybox_direct.sdk')->addArgument( |
84
|
|
|
$container->findDefinition('nexy_paybox_direct.http_client.'.$config['client']) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.