ResourceExtension::createChoiceFormDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\DependencyInjection\Extension;
13
14
use Lug\Bundle\ResourceBundle\ResourceBundleInterface;
15
use Lug\Bundle\ResourceBundle\Util\ClassUtils;
16
use Lug\Component\Resource\Model\Resource;
17
use Lug\Component\Resource\Model\ResourceInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
22
23
/**
24
 * @author GeLo <[email protected]>
25
 */
26
class ResourceExtension extends ConfigurableExtension
27
{
28
    /**
29
     * @var ResourceBundleInterface
30
     */
31
    private $bundle;
32
33
    /**
34
     * @param ResourceBundleInterface $bundle
35
     */
36 8
    public function __construct(ResourceBundleInterface $bundle)
37
    {
38 8
        $this->bundle = $bundle;
39 8
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 7
    public function getConfiguration(array $config, ContainerBuilder $container)
45
    {
46 7
        if (!class_exists($class = ClassUtils::getNamespace($this).'\\Configuration')) {
47
            $class = ResourceConfiguration::class;
48
        }
49
50 7
        return new $class($this->bundle);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 8
    public function getAlias()
57
    {
58 8
        return $this->bundle->getAlias();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 7
    protected function loadInternal(array $config, ContainerBuilder $container)
65
    {
66 7
        foreach ($this->bundle->getResources() as $resource) {
67 7
            $resourceConfig = $config['resources'][$resource->getName()];
68
69 7
            $this->configureResource($resource, $resourceConfig);
70 7
            $this->loadResource($resource, $container);
71
72 7
            foreach ($resource->getRelations() as $name => $relation) {
73
                $this->configureResource($relation, $resourceConfig[$name]);
74
                $this->loadResource($relation, $container);
75 7
            }
76 7
        }
77
78 7
        $this->loadBundle($config, $container);
79 7
    }
80
81
    /**
82
     * @param mixed[]          $config
83
     * @param ContainerBuilder $container
84
     */
85
    protected function loadBundle(array $config, ContainerBuilder $container)
86
    {
87
    }
88
89
    /**
90
     * @param ResourceInterface $resource
91
     * @param mixed[]           $config
92
     */
93 7
    private function configureResource(ResourceInterface $resource, array $config)
94
    {
95 7
        $driverConfig = $config['driver'];
96 7
        $mappingConfig = $driverConfig['mapping'];
97
98 7
        $resource->setModel($config['model']);
99 7
        $resource->setDriver(isset($driverConfig['name']) ? $driverConfig['name'] : null);
100 7
        $resource->setDriverManager(isset($driverConfig['manager']) ? $driverConfig['manager'] : null);
101 7
        $resource->setDriverMappingPath(isset($mappingConfig['path']) ? $mappingConfig['path'] : null);
102 7
        $resource->setDriverMappingFormat(isset($mappingConfig['format']) ? $mappingConfig['format'] : null);
103 7
        $resource->setRepository(isset($config['repository']) ? $config['repository'] : null);
104 7
        $resource->setFactory(isset($config['factory']) ? $config['factory'] : null);
105 7
        $resource->setForm(isset($config['form']) ? $config['form'] : null);
106 7
        $resource->setChoiceForm(isset($config['choice_form']) ? $config['choice_form'] : null);
107 7
        $resource->setDomainManager(isset($config['domain_manager']) ? $config['domain_manager'] : null);
108 7
        $resource->setController(isset($config['controller']) ? $config['controller'] : null);
109 7
        $resource->setIdPropertyPath(isset($config['id_property_path']) ? $config['id_property_path'] : null);
110 7
        $resource->setLabelPropertyPath(isset($config['label_property_path']) ? $config['label_property_path'] : null);
111 7
    }
112
113
    /**
114
     * @param ResourceInterface $resource
115
     * @param ContainerBuilder  $container
116
     */
117 7
    private function loadResource(ResourceInterface $resource, ContainerBuilder $container)
118
    {
119 7
        $container->setDefinition('lug.resource.'.$resource->getName(), $this->createResourceDefinition($resource));
120
121 7
        if ($resource->getDriverManager() !== null) {
122 7
            $container->setAlias('lug.manager.'.$resource->getName(), $this->createManagerAlias($resource));
123 7
        }
124
125 7 View Code Duplication
        if ($resource->getRepository() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126 7
            $repository = 'lug.repository.'.$resource->getName();
127
128 7
            if (class_exists($resource->getRepository())) {
129 7
                $container->setDefinition($repository, $this->createRepositoryDefinition($resource));
130 7
            } elseif ($repository !== $resource->getRepository()) {
131
                $container->setAlias($repository, $resource->getRepository());
132
            }
133 7
        }
134
135 7 View Code Duplication
        if ($resource->getFactory() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
136 7
            $factory = 'lug.factory.'.$resource->getName();
137
138 7
            if (class_exists($resource->getFactory())) {
139 7
                $container->setDefinition($factory, $this->createFactoryDefinition($resource));
140 7
            } elseif ($factory !== $resource->getFactory()) {
141
                $container->setAlias($factory, $resource->getFactory());
142
            }
143 7
        }
144
145 7 View Code Duplication
        if ($resource->getForm() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
146 7
            $form = 'lug.form.type.'.$resource->getName();
147
148 7
            if (class_exists($resource->getForm())) {
149 7
                $container->setDefinition($form, $this->createFormDefinition($resource));
150 7
            } elseif ($form !== $resource->getForm()) {
151
                $container->setAlias($form, $resource->getForm());
152
            }
153 7
        }
154
155 7 View Code Duplication
        if ($resource->getChoiceForm() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
156 7
            $choiceForm = 'lug.form.type.'.$resource->getName().'.choice';
157
158 7
            if (class_exists($resource->getChoiceForm())) {
159 7
                $container->setDefinition($choiceForm, $this->createChoiceFormDefinition($resource));
160 7
            } elseif ($choiceForm !== $resource->getChoiceForm()) {
161
                $container->setAlias($choiceForm, $resource->getChoiceForm());
162
            }
163 7
        }
164
165 7 View Code Duplication
        if ($resource->getDomainManager() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166 7
            $domainManager = 'lug.domain_manager.'.$resource->getName();
167
168 7
            if (class_exists($resource->getDomainManager())) {
169 7
                $container->setDefinition($domainManager, $this->createDomainManagerDefinition($resource));
170 7
            } elseif ($domainManager !== $resource->getDomainManager()) {
171
                $container->setAlias($domainManager, $resource->getDomainManager());
172
            }
173 7
        }
174
175 7 View Code Duplication
        if ($resource->getController() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
176 7
            $controller = 'lug.controller.'.$resource->getName();
177
178 7
            if (class_exists($resource->getController())) {
179 7
                $container->setDefinition($controller, $this->createControllerDefinition($resource));
180 7
            } elseif ($controller !== $resource->getController()) {
181
                $container->setAlias($container, $resource->getController());
0 ignored issues
show
Documentation introduced by
$container is of type object<Symfony\Component...ction\ContainerBuilder>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
            }
183 7
        }
184
185 7
        $container->addObjectResource($resource);
186 7
    }
187
188
    /**
189
     * @param ResourceInterface $resource
190
     *
191
     * @return Definition
192
     */
193 7
    private function createResourceDefinition(ResourceInterface $resource)
194
    {
195 7
        $definition = new Definition(Resource::class, [
196 7
            $resource->getName(),
197 7
            $resource->getInterfaces(),
198 7
            $resource->getModel(),
199 7
        ]);
200
201
        $definition
202 7
            ->addMethodCall('setDriver', [$resource->getDriver()])
203 7
            ->addMethodCall('setDriverManager', [$resource->getDriverManager()])
204 7
            ->addMethodCall('setDriverMappingPath', [$resource->getDriverMappingPath()])
205 7
            ->addMethodCall('setDriverMappingFormat', [$resource->getDriverMappingFormat()])
206 7
            ->addMethodCall('setRepository', [$resource->getRepository()])
207 7
            ->addMethodCall('setFactory', [$resource->getFactory()])
208 7
            ->addMethodCall('setForm', [$resource->getForm()])
209 7
            ->addMethodCall('setChoiceForm', [$resource->getChoiceForm()])
210 7
            ->addMethodCall('setDomainManager', [$resource->getDomainManager()])
211 7
            ->addMethodCall('setController', [$resource->getController()])
212 7
            ->addMethodCall('setIdPropertyPath', [$resource->getIdPropertyPath()])
213 7
            ->addMethodCall('setLabelPropertyPath', [$resource->getLabelPropertyPath()])
214 7
            ->addTag('lug.resource');
215
216 7
        foreach ($resource->getRelations() as $name => $relation) {
217
            $definition->addMethodCall('addRelation', [$name, new Reference('lug.resource.'.$relation->getName())]);
218 7
        }
219
220 7
        return $definition;
221
    }
222
223
    /**
224
     * @param ResourceInterface $resource
225
     *
226
     * @return string
227
     */
228 7
    private function createManagerAlias(ResourceInterface $resource)
229
    {
230 7
        return $resource->getDriver() === ResourceInterface::DRIVER_DOCTRINE_MONGODB
231 7
            ? 'doctrine_mongodb.odm.'.$resource->getDriverManager().'_document_manager'
232 7
            : 'doctrine.orm.'.$resource->getDriverManager().'_entity_manager';
233
    }
234
235
    /**
236
     * @param ResourceInterface $resource
237
     *
238
     * @return Definition
239
     */
240 7 View Code Duplication
    private function createFactoryDefinition(ResourceInterface $resource)
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...
241
    {
242 7
        $definition = new Definition($resource->getFactory(), [
243 7
            new Reference('lug.resource.'.$resource->getName()),
244 7
            new Reference('property_accessor'),
245 7
        ]);
246
247 7
        $definition->addTag('lug.factory', ['resource' => $resource->getName()]);
248
249 7
        return $definition;
250
    }
251
252
    /**
253
     * @param ResourceInterface $resource
254
     *
255
     * @return Definition
256
     */
257 7 View Code Duplication
    private function createRepositoryDefinition(ResourceInterface $resource)
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...
258
    {
259 7
        $definition = new Definition($resource->getRepository(), [$resource->getModel()]);
260 7
        $definition->setFactory([new Reference('lug.manager.'.$resource->getName()), 'getRepository']);
261 7
        $definition->addTag('lug.repository', ['resource' => $resource->getName()]);
262
263 7
        return $definition;
264
    }
265
266
    /**
267
     * @param ResourceInterface $resource
268
     *
269
     * @return Definition
270
     */
271 7 View Code Duplication
    private function createFormDefinition(ResourceInterface $resource)
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...
272
    {
273 7
        $definition = new Definition($resource->getForm(), [
274 7
            new Reference('lug.resource.'.$resource->getName()),
275 7
            new Reference('lug.factory.'.$resource->getName()),
276 7
        ]);
277
278 7
        $definition->addTag('form.type');
279
280 7
        return $definition;
281
    }
282
283
    /**
284
     * @param ResourceInterface $resource
285
     *
286
     * @return Definition
287
     */
288 7 View Code Duplication
    private function createChoiceFormDefinition(ResourceInterface $resource)
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...
289
    {
290 7
        $definition = new Definition($resource->getChoiceForm(), [
291 7
            new Reference('lug.resource.'.$resource->getName()),
292 7
        ]);
293
294 7
        $definition->addTag('form.type');
295
296 7
        return $definition;
297
    }
298
299
    /**
300
     * @param ResourceInterface $resource
301
     *
302
     * @return Definition
303
     */
304 7
    private function createDomainManagerDefinition(ResourceInterface $resource)
305
    {
306 7
        $definition = new Definition($resource->getDomainManager(), [
307 7
            new Reference('lug.resource.'.$resource->getName()),
308 7
            new Reference('lug.resource.domain.event_dispatcher'),
309 7
            new Reference('lug.manager.'.$resource->getName()),
310 7
            new Reference('lug.repository.'.$resource->getName()),
311 7
        ]);
312
313 7
        $definition->addTag('lug.domain_manager', ['resource' => $resource->getName()]);
314
315 7
        return $definition;
316
    }
317
318
    /**
319
     * @param ResourceInterface $resource
320
     *
321
     * @return Definition
322
     */
323 7 View Code Duplication
    private function createControllerDefinition(ResourceInterface $resource)
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...
324
    {
325 7
        $definition = new Definition($resource->getController(), [
326 7
            new Reference('lug.resource.'.$resource->getName()),
327 7
        ]);
328
329 7
        $definition->addMethodCall('setContainer', [new Reference('service_container')]);
330 7
        $definition->addTag('lug.controller', ['resource' => $resource->getName()]);
331
332 7
        return $definition;
333
    }
334
}
335