Completed
Pull Request — master (#11)
by Jacob
03:06
created

Persisters::createSmf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
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
     * {@inheritdoc}
19
     */
20 View Code Duplication
    public function load(array $config, ContainerBuilder $container)
21
    {
22
        $managerDef = $container->getDefinition(Utility::getAliasedName('storage_manager'));
23
24
        foreach ($config['persisters'] as $name => $persisterConfig) {
25
            $persisterName = Utility::getAliasedName(sprintf('persister.%s', $name));
26
            if (isset($persisterConfig['service'])) {
27
                // Custom persister.
28
                $container->setAlias($persisterName, Utility::cleanServiceName($persisterConfig['service']));
29
            } else {
30
                // Built-in persister.
31
                switch ($persisterConfig['type']) {
32
                    case 'mongodb':
33
                        $definition = $this->createMongoDbPersister($persisterName, $persisterConfig, $container);
34
                        break;
35
                    default:
36
                        throw new \RuntimeException(sprintf('The persister type "%s" is currently not supported.', $persisterConfig['type']));
37
                }
38
                $container->setDefinition($persisterName, $definition);
39
            }
40
            $managerDef->addMethodCall('addPersister', [new Reference($persisterName)]);
41
        }
42
        return $this;
43
    }
44
45
    /**
46
     * Creates the connection service definition.
47
     *
48
     * @param   array   $persisterConfig
49
     * @return  Definition
50
     */
51
    private function createConnection(array $persisterConfig)
52
    {
53
        $options = isset($persisterConfig['parameters']['options']) && is_array($persisterConfig['parameters']['options']) ? $persisterConfig['parameters']['options'] : [];
54
        $definition = new Definition(
55
            'Doctrine\MongoDB\Connection',
56
            [$persisterConfig['parameters']['host'], $options]
57
        );
58
        $definition->setPublic(false);
59
        return $definition;
60
    }
61
62
    /**
63
     * Creates the persistence formatter service definition.
64
     *
65
     * @return  Definition
66
     */
67
    private function createFormatter()
68
    {
69
        $definition = new Definition(
70
            Utility::getLibraryClass('Persister\MongoDb\Formatter')
71
        );
72
        $definition->setPublic(false);
73
        return $definition;
74
    }
75
76
    /**
77
     * Creates the MongoDB persister service definition.
78
     * Will also load support services.
79
     *
80
     * @param   string              $persisterName
81
     * @param   array               $persisterConfig
82
     * @param   ContainerBuilder    $container
83
     * @return  Definition
84
     */
85
    private function createMongoDbPersister($persisterName, array $persisterConfig, ContainerBuilder $container)
86
    {
87
        // Storage metadata
88
        $smfName = sprintf('%s.metadata', $persisterName);
89
        $definition = $this->createSmf();
90
        $container->setDefinition($smfName, $definition);
91
92
        // Connection
93
        $conName = sprintf('%s.connection', $persisterName);
94
        $definition = $this->createConnection($persisterConfig);
95
        $container->setDefinition($conName, $definition);
96
97
        // Formatter
98
        $formatterName = sprintf('%s.formatter', $persisterName);
99
        $definition = $this->createFormatter();
100
        $container->setDefinition($formatterName, $definition);
101
102
        // Query
103
        $queryName = sprintf('%s.query', $persisterName);
104
        $definition = $this->createQuery($conName, $formatterName);
105
        $container->setDefinition($queryName, $definition);
106
107
        // Persister
108
        return new Definition(
109
            Utility::getLibraryClass('Persister\MongoDb\Persister'),
110
            [new Reference($queryName), new Reference($smfName)]
111
        );
112
    }
113
114
    /**
115
     * Creates the storage metadata factory service definition.
116
     *
117
     * @return  Definition
118
     */
119 View Code Duplication
    private function createSmf()
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...
120
    {
121
        $definition = new Definition(
122
            Utility::getLibraryClass('Persister\MongoDb\StorageMetadataFactory'),
123
            [new Reference(Utility::getAliasedName('util.entity'))]
124
        );
125
        $definition->setPublic(false);
126
        return $definition;
127
    }
128
129
    private function createQuery($conName, $formatterName)
130
    {
131
        $definition = new Definition(
132
            Utility::getLibraryClass('Persister\MongoDb\Query'),
133
            [new Reference($conName), new Reference($formatterName)]
134
        );
135
        return $definition;
136
    }
137
}
138