Completed
Pull Request — master (#47)
by Eric
32:49
created

ResourceExtension::createControllerDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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