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

Persisters::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 persister services.
12
 *
13
 * @author  Jacob Bare <[email protected]>
14
 */
15
class Persisters implements ServiceLoaderInterface
16
{
17
    /**
18
     * Creates the MongoDB persister service definition.
19
     * Will also load support services.
20
     *
21
     * @param   string              $persisterName
22
     * @param   array               $persisterConfig
23
     * @param   ContainerBuilder    $container
24
     * @return  Definition
25
     */
26
    private function createMongoDbPersister($persisterName, array $persisterConfig, ContainerBuilder $container)
27
    {
28
        // Storage metadata
29
        $smfName = sprintf('%s.metadata', $persisterName);
30
        $definition = new Definition(
31
            Utility::getLibraryClass('Persister\MongoDb\StorageMetadataFactory'),
32
            [new Reference(Utility::getAliasedName('util.entity'))]
33
        );
34
        $definition->setPublic(false);
35
        $container->setDefinition($smfName, $definition);
36
37
        // Connection
38
        $conName = sprintf('%s.connection', $persisterName);
39
        $options = isset($persisterConfig['parameters']['options']) && is_array($persisterConfig['parameters']['options']) ? $persisterConfig['parameters']['options'] : [];
40
        $definition = new Definition(
41
            'Doctrine\MongoDB\Connection',
42
            [$persisterConfig['parameters']['host'], $options]
43
        );
44
        $definition->setPublic(false);
45
        $container->setDefinition($conName, $definition);
46
47
        // Persister
48
        return new Definition(
49
            Utility::getLibraryClass('Persister\MongoDb\Persister'),
50
            [new Reference($conName), new Reference($smfName)]
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 View Code Duplication
    public function load(array $config, ContainerBuilder $container)
1 ignored issue
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...
58
    {
59
        $managerDef = $container->getDefinition(Utility::getAliasedName('storage_manager'));
60
61
        foreach ($config['persisters'] as $name => $persisterConfig) {
62
            $persisterName = Utility::getAliasedName(sprintf('persister.%s', $name));
63
            if (isset($persisterConfig['service'])) {
64
                // Custom persister.
65
                $container->setAlias($persisterName, Utility::cleanServiceName($persisterConfig['service']));
66
            } else {
67
                // Built-in persister.
68
                switch ($persisterConfig['type']) {
69
                    case 'mongodb':
70
                        $definition = $this->createMongoDbPersister($persisterName, $persisterConfig, $container);
71
                        break;
72
                    default:
73
                        throw new \RuntimeException(sprintf('The persister type "%s" is currently not supported.', $persisterConfig['type']));
74
                }
75
                $container->setDefinition($persisterName, $definition);
76
            }
77
            $managerDef->addMethodCall('addPersister', [new Reference($persisterName)]);
78
        }
79
        return $this;
80
    }
81
}
82