1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lamoda\AtolClientBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Lamoda\AtolClient\V3\AtolApi as AtolApiV3; |
6
|
|
|
use Lamoda\AtolClient\V4\AtolApi as AtolApiV4; |
7
|
|
|
use Lamoda\AtolClientBundle\AtolClientBundle; |
8
|
|
|
use Symfony\Component\Config\FileLocator; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
12
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
13
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
14
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
15
|
|
|
|
16
|
|
|
final class AtolClientExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
3 |
|
public function load(array $configs, ContainerBuilder $container) |
22
|
|
|
{ |
23
|
3 |
|
$configuration = new Configuration(); |
24
|
3 |
|
$config = $this->processConfiguration($configuration, $configs); |
25
|
3 |
|
$this->loadConfig($container); |
26
|
3 |
|
$this->createAtolClients($config, $container); |
27
|
3 |
|
} |
28
|
|
|
|
29
|
3 |
|
private function loadConfig(ContainerBuilder $container): void |
30
|
|
|
{ |
31
|
3 |
|
$locator = new FileLocator(__DIR__ . '/../Resources/config'); |
32
|
3 |
|
$loader = new YamlFileLoader($container, $locator); |
33
|
3 |
|
$loader->load('services.yml'); |
34
|
3 |
|
} |
35
|
|
|
|
36
|
3 |
|
private function createAtolClients(array $config, ContainerBuilder $container): void |
37
|
|
|
{ |
38
|
3 |
|
foreach ($config['clients'] as $name => $clientConfig) { |
39
|
3 |
|
$this->createAtolClient($name, $clientConfig, $container); |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
if ($container->hasDefinition('atol_client.v3.default')) { |
43
|
2 |
|
$container->setAlias('atol_client.v3', 'atol_client.v3.default'); |
44
|
2 |
|
$container->setAlias(AtolApiV3::class, 'atol_client.v3.default'); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
if ($container->hasDefinition('atol_client.v4.default')) { |
48
|
1 |
|
$container->setAlias('atol_client.v4', 'atol_client.v4.default'); |
49
|
1 |
|
$container->setAlias(AtolApiV4::class, 'atol_client.v4.default'); |
50
|
|
|
} |
51
|
3 |
|
} |
52
|
|
|
|
53
|
3 |
|
private function createAtolClient(string $name, array $clientConfig, ContainerBuilder $container): void |
54
|
|
|
{ |
55
|
3 |
|
switch ($clientConfig['version']) { |
56
|
|
|
case AtolClientBundle::API_CLIENT_VERSION_3: |
57
|
2 |
|
$this->createAtolClientV3($name, $clientConfig, $container); |
58
|
2 |
|
break; |
59
|
|
|
case AtolClientBundle::API_CLIENT_VERSION_4: |
60
|
2 |
|
$this->createAtolClientV4($name, $clientConfig, $container); |
61
|
2 |
|
break; |
62
|
|
|
default: |
63
|
|
|
throw new InvalidArgumentException('Wrong client version: ' . $clientConfig['version']); |
64
|
|
|
} |
65
|
3 |
|
} |
66
|
|
|
|
67
|
2 |
View Code Duplication |
private function createAtolClientV3(string $name, array $clientConfig, ContainerBuilder $container): void |
|
|
|
|
68
|
|
|
{ |
69
|
2 |
|
$definition = new Definition(AtolApiV3::class, [ |
70
|
2 |
|
new Reference('atol_client.object_converter'), |
71
|
2 |
|
new Reference($clientConfig['guzzle_client']), |
72
|
2 |
|
$clientConfig['guzzle_client_options'], |
73
|
2 |
|
$clientConfig['base_url'], |
74
|
2 |
|
$clientConfig['cash_register_group_code'], |
75
|
|
|
]); |
76
|
2 |
|
$definition->setPublic(false); |
77
|
|
|
|
78
|
2 |
|
$id = 'atol_client.v3.' . $name; |
79
|
|
|
|
80
|
2 |
|
$container->setDefinition($id, $definition); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
View Code Duplication |
private function createAtolClientV4(string $name, array $clientConfig, ContainerBuilder $container): void |
|
|
|
|
84
|
|
|
{ |
85
|
2 |
|
$definition = new Definition(AtolApiV4::class, [ |
86
|
2 |
|
new Reference('atol_client.object_converter'), |
87
|
2 |
|
new Reference($clientConfig['guzzle_client']), |
88
|
2 |
|
$clientConfig['guzzle_client_options'], |
89
|
2 |
|
$clientConfig['base_url'], |
90
|
|
|
]); |
91
|
2 |
|
$definition->setPublic(false); |
92
|
|
|
|
93
|
2 |
|
$id = 'atol_client.v4.' . $name; |
94
|
|
|
|
95
|
2 |
|
$container->setDefinition($id, $definition); |
96
|
2 |
|
} |
97
|
|
|
} |
98
|
|
|
|
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.