Completed
Push — master ( 91e054...12ace7 )
by Kamil
31:08 queued 26:39
created

AbstractDriver::addController()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 25
rs 8.8571
cc 1
eloc 20
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver;
13
14
use Sylius\Component\Resource\Factory\Factory;
15
use Sylius\Component\Resource\Metadata\Metadata;
16
use Sylius\Component\Resource\Metadata\MetadataInterface;
17
use Sylius\Component\Translation\Factory\TranslatableFactoryInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Parameter;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * @author Paweł Jędrzejewski <[email protected]>
25
 * @author Arnaud Langlade <[email protected]>
26
 */
27
abstract class AbstractDriver implements DriverInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(ContainerBuilder $container, MetadataInterface $metadata)
33
    {
34
        $this->setClassesParameters($container, $metadata);
35
36
        if ($metadata->hasClass('controller')) {
37
            $this->addController($container, $metadata);
38
        }
39
40
        $this->addManager($container, $metadata);
41
        $this->addRepository($container, $metadata);
42
43
        if ($metadata->hasClass('factory')) {
44
            $this->addFactory($container, $metadata);
45
        }
46
47
        if ($metadata->hasClass('form')) {
48
            $this->addForms($container, $metadata);
49
        }
50
    }
51
52
    /**
53
     * @param ContainerBuilder $container
54
     * @param MetadataInterface $metadata
55
     */
56
    protected function setClassesParameters(ContainerBuilder $container, MetadataInterface $metadata)
57
    {
58
        if ($metadata->hasClass('model')) {
59
            $container->setParameter(sprintf('%s.model.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('model'));
60
        }
61
        if ($metadata->hasClass('controller')) {
62
            $container->setParameter(sprintf('%s.controller.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('controller'));
63
        }
64
        if ($metadata->hasClass('factory')) {
65
            $container->setParameter(sprintf('%s.factory.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('factory'));
66
        }
67
        if ($metadata->hasClass('repository')) {
68
            $container->setParameter(sprintf('%s.repository.%s.class', $metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('repository'));
69
        }
70
71
        if (!$metadata->hasParameter('validation_groups')) {
72
            return;
73
        }
74
75
        $validationGroups = $metadata->getParameter('validation_groups');
76
77
        foreach ($validationGroups as $formName => $groups) {
0 ignored issues
show
Bug introduced by
The expression $validationGroups of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
78
            $suffix = 'default' === $formName ? '' : sprintf('_%s', $formName);
79
            $container->setParameter(sprintf('%s.validation_groups.%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix), array_merge(array('Default'), $groups));
80
        }
81
    }
82
83
    /**
84
     * @param ContainerBuilder $container
85
     * @param MetadataInterface $metadata
86
     */
87
    protected function addController(ContainerBuilder $container, MetadataInterface $metadata)
88
    {
89
        $definition = new Definition($metadata->getClass('controller'));
90
        $definition
91
            ->setArguments(array(
92
                $this->getMetdataDefinition($metadata),
93
                new Reference('sylius.resource_controller.request_configuration_factory'),
94
                new Reference('sylius.resource_controller.view_handler'),
95
                new Reference($metadata->getServiceId('repository')),
96
                new Reference($metadata->getServiceId('factory')),
97
                new Reference('sylius.resource_controller.new_resource_factory'),
98
                new Reference($metadata->getServiceId('manager')),
99
                new Reference('sylius.resource_controller.single_resource_provider'),
100
                new Reference('sylius.resource_controller.resources_collection_provider'),
101
                new Reference('sylius.resource_controller.form_factory'),
102
                new Reference('sylius.resource_controller.redirect_handler'),
103
                new Reference('sylius.resource_controller.flash_helper'),
104
                new Reference('sylius.resource_controller.authorization_checker'),
105
                new Reference('sylius.resource_controller.event_dispatcher'),
106
            ))
107
            ->addMethodCall('setContainer', array(new Reference('service_container')))
108
        ;
109
110
        $container->setDefinition($metadata->getServiceId('controller'), $definition);
111
    }
112
113
    /**
114
     * @param ContainerBuilder $container
115
     * @param MetadataInterface $metadata
116
     */
117
    protected function addFactory(ContainerBuilder $container, MetadataInterface $metadata)
118
    {
119
        $translatableFactoryInterface = TranslatableFactoryInterface::class;
120
121
        $factoryClass = $metadata->getClass('factory');
122
        $modelClass = $metadata->getClass('model');
123
124
        $reflection = new \ReflectionClass($factoryClass);
125
        $definition = new Definition($factoryClass);
126
127
        if (interface_exists($translatableFactoryInterface) && $reflection->implementsInterface($translatableFactoryInterface)) {
128
            $decoratedDefinition = new Definition(Factory::class);
129
            $decoratedDefinition->setArguments(array($modelClass));
130
131
            $definition->setArguments(array($decoratedDefinition, new Reference('sylius.translation.locale_provider')));
132
133
            $container->setDefinition($metadata->getServiceId('factory'), $definition);
134
135
            return;
136
        }
137
138
        $definition->setArguments(array($modelClass));
139
140
        $container->setDefinition($metadata->getServiceId('factory'), $definition);
141
    }
142
143
    /**
144
     * @param ContainerBuilder $container
145
     * @param MetadataInterface $metadata
146
     */
147
    protected function addForms(ContainerBuilder $container, MetadataInterface $metadata)
148
    {
149
        foreach ($metadata->getClass('form') as $formName => $formClass) {
0 ignored issues
show
Bug introduced by
The expression $metadata->getClass('form') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
150
            $suffix = 'default' === $formName ? '' : sprintf('_%s', $formName);
151
            $alias = sprintf('%s_%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix);
152
153
            $definition = new Definition($formClass);
154
155
            switch ($formName) {
156
                case 'choice':
157
                    $definition->setArguments(array(
158
                        $metadata->getClass('model'),
159
                        $metadata->getDriver(),
160
                        $alias,
161
                    ));
162
                break;
163
164
                default:
165
                    $validationGroupsParameterName = sprintf('%s.validation_groups.%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix);
166
                    $validationGroups = new Parameter($validationGroupsParameterName);
167
168
                    if (!$container->hasParameter($validationGroupsParameterName)) {
169
                        $validationGroups = array('Default');
170
                    }
171
172
                    $definition->setArguments(array(
173
                        $metadata->getClass('model'),
174
                        $validationGroups
175
                    ));
176
                break;
177
            }
178
179
            $definition->addTag('form.type', array('alias' => $alias));
180
181
            $container->setParameter(sprintf('%s.form.type.%s%s.class', $metadata->getApplicationName(), $metadata->getName(), $suffix), $formClass);
182
            $container->setDefinition(
183
                sprintf('%s.form.type.%s%s', $metadata->getApplicationName(), $metadata->getName(), $suffix),
184
                $definition
185
            );
186
        }
187
188
        if (!$container->hasDefinition(sprintf('%s.form.type.%s', $metadata->getApplicationName(), $metadata->getName()))) {
189
            $this->addDefaultForm($container, $metadata);
190
        }
191
    }
192
193
    /**
194
     * @param MetadataInterface $metadata
195
     *
196
     * @return Definition
197
     */
198
    protected function getMetdataDefinition(MetadataInterface $metadata)
199
    {
200
        $definition = new Definition(Metadata::class);
201
        $definition
202
            ->setFactory(array(new Reference('sylius.resource_registry'), 'get'))
203
            ->setArguments(array($metadata->getAlias()))
204
        ;
205
206
        return $definition;
207
208
    }
209
210
    /**
211
     * @param ContainerBuilder $container
212
     * @param MetadataInterface $metadata
213
     */
214
    abstract protected function addDefaultForm(ContainerBuilder $container, MetadataInterface $metadata);
215
216
    /**
217
     * @param ContainerBuilder $container
218
     * @param MetadataInterface $metadata
219
     */
220
    abstract protected function addManager(ContainerBuilder $container, MetadataInterface $metadata);
221
222
    /**
223
     * @param ContainerBuilder $container
224
     * @param MetadataInterface $metadata
225
     */
226
    abstract protected function addRepository(ContainerBuilder $container, MetadataInterface $metadata);
227
}
228