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

SearchClients::load()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
rs 8.6845
cc 4
eloc 16
nc 4
nop 2
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