Completed
Pull Request — master (#3)
by Jacob
02:20
created

SearchClients   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 42.86 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 4
dl 24
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createElasticClient() 0 16 1
B load() 24 24 4

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 As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader;
4
5
use As3\Bundle\ModlrBundle\DependencyInjection\Utility;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Loads search client services.
12
 *
13
 * @author  Jacob Bare <[email protected]>
14
 */
15
class SearchClients implements ServiceLoaderInterface
16
{
17
    /**
18
     * Creates the Elastic search client service definition.
19
     * Will also load support services.
20
     *
21
     * @param   string              $clientName
22
     * @param   array               $clientConfig
23
     * @param   ContainerBuilder    $container
24
     * @return  Definition
25
     */
26
    private function createElasticClient($clientName, array $clientConfig, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $clientConfig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    {
28
        // Storage metadata
29
        $smfName = sprintf('%s.metadata', $clientName);
30
        $definition = new Definition(
31
            Utility::getLibraryClass('Search\Elastic\StorageMetadataFactory')
32
        );
33
        $definition->setPublic(false);
34
        $container->setDefinition($smfName, $definition);
35
36
        // Client
37
        return new Definition(
38
            Utility::getLibraryClass('Search\Elastic\Client'),
39
            [new Reference($smfName)]
40
        );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function load(array $config, ContainerBuilder $container)
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...
47
    {
48
        $managerDef = $container->getDefinition(Utility::getAliasedName('storage_manager'));
49
50
        foreach ($config['search_clients'] as $name => $clientConfig) {
51
            $clientName = Utility::getAliasedName(sprintf('search_client.%s', $name));
52
            if (isset($clientConfig['service'])) {
53
                // Custom search client.
54
                $container->setAlias($clientName, Utility::cleanServiceName($clientConfig['service']));
55
            } else {
56
                // Built-in client.
57
                switch ($clientConfig['type']) {
58
                    case 'elastic':
59
                        $definition = $this->createElasticClient($clientName, $clientConfig, $container);
60
                        break;
61
                    default:
62
                        throw new \RuntimeException(sprintf('The search client type "%s" is currently not supported.', $clientConfig['type']));
63
                }
64
                $container->setDefinition($clientName, $definition);
65
            }
66
            $managerDef->addMethodCall('addSearchClient', [new Reference($clientName)]);
67
        }
68
        return $this;
69
    }
70
}
71