Completed
Pull Request — master (#2)
by Evgeny
04:13
created

AtolClientExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 35.37 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 29
loc 82
ccs 49
cts 50
cp 0.98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 7 1
A loadConfig() 0 6 1
A createAtolClients() 0 16 4
A createAtolClientV3() 15 15 1
A createAtolClientV4() 14 14 1
A createAtolClient() 0 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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...
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
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...
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