Completed
Push — 0.3-dev ( 29e489...87cbea )
by Arnaud
03:16
created

ActionFactory::configureOptionsResolver()   D

Complexity

Conditions 13
Paths 4

Size

Total Lines 94
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 0
Metric Value
c 9
b 1
f 0
dl 0
loc 94
rs 4.9922
cc 13
eloc 66
nc 4
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LAG\AdminBundle\Admin\Factory;
4
5
use Exception;
6
use LAG\AdminBundle\Admin\Action;
7
use LAG\AdminBundle\Admin\Admin;
8
use LAG\AdminBundle\Admin\AdminInterface;
9
use LAG\AdminBundle\Admin\Configuration\ActionConfiguration;
10
use LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration;
11
use LAG\AdminBundle\Field\Factory\FieldFactory;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
class ActionFactory
16
{
17
    /**
18
     * @var FieldFactory
19
     */
20
    protected $fieldFactory;
21
22
    /**
23
     * @var FilterFactory
24
     */
25
    protected $filterFactory;
26
27
    /**
28
     * @var ApplicationConfiguration
29
     */
30
    protected $configuration;
31
32
    public function __construct(
33
        FieldFactory $fieldFactory,
34
        FilterFactory $filterFactory,
35
        ApplicationConfiguration $configuration
36
    )
37
    {
38
        $this->fieldFactory = $fieldFactory;
39
        $this->filterFactory = $filterFactory;
40
        $this->configuration = $configuration;
41
    }
42
43
    /**
44
     * Create an Action from configuration values.
45
     *
46
     * @param string $actionName
47
     * @param array $actionConfiguration
48
     * @param AdminInterface $admin
49
     *
50
     * @return Action
51
     */
52
    public function create($actionName, array $actionConfiguration, AdminInterface $admin)
53
    {
54
        // resolving default options
55
        $resolver = new OptionsResolver();
56
        $this->configureOptionsResolver($resolver, $actionName, $admin);
0 ignored issues
show
Documentation introduced by
$admin is of type object<LAG\AdminBundle\Admin\AdminInterface>, but the function expects a null|object<LAG\AdminBundle\Admin\Admin>.

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...
57
        $actionConfiguration = $resolver->resolve($actionConfiguration);
58
59
        // creating action object from configuration
60
        $action = $this->createActionFromConfiguration($actionConfiguration, $actionName);
61
62
        // creating actions linked to current action
63
        foreach ($actionConfiguration['actions'] as $customActionName => $customActionConfiguration) {
64
            // resolve configuration
65
            $customActionConfiguration = $resolver->resolve($customActionConfiguration);
66
            // create action
67
            $customAction = $this->createActionFromConfiguration($customActionConfiguration, $customActionName);
68
            // add to the main action
69
            $action->addAction($customAction);
70
        }
71
72
        // adding fields items to actions
73
        foreach ($actionConfiguration['fields'] as $fieldName => $fieldConfiguration) {
74
            $field = $this
75
                ->fieldFactory
76
                ->create($fieldName, $fieldConfiguration);
77
            $action->addField($field);
78
        }
79
80
        // adding filters to the action
81
        foreach ($actionConfiguration['filters'] as $fieldName => $filterConfiguration) {
82
            $filter = $this
83
                ->filterFactory
84
                ->create($fieldName, $filterConfiguration);
85
            $action->addFilter($filter);
86
        }
87
        return $action;
88
    }
89
90
    /**
91
     * Create an action and its configuration object from configuration values
92
     *
93
     * @param array $actionConfiguration
94
     * @param $actionName
95
     * @return Action
96
     */
97
    protected function createActionFromConfiguration(array $actionConfiguration, $actionName)
98
    {
99
        $configuration = new ActionConfiguration($actionConfiguration);
100
        $action = new Action($actionName, $actionConfiguration, $configuration);
101
102
        return $action;
103
    }
104
105
    /**
106
     * Return action configuration resolver
107
     *
108
     * @param OptionsResolver $resolver
109
     * @param $actionName
110
     * @param Admin|null $admin
111
     */
112
    protected function configureOptionsResolver(OptionsResolver $resolver, $actionName, Admin $admin = null)
113
    {
114
        $defaultCriteria = [];
115
        $defaultLoadStrategy = Admin::LOAD_STRATEGY_UNIQUE;
116
117
        if ($actionName == 'edit') {
118
            $defaultCriteria = [
119
                'id'
120
            ];
121
        } else if ($actionName == 'delete') {
122
            $defaultCriteria = [
123
                'id'
124
            ];
125
        } else if ($actionName == 'create') {
126
            $defaultLoadStrategy = Admin::LOAD_STRATEGY_NONE;
127
        }
128
129
        $resolver
130
            ->setDefaults([
131
                'title' => null,
132
                'fields' => [
133
                    'id' => [],
134
                ],
135
                'permissions' => ['ROLE_ADMIN'],
136
                'export' => [
137
                    'json',
138
                    'html',
139
                    'csv',
140
                    'xls'
141
                ],
142
                'order' => [],
143
                'actions' => [],
144
                'submit_actions' => [],
145
                'target' => '_self',
146
                'route' => '',
147
                'parameters' => [],
148
                'icon' => null,
149
                'filters' => [],
150
                'batch' => [],
151
                'load_strategy' => $defaultLoadStrategy,
152
                'pager' => 'pagerfanta',
153
                'criteria' => $defaultCriteria
154
            ])
155
            ->setAllowedValues('pager', [
156
                null,
157
                'pagerfanta',
158
            ])
159
            ->setAllowedValues('load_strategy', [
160
                Admin::LOAD_STRATEGY_NONE,
161
                Admin::LOAD_STRATEGY_UNIQUE,
162
                Admin::LOAD_STRATEGY_MULTIPLE,
163
            ])
164
            ->setNormalizer('route', function (Options $options, $value) use ($admin, $actionName) {
165
                if (!$value) {
166
                    // if no route was provided, it should be linked to an Admin
167
                    if (!$admin) {
168
                        throw new Exception('No route was provided for action : ' . $actionName);
169
                    }
170
                    return $admin
171
                        ->generateRouteName($actionName);
172
                }
173
                return $value;
174
            })
175
            ->setNormalizer('title', function (Options $options, $value) use ($admin, $actionName) {
176
                if (!$value) {
177
                    $adminKey = '';
178
                    // if an Admin is linked to this action, we use its name in translation key
179
                    if ($admin) {
180
                        $adminKey = $admin->getName();
181
                    }
182
                    return $this->configuration->getTranslationKey($actionName, $adminKey);
183
                }
184
                return $value;
185
            })
186
            ->setNormalizer('batch', function (Options $options, $value) use ($admin, $actionName) {
187
                if ($value) {
188
                    if (!is_array($value)) {
189
                        $value = [$value];
190
                    }
191
                    foreach ($value as $key => $title) {
192
                        if (!$title) {
193
                            $adminKey = '';
194
                            // if an Admin is linked to this action, we use its name in translation key
195
                            if ($admin) {
196
                                $adminKey = $admin->getName();
197
                            }
198
                            $value[$key] = $this->configuration->getTranslationKey('batch.' . $key, $adminKey);
199
                        }
200
                    }
201
                }
202
                return $value;
203
            })
204
        ;
205
    }
206
}
207