Completed
Push — master ( eae980...a3dd6c )
by Eric
10s
created

ResourceExtension   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 319
Duplicated Lines 29.47 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.12%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 52
c 6
b 1
f 1
lcom 1
cbo 7
dl 94
loc 319
ccs 143
cts 170
cp 0.8412
rs 7.9487

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfiguration() 0 8 2
A getAlias() 0 4 1
A loadInternal() 0 16 3
A loadBundle() 0 3 1
B configureResource() 0 19 13
F loadResource() 54 70 20
B createResourceDefinition() 0 32 2
A createManagerAlias() 0 8 2
A createFactoryDefinition() 0 16 2
A createRepositoryDefinition() 8 8 1
A createFormDefinition() 11 11 1
A createChoiceFormDefinition() 10 10 1
A createDomainManagerDefinition() 0 13 1
A createControllerDefinition() 11 11 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ResourceExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ResourceExtension, and based on these observations, apply Extract Interface, too.

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 Lug\Component\Translation\Factory\TranslatableFactory;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Definition;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class ResourceExtension extends ConfigurableExtension
28
{
29
    /**
30
     * @var ResourceBundleInterface
31
     */
32
    private $bundle;
33
34
    /**
35
     * @param ResourceBundleInterface $bundle
36
     */
37 8
    public function __construct(ResourceBundleInterface $bundle)
38
    {
39 8
        $this->bundle = $bundle;
40 8
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 7
    public function getConfiguration(array $config, ContainerBuilder $container)
46
    {
47 7
        if (!class_exists($class = ClassUtils::getNamespace($this).'\\Configuration')) {
48
            $class = ResourceConfiguration::class;
49
        }
50
51 7
        return new $class($this->bundle);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 8
    public function getAlias()
58
    {
59 8
        return $this->bundle->getAlias();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 7
    protected function loadInternal(array $config, ContainerBuilder $container)
66
    {
67 7
        foreach ($this->bundle->getResources() as $resource) {
68 7
            $resourceConfig = $config['resources'][$resource->getName()];
69
70 7
            $this->configureResource($resource, $resourceConfig);
71 7
            $this->loadResource($resource, $container);
72
73 7
            if ($resource->getTranslation() !== null) {
74
                $this->configureResource($resource->getTranslation(), $resourceConfig['translation']);
0 ignored issues
show
Documentation introduced by
$resource->getTranslation() is of type null, but the function expects a object<Lug\Component\Res...odel\ResourceInterface>.

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...
75
                $this->loadResource($resource->getTranslation(), $container);
0 ignored issues
show
Documentation introduced by
$resource->getTranslation() is of type null, but the function expects a object<Lug\Component\Res...odel\ResourceInterface>.

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...
76
            }
77 7
        }
78
79 7
        $this->loadBundle($config, $container);
80 7
    }
81
82
    /**
83
     * @param mixed[]          $config
84
     * @param ContainerBuilder $container
85
     */
86
    protected function loadBundle(array $config, ContainerBuilder $container)
87
    {
88
    }
89
90
    /**
91
     * @param ResourceInterface $resource
92
     * @param mixed[]           $config
93
     */
94 7
    private function configureResource(ResourceInterface $resource, array $config)
95
    {
96 7
        $driverConfig = $config['driver'];
97 7
        $mappingConfig = $driverConfig['mapping'];
98
99 7
        $resource->setModel($config['model']);
100 7
        $resource->setDriver(isset($driverConfig['name']) ? $driverConfig['name'] : null);
101 7
        $resource->setDriverManager(isset($driverConfig['manager']) ? $driverConfig['manager'] : null);
102 7
        $resource->setDriverMappingPath(isset($mappingConfig['path']) ? $mappingConfig['path'] : null);
103 7
        $resource->setDriverMappingFormat(isset($mappingConfig['format']) ? $mappingConfig['format'] : null);
104 7
        $resource->setRepository(isset($config['repository']) ? $config['repository'] : null);
105 7
        $resource->setFactory(isset($config['factory']) ? $config['factory'] : null);
106 7
        $resource->setForm(isset($config['form']) ? $config['form'] : null);
107 7
        $resource->setChoiceForm(isset($config['choice_form']) ? $config['choice_form'] : null);
108 7
        $resource->setDomainManager(isset($config['domain_manager']) ? $config['domain_manager'] : null);
109 7
        $resource->setController(isset($config['controller']) ? $config['controller'] : null);
110 7
        $resource->setIdPropertyPath(isset($config['id_property_path']) ? $config['id_property_path'] : null);
111 7
        $resource->setLabelPropertyPath(isset($config['label_property_path']) ? $config['label_property_path'] : null);
112 7
    }
113
114
    /**
115
     * @param ResourceInterface $resource
116
     * @param ContainerBuilder  $container
117
     */
118 7
    private function loadResource(ResourceInterface $resource, ContainerBuilder $container)
119
    {
120 7
        $container->setDefinition('lug.resource.'.$resource->getName(), $this->createResourceDefinition($resource));
121
122 7
        if ($resource->getDriverManager() !== null) {
123 7
            $container->setAlias('lug.manager.'.$resource->getName(), $this->createManagerAlias($resource));
124 7
        }
125
126 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...
127 7
            $repository = 'lug.repository.'.$resource->getName();
128
129 7
            if (class_exists($resource->getRepository())) {
130 7
                $container->setDefinition($repository, $this->createRepositoryDefinition($resource));
131 7
            } elseif ($repository !== $resource->getRepository()) {
132
                $container->setAlias($repository, $resource->getRepository());
133
            }
134 7
        }
135
136 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...
137 7
            $factory = 'lug.factory.'.$resource->getName();
138
139 7
            if (class_exists($resource->getFactory())) {
140 7
                $container->setDefinition($factory, $this->createFactoryDefinition($resource));
141 7
            } elseif ($factory !== $resource->getFactory()) {
142
                $container->setAlias($factory, $resource->getFactory());
143
            }
144 7
        }
145
146 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...
147 7
            $form = 'lug.form.type.'.$resource->getName();
148
149 7
            if (class_exists($resource->getForm())) {
150 7
                $container->setDefinition($form, $this->createFormDefinition($resource));
151 7
            } elseif ($form !== $resource->getForm()) {
152
                $container->setAlias($form, $resource->getForm());
153
            }
154 7
        }
155
156 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...
157 7
            $choiceForm = 'lug.form.type.'.$resource->getName().'.choice';
158
159 7
            if (class_exists($resource->getChoiceForm())) {
160 7
                $container->setDefinition($choiceForm, $this->createChoiceFormDefinition($resource));
161 7
            } elseif ($choiceForm !== $resource->getChoiceForm()) {
162
                $container->setAlias($choiceForm, $resource->getChoiceForm());
163
            }
164 7
        }
165
166 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...
167 7
            $domainManager = 'lug.domain_manager.'.$resource->getName();
168
169 7
            if (class_exists($resource->getDomainManager())) {
170 7
                $container->setDefinition($domainManager, $this->createDomainManagerDefinition($resource));
171 7
            } elseif ($domainManager !== $resource->getDomainManager()) {
172
                $container->setAlias($domainManager, $resource->getDomainManager());
173
            }
174 7
        }
175
176 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...
177 7
            $controller = 'lug.controller.'.$resource->getName();
178
179 7
            if (class_exists($resource->getController())) {
180 7
                $container->setDefinition($controller, $this->createControllerDefinition($resource));
181 7
            } elseif ($controller !== $resource->getController()) {
182
                $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...
183
            }
184 7
        }
185
186 7
        $container->addObjectResource($resource);
187 7
    }
188
189
    /**
190
     * @param ResourceInterface $resource
191
     *
192
     * @return Definition
193
     */
194 7
    private function createResourceDefinition(ResourceInterface $resource)
195
    {
196 7
        $definition = new Definition(Resource::class, [
197 7
            $resource->getName(),
198 7
            $resource->getInterfaces(),
199 7
            $resource->getModel(),
200 7
        ]);
201
202
        $definition
203 7
            ->addMethodCall('setDriver', [$resource->getDriver()])
204 7
            ->addMethodCall('setDriverManager', [$resource->getDriverManager()])
205 7
            ->addMethodCall('setDriverMappingPath', [$resource->getDriverMappingPath()])
206 7
            ->addMethodCall('setDriverMappingFormat', [$resource->getDriverMappingFormat()])
207 7
            ->addMethodCall('setRepository', [$resource->getRepository()])
208 7
            ->addMethodCall('setFactory', [$resource->getFactory()])
209 7
            ->addMethodCall('setForm', [$resource->getForm()])
210 7
            ->addMethodCall('setChoiceForm', [$resource->getChoiceForm()])
211 7
            ->addMethodCall('setDomainManager', [$resource->getDomainManager()])
212 7
            ->addMethodCall('setController', [$resource->getController()])
213 7
            ->addMethodCall('setIdPropertyPath', [$resource->getIdPropertyPath()])
214 7
            ->addMethodCall('setLabelPropertyPath', [$resource->getLabelPropertyPath()])
215 7
            ->addTag('lug.resource');
216
217 7
        if ($resource->getTranslation() !== null) {
218
            $definition->addMethodCall(
219
                'setTranslation',
220
                [new Reference('lug.resource.'.$resource->getTranslation()->getName())]
221
            );
222
        }
223
224 7
        return $definition;
225
    }
226
227
    /**
228
     * @param ResourceInterface $resource
229
     *
230
     * @return string
231
     */
232 7
    private function createManagerAlias(ResourceInterface $resource)
233
    {
234 7
        if ($resource->getDriver() === ResourceInterface::DRIVER_DOCTRINE_MONGODB) {
235
            return 'doctrine_mongodb.odm.'.$resource->getDriverManager().'_document_manager';
236
        }
237
238 7
        return 'doctrine.orm.'.$resource->getDriverManager().'_entity_manager';
239
    }
240
241
    /**
242
     * @param ResourceInterface $resource
243
     *
244
     * @return Definition
245
     */
246 7
    private function createFactoryDefinition(ResourceInterface $resource)
247
    {
248
        $arguments = [
249 7
            new Reference('lug.resource.'.$resource->getName()),
250 7
            new Reference('property_accessor'),
251 7
        ];
252
253 7
        if (is_a($resource->getFactory(), TranslatableFactory::class, true)) {
254
            $arguments[] = new Reference('lug.translation.context.locale');
255
        }
256
257 7
        $definition = new Definition($resource->getFactory(), $arguments);
258 7
        $definition->addTag('lug.factory', ['resource' => $resource->getName()]);
259
260 7
        return $definition;
261
    }
262
263
    /**
264
     * @param ResourceInterface $resource
265
     *
266
     * @return Definition
267
     */
268 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...
269
    {
270 7
        $definition = new Definition($resource->getRepository(), [$resource->getModel()]);
271 7
        $definition->setFactory([new Reference('lug.manager.'.$resource->getName()), 'getRepository']);
272 7
        $definition->addTag('lug.repository', ['resource' => $resource->getName()]);
273
274 7
        return $definition;
275
    }
276
277
    /**
278
     * @param ResourceInterface $resource
279
     *
280
     * @return Definition
281
     */
282 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...
283
    {
284 7
        $definition = new Definition($resource->getForm(), [
285 7
            new Reference('lug.resource.'.$resource->getName()),
286 7
            new Reference('lug.factory.'.$resource->getName()),
287 7
        ]);
288
289 7
        $definition->addTag('form.type');
290
291 7
        return $definition;
292
    }
293
294
    /**
295
     * @param ResourceInterface $resource
296
     *
297
     * @return Definition
298
     */
299 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...
300
    {
301 7
        $definition = new Definition($resource->getChoiceForm(), [
302 7
            new Reference('lug.resource.'.$resource->getName()),
303 7
        ]);
304
305 7
        $definition->addTag('form.type');
306
307 7
        return $definition;
308
    }
309
310
    /**
311
     * @param ResourceInterface $resource
312
     *
313
     * @return Definition
314
     */
315 7
    private function createDomainManagerDefinition(ResourceInterface $resource)
316
    {
317 7
        $definition = new Definition($resource->getDomainManager(), [
318 7
            new Reference('lug.resource.'.$resource->getName()),
319 7
            new Reference('lug.resource.domain.event_dispatcher'),
320 7
            new Reference('lug.manager.'.$resource->getName()),
321 7
            new Reference('lug.repository.'.$resource->getName()),
322 7
        ]);
323
324 7
        $definition->addTag('lug.domain_manager', ['resource' => $resource->getName()]);
325
326 7
        return $definition;
327
    }
328
329
    /**
330
     * @param ResourceInterface $resource
331
     *
332
     * @return Definition
333
     */
334 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...
335
    {
336 7
        $definition = new Definition($resource->getController(), [
337 7
            new Reference('lug.resource.'.$resource->getName()),
338 7
        ]);
339
340 7
        $definition->addMethodCall('setContainer', [new Reference('service_container')]);
341 7
        $definition->addTag('lug.controller', ['resource' => $resource->getName()]);
342
343 7
        return $definition;
344
    }
345
}
346