ShippingMethodFormBuilder::buildForm()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 147
Code Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 94
nc 1
nop 1

How to fix   Long Method   

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
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
namespace WellCommerce\Bundle\OrderBundle\Form\Admin;
13
14
use Doctrine\Common\Collections\Collection;
15
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
16
use WellCommerce\Bundle\OrderBundle\Calculator\ShippingCalculatorInterface;
17
use WellCommerce\Component\Form\Elements\FormInterface;
18
19
/**
20
 * Class ShippingMethodFormBuilder
21
 *
22
 * @author  Adam Piotrowski <[email protected]>
23
 */
24
class ShippingMethodFormBuilder extends AbstractFormBuilder
25
{
26
    protected $calculators;
27
    
28
    public function getAlias(): string
29
    {
30
        return 'admin.shipping_method';
31
    }
32
    
33
    public function buildForm(FormInterface $form)
34
    {
35
        $calculatorOptions = [];
36
        $calculators       = $this->getCalculators();
37
        
38
        $calculators->map(function (ShippingCalculatorInterface $calculator) use (&$calculatorOptions) {
39
            $calculatorOptions[$calculator->getAlias()] = $calculator->getAlias();
40
        });
41
    
42
        $currencies = $this->get('currency.dataset.admin')->getResult('select', ['order_by' => 'code'], [
43
            'label_column' => 'code',
44
            'value_column' => 'id',
45
        ]);
46
        
47
        $requiredData = $form->addChild($this->getElement('nested_fieldset', [
48
            'name'  => 'required_data',
49
            'label' => 'common.fieldset.general'
50
        ]));
51
        
52
        $languageData = $requiredData->addChild($this->getElement('language_fieldset', [
53
            'name'        => 'translations',
54
            'label'       => 'common.fieldset.translations',
55
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('shipping_method.repository'))
56
        ]));
57
        
58
        $languageData->addChild($this->getElement('text_field', [
59
            'name'  => 'name',
60
            'label' => 'common.label.name',
61
            'rules' => [
62
                $this->getRule('required')
63
            ],
64
        ]));
65
        
66
        $requiredData->addChild($this->getElement('checkbox', [
67
            'name'  => 'enabled',
68
            'label' => 'common.label.enabled',
69
        ]));
70
        
71
        $requiredData->addChild($this->getElement('text_field', [
72
            'name'  => 'hierarchy',
73
            'label' => 'common.label.hierarchy',
74
            'rules' => [
75
                $this->getRule('required')
76
            ],
77
        ]));
78
        
79
        $requiredData->addChild($this->getElement('select', [
80
            'name'    => 'optionsProvider',
81
            'label'   => 'shipping_method.label.options_provider',
82
            'options' => $this->getOptionProviders()
83
        ]));
84
    
85
        $clientGroupData = $form->addChild($this->getElement('nested_fieldset', [
86
            'name'  => 'client_group_data',
87
            'label' => 'shipping_method.fieldset.client_groups'
88
        ]));
89
    
90
        $clientGroupData->addChild($this->getElement('tip', [
91
            'tip' => 'shipping_method.tip.client_groups',
92
        ]));
93
        
94
        $clientGroupData->addChild($this->getElement('multi_select', [
95
            'name'        => 'clientGroups',
96
            'label'       => 'shipping_method.label.client_groups',
97
            'options'     => $this->get('client_group.dataset.admin')->getResult('select'),
98
            'transformer' => $this->getRepositoryTransformer('collection', $this->get('client_group.repository')),
99
        ]));
100
        
101
        $costsData = $form->addChild($this->getElement('nested_fieldset', [
102
            'name'  => 'costs_data',
103
            'label' => 'shipping_method.fieldset.costs'
104
        ]));
105
        
106
        $costsData->addChild($this->getElement('select', [
107
            'name'    => 'calculator',
108
            'label'   => 'shipping_method.label.calculator',
109
            'options' => $calculatorOptions
110
        ]));
111
        
112
        $costsData->addChild($this->getElement('select', [
113
            'name'        => 'currency',
114
            'label'       => 'common.label.currency',
115
            'options'     => $currencies,
116
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('currency.repository'))
117
        ]));
118
        
119
        $tax = $costsData->addChild($this->getElement('select', [
120
            'name'        => 'tax',
121
            'label'       => 'common.label.tax',
122
            'options'     => $this->get('tax.dataset.admin')->getResult('select'),
123
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('tax.repository'))
124
        ]));
125
        
126
        $costsData->addChild($this->getElement('range_editor', [
127
            'name'            => 'costs',
128
            'label'           => 'shipping_method.label.costs',
129
            'vat_field'       => $tax,
130
            'range_precision' => 2,
131
            'transformer'     => $this->getRepositoryTransformer('shipping_cost_collection', $this->get('shipping_method_cost.repository'))
132
        ]));
133
        
134
        $countriesData = $form->addChild($this->getElement('nested_fieldset', [
135
            'name'  => 'countries_data',
136
            'label' => 'shipping_method.fieldset.countries'
137
        ]));
138
        
139
        $countriesData->addChild($this->getElement('multi_select', [
140
            'name'    => 'countries',
141
            'label'   => 'address.label.country',
142
            'options' => $this->get('country.repository')->all(),
143
        ]));
144
    
145
        $dimensionData = $form->addChild($this->getElement('nested_fieldset', [
146
            'name'  => 'box_dimension',
147
            'label' => 'shipping_method.fieldset.box_dimension',
148
        ]));
149
    
150
        $dimensionData->addChild($this->getElement('text_field', [
151
            'name'    => 'boxDimension.width',
152
            'label'   => 'common.label.dimension.width',
153
            'filters' => [
154
                $this->getFilter('comma_to_dot_changer'),
155
            ],
156
        ]));
157
    
158
        $dimensionData->addChild($this->getElement('text_field', [
159
            'name'    => 'boxDimension.height',
160
            'label'   => 'common.label.dimension.height',
161
            'filters' => [
162
                $this->getFilter('comma_to_dot_changer'),
163
            ],
164
        ]));
165
    
166
        $dimensionData->addChild($this->getElement('text_field', [
167
            'name'    => 'boxDimension.depth',
168
            'label'   => 'common.label.dimension.depth',
169
            'filters' => [
170
                $this->getFilter('comma_to_dot_changer'),
171
            ],
172
        ]));
173
        
174
        $this->addShopsFieldset($form);
175
        
176
        $form->addFilter($this->getFilter('no_code'));
177
        $form->addFilter($this->getFilter('trim'));
178
        $form->addFilter($this->getFilter('secure'));
179
    }
180
    
181
    private function getCalculators() : Collection
182
    {
183
        return $this->get('shipping_method.calculator.collection');
184
    }
185
    
186
    private function getOptionProviders() : array
187
    {
188
        $providers           = $this->get('shipping_method.options_provider.collection');
189
        $optionProviders[''] = 'common.label.none';
0 ignored issues
show
Coding Style Comprehensibility introduced by Adam Piotrowski
$optionProviders was never initialized. Although not strictly required by PHP, it is generally a good practice to add $optionProviders = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
190
        
191
        foreach ($providers->getKeys() as $alias) {
192
            $optionProviders[$alias] = $alias;
193
        }
194
        
195
        return $optionProviders;
196
    }
197
}
198