ClientFormBuilder::buildForm()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 246
Code Lines 156

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 246
rs 8.2857
c 0
b 0
f 0
cc 2
eloc 156
nc 2
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\AppBundle\Form\Admin;
13
14
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
15
use WellCommerce\Component\Form\Elements\FormInterface;
16
17
/**
18
 * Class ClientFormBuilder
19
 *
20
 * @author Adam Piotrowski <[email protected]>
21
 */
22
class ClientFormBuilder extends AbstractFormBuilder
23
{
24
    public function getAlias(): string
25
    {
26
        return 'admin.client';
27
    }
28
    
29
    public function buildForm(FormInterface $form)
30
    {
31
        $countries      = $this->get('country.repository')->all();
32
        $defaultCountry = $this->getShopStorage()->getCurrentShop()->getDefaultCountry();
33
        
34
        $requiredData = $form->addChild($this->getElement('nested_fieldset', [
35
            'name'  => 'required_data',
36
            'label' => 'common.fieldset.general',
37
        ]));
38
        
39
        $requiredData->addChild($this->getElement('select', [
40
            'name'        => 'shop',
41
            'label'       => 'common.label.shop',
42
            'options'     => $this->get('shop.dataset.admin')->getResult('select'),
43
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('shop.repository')),
44
        ]));
45
        
46
        $requiredData->addChild($this->getElement('select', [
47
            'name'        => 'clientGroup',
48
            'label'       => 'common.label.client_group',
49
            'options'     => $this->get('client_group.dataset.admin')->getResult('select'),
50
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('client_group.repository')),
51
        ]));
52
53
        $requiredData->addChild($this->getElement('checkbox', [
54
            'name'  => 'enabled',
55
            'label' => 'common.label.enabled',
56
        ]));
57
58
        $clientDetailsData = $requiredData->addChild($this->getElement('nested_fieldset', [
59
            'name'  => 'clientDetails',
60
            'label' => 'client.heading.client_details',
61
        ]));
62
        
63
        $clientDetailsData->addChild($this->getElement('text_field', [
64
            'name'  => 'clientDetails.username',
65
            'label' => 'client.label.username',
66
            'rules' => [
67
                $this->getRule('required'),
68
            ],
69
        ]));
70
        
71
        $clientDetailsData->addChild($this->getElement('checkbox', [
72
            'name'    => 'clientDetails.conditionsAccepted',
73
            'label'   => 'client.label.accept_conditions',
74
            'default' => true,
75
            'comment' => 'client.label.accept_conditions',
76
        ]));
77
        
78
        $clientDetailsData->addChild($this->getElement('checkbox', [
79
            'name'    => 'clientDetails.newsletterAccepted',
80
            'label'   => 'client.label.accept_newsletter',
81
            'comment' => 'client.label.accept_newsletter',
82
        ]));
83
        
84
        $discountSettings = $form->addChild($this->getElement('nested_fieldset', [
85
            'name'  => 'discount_data',
86
            'label' => 'common.fieldset.discount_settings',
87
        ]));
88
        
89
        $discountSettings->addChild($this->getElement('tip', [
90
            'tip' => 'common.tip.discount_expression',
91
        ]));
92
        
93
        $discountSettings->addChild($this->getElement('text_field', [
94
            'name'   => 'clientDetails.discount',
95
            'label'  => 'common.label.discount',
96
            'suffix' => '%',
97
        ]));
98
        
99
        $minimumOrderAmount = $form->addChild($this->getElement('nested_fieldset', [
100
            'name'  => 'minimumOrderAmount',
101
            'label' => 'common.fieldset.minimum_order_amount',
102
        ]));
103
        
104
        $minimumOrderAmount->addChild($this->getElement('text_field', [
105
            'name'    => 'minimumOrderAmount.value',
106
            'label'   => 'common.label.minimum_order_amount.value',
107
            'suffix'  => '%',
108
            'filters' => [
109
                $this->getFilter('comma_to_dot_changer'),
110
            ],
111
            'rules'   => [
112
                $this->getRule('required'),
113
            ],
114
            'default' => 0,
115
        ]));
116
        
117
        $minimumOrderAmount->addChild($this->getElement('select', [
118
            'name'    => 'minimumOrderAmount.currency',
119
            'label'   => 'common.label.minimum_order_amount.currency',
120
            'options' => $this->get('currency.dataset.admin')->getResult('select', ['order_by' => 'code'], [
121
                'label_column' => 'code',
122
                'value_column' => 'code',
123
            ]),
124
        ]));
125
        
126
        if ($this->getRouterHelper()->getCurrentAction() === 'addAction') {
127
            $clientDetailsData->addChild($this->getElement('text_field', [
128
                'name'  => 'clientDetails.hashedPassword',
129
                'label' => 'client.label.password',
130
            ]))->setValue($this->getSecurityHelper()->generateRandomPassword());
131
        }
132
        
133
        $contactDetailsData = $requiredData->addChild($this->getElement('nested_fieldset', [
134
            'name'  => 'contactDetails',
135
            'label' => 'client.heading.contact_details',
136
        ]));
137
        
138
        $contactDetailsData->addChild($this->getElement('text_field', [
139
            'name'  => 'contactDetails.firstName',
140
            'label' => 'common.label.first_name',
141
            'rules' => [
142
                $this->getRule('required'),
143
            ],
144
        ]));
145
        
146
        $contactDetailsData->addChild($this->getElement('text_field', [
147
            'name'  => 'contactDetails.lastName',
148
            'label' => 'common.label.last_name',
149
            'rules' => [
150
                $this->getRule('required'),
151
            ],
152
        ]));
153
        
154
        $contactDetailsData->addChild($this->getElement('text_field', [
155
            'name'  => 'contactDetails.phone',
156
            'label' => 'common.label.phone',
157
            'rules' => [
158
                $this->getRule('required'),
159
            ],
160
        ]));
161
        
162
        $billingAddress = $form->addChild($this->getElement('nested_fieldset', [
163
            'name'  => 'billingAddress',
164
            'label' => 'client.heading.billing_address',
165
        ]));
166
        
167
        $billingAddress->addChild($this->getElement('text_field', [
168
            'name'  => 'billingAddress.firstName',
169
            'label' => 'client.label.address.first_name',
170
        ]));
171
        
172
        $billingAddress->addChild($this->getElement('text_field', [
173
            'name'  => 'billingAddress.lastName',
174
            'label' => 'client.label.address.last_name',
175
        ]));
176
        
177
        $billingAddress->addChild($this->getElement('text_field', [
178
            'name'  => 'billingAddress.line1',
179
            'label' => 'client.label.address.line1',
180
        ]));
181
        
182
        $billingAddress->addChild($this->getElement('text_field', [
183
            'name'  => 'billingAddress.line2',
184
            'label' => 'client.label.address.line2',
185
        ]));
186
        
187
        $billingAddress->addChild($this->getElement('text_field', [
188
            'name'  => 'billingAddress.postalCode',
189
            'label' => 'client.label.address.postal_code',
190
        ]));
191
        
192
        $billingAddress->addChild($this->getElement('text_field', [
193
            'name'  => 'billingAddress.province',
194
            'label' => 'client.label.address.province',
195
        ]));
196
        
197
        $billingAddress->addChild($this->getElement('text_field', [
198
            'name'  => 'billingAddress.city',
199
            'label' => 'client.label.address.city',
200
        ]));
201
        
202
        $billingAddress->addChild($this->getElement('select', [
203
            'name'    => 'billingAddress.country',
204
            'label'   => 'client.label.address.country',
205
            'options' => $countries,
206
            'default' => $defaultCountry,
207
        ]));
208
        
209
        $billingAddress->addChild($this->getElement('text_field', [
210
            'name'  => 'billingAddress.vatId',
211
            'label' => 'client.label.address.vat_id',
212
        ]));
213
        
214
        $billingAddress->addChild($this->getElement('text_field', [
215
            'name'  => 'billingAddress.companyName',
216
            'label' => 'client.label.address.company_name',
217
        ]));
218
        
219
        $shippingAddress = $form->addChild($this->getElement('nested_fieldset', [
220
            'name'  => 'shippingAddress',
221
            'label' => 'client.heading.shipping_address',
222
        ]));
223
        
224
        $shippingAddress->addChild($this->getElement('text_field', [
225
            'name'  => 'shippingAddress.firstName',
226
            'label' => 'client.label.address.first_name',
227
        ]));
228
        
229
        $shippingAddress->addChild($this->getElement('text_field', [
230
            'name'  => 'shippingAddress.lastName',
231
            'label' => 'client.label.address.last_name',
232
        ]));
233
        
234
        $shippingAddress->addChild($this->getElement('text_field', [
235
            'name'  => 'shippingAddress.line1',
236
            'label' => 'client.label.address.line1',
237
        ]));
238
        
239
        $shippingAddress->addChild($this->getElement('text_field', [
240
            'name'  => 'shippingAddress.companyName',
241
            'label' => 'client.label.address.company_name',
242
        ]));
243
        
244
        $shippingAddress->addChild($this->getElement('text_field', [
245
            'name'  => 'shippingAddress.line2',
246
            'label' => 'client.label.address.line2',
247
        ]));
248
        
249
        $shippingAddress->addChild($this->getElement('text_field', [
250
            'name'  => 'shippingAddress.postalCode',
251
            'label' => 'client.label.address.postal_code',
252
        ]));
253
        
254
        $shippingAddress->addChild($this->getElement('text_field', [
255
            'name'  => 'shippingAddress.state',
256
            'label' => 'client.label.address.state',
257
        ]));
258
        
259
        $shippingAddress->addChild($this->getElement('text_field', [
260
            'name'  => 'shippingAddress.city',
261
            'label' => 'client.label.address.city',
262
        ]));
263
        
264
        $shippingAddress->addChild($this->getElement('select', [
265
            'name'    => 'shippingAddress.country',
266
            'label'   => 'client.label.address.country',
267
            'options' => $countries,
268
            'default' => $defaultCountry,
269
        ]));
270
        
271
        $form->addFilter($this->getFilter('no_code'));
272
        $form->addFilter($this->getFilter('trim'));
273
        $form->addFilter($this->getFilter('secure'));
274
    }
275
}
276