CouponFormBuilder::buildForm()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 71
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\CouponBundle\Form\Admin;
13
14
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
15
use WellCommerce\Component\Form\DataTransformer\DateTransformer;
16
use WellCommerce\Component\Form\Elements\FormInterface;
17
18
/**
19
 * Class CouponForm
20
 *
21
 * @author  Adam Piotrowski <[email protected]>
22
 */
23
class CouponFormBuilder extends AbstractFormBuilder
24
{
25
    public function getAlias(): string
26
    {
27
        return 'admin.coupon';
28
    }
29
    
30
    public function buildForm(FormInterface $form)
31
    {
32
        $currencies = $this->get('currency.dataset.admin')->getResult('select', ['order_by' => 'code'], [
33
            'label_column' => 'code',
34
            'value_column' => 'code'
35
        ]);
36
37
        $requiredData = $form->addChild($this->getElement('nested_fieldset', [
38
            'name'  => 'required_data',
39
            'label' => 'common.fieldset.general'
40
        ]));
41
42
        $languageData = $requiredData->addChild($this->getElement('language_fieldset', [
43
            'name'        => 'translations',
44
            'label'       => 'common.fieldset.translations',
45
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('coupon.repository'))
46
        ]));
47
48
        $languageData->addChild($this->getElement('text_field', [
49
            'name'  => 'name',
50
            'label' => 'common.label.name',
51
            'rules' => [
52
                $this->getRule('required')
53
            ],
54
        ]));
55
56
        $languageData->addChild($this->getElement('text_area', [
57
            'name'  => 'description',
58
            'label' => 'common.label.description',
59
        ]));
60
61
        $requiredData->addChild($this->getElement('date', [
62
            'name'        => 'validFrom',
63
            'label'       => 'common.label.valid_from',
64
            'transformer' => new DateTransformer('m/d/Y'),
65
        ]));
66
67
        $requiredData->addChild($this->getElement('date', [
68
            'name'        => 'validTo',
69
            'label'       => 'common.label.valid_to',
70
            'transformer' => new DateTransformer('m/d/Y'),
71
        ]));
72
73
        $requiredData->addChild($this->getElement('text_field', [
74
            'name'  => 'code',
75
            'label' => 'common.label.code',
76
            'rules' => [
77
                $this->getRule('required')
78
            ],
79
        ]));
80
81
        $requiredData->addChild($this->getElement('text_field', [
82
            'name'  => 'clientUsageLimit',
83
            'label' => 'coupon.label.client_usage_limit',
84
            'rules' => [
85
                $this->getRule('required')
86
            ],
87
        ]));
88
89
        $requiredData->addChild($this->getElement('text_field', [
90
            'name'  => 'globalUsageLimit',
91
            'label' => 'coupon.label.global_usage_limit',
92
            'rules' => [
93
                $this->getRule('required')
94
            ],
95
        ]));
96
97
        $discountPane = $form->addChild($this->getElement('nested_fieldset', [
98
            'name'  => 'discount_pane',
99
            'label' => 'coupon.fieldset.discount_settings'
100
        ]));
101
    
102
        $discountPane->addChild($this->getElement('select', [
103
            'name'         => 'currency',
104
            'label'        => 'common.label.currency',
105
            'options'      => $currencies,
106
        ]));
107
        
108
        $discountPane->addChild($this->getElement('select', [
109
            'name'    => 'modifierType',
110
            'label'   => 'coupon.label.modifier_type',
111
            'options' => $this->getModifierTypes()
112
        ]));
113
        
114
        $discountPane->addChild($this->getElement('text_field', [
115
            'name'  => 'modifierValue',
116
            'label' => 'coupon.label.modifier_value',
117
            'rules' => [
118
                $this->getRule('required')
119
            ],
120
        ]));
121
    
122
        $discountPane->addChild($this->getElement('text_field', [
123
            'name'  => 'minimumOrderValue',
124
            'label' => 'coupon.label.minimum_order_value',
125
            'rules' => [
126
                $this->getRule('required')
127
            ],
128
        ]));
129
    
130
        $discountPane->addChild($this->getElement('checkbox', [
131
            'name'  => 'excludePromotions',
132
            'label' => 'coupon.label.exclude_promotions',
133
            'rules' => [
134
                $this->getRule('required')
135
            ],
136
        ]));
137
138
        $form->addFilter($this->getFilter('no_code'));
139
        $form->addFilter($this->getFilter('trim'));
140
        $form->addFilter($this->getFilter('secure'));
141
    }
142
143
    protected function getModifierTypes()
144
    {
145
        return [
146
            '%' => 'coupon.label.modifier_type_percent',
147
            '-' => 'coupon.label.modifier_type_subtract',
148
        ];
149
    }
150
}
151