PaymentMethodFormBuilder::buildForm()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 129
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 8.2857
c 0
b 0
f 0
cc 3
eloc 84
nc 4
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 Symfony\Component\PropertyAccess\PropertyPath;
15
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
16
use WellCommerce\Bundle\OrderBundle\Processor\PaymentProcessorCollection;
17
use WellCommerce\Component\Form\Conditions\Equals;
18
use WellCommerce\Component\Form\Elements\FormInterface;
19
20
/**
21
 * Class PaymentMethodFormBuilder
22
 *
23
 * @author  Adam Piotrowski <[email protected]>
24
 */
25
class PaymentMethodFormBuilder extends AbstractFormBuilder
26
{
27
    public function getAlias(): string
28
    {
29
        return 'admin.payment_method';
30
    }
31
    
32
    public function buildForm(FormInterface $form)
33
    {
34
        $orderStatuses = $this->get('order_status.dataset.admin')->getResult('select');
35
        $options       = [];
36
        $processors    = $this->getPaymentProcessorCollection();
37
        $processorKeys = $processors->keys();
38
        foreach ($processors->all() as $processor) {
39
            $processorName           = $processor->getConfigurator()->getName();
40
            $options[$processorName] = $processorName;
41
        }
42
        
43
        $defaultProcessor = reset($processorKeys);
44
        
45
        $requiredData = $form->addChild($this->getElement('nested_fieldset', [
46
            'name'  => 'required_data',
47
            'label' => 'common.fieldset.general',
48
        ]));
49
        
50
        $languageData = $requiredData->addChild($this->getElement('language_fieldset', [
51
            'name'        => 'translations',
52
            'label'       => 'common.fieldset.translations',
53
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('payment_method.repository')),
54
        ]));
55
        
56
        $languageData->addChild($this->getElement('text_field', [
57
            'name'  => 'name',
58
            'label' => 'common.label.name',
59
            'rules' => [
60
                $this->getRule('required'),
61
            ],
62
        ]));
63
        
64
        $processorType = $requiredData->addChild($this->getElement('select', [
65
            'name'    => 'processor',
66
            'label'   => 'payment_method.label.processor',
67
            'options' => $options,
68
            'default' => $defaultProcessor,
69
        ]));
70
        
71
        $requiredData->addChild($this->getElement('checkbox', [
72
            'name'  => 'enabled',
73
            'label' => 'common.label.enabled',
74
        ]));
75
        
76
        $requiredData->addChild($this->getElement('text_field', [
77
            'name'  => 'hierarchy',
78
            'label' => 'common.label.hierarchy',
79
            'rules' => [
80
                $this->getRule('required'),
81
            ],
82
        ]));
83
        
84
        $clientGroupData = $form->addChild($this->getElement('nested_fieldset', [
85
            'name'  => 'client_group_data',
86
            'label' => 'payment_method.fieldset.client_groups',
87
        ]));
88
        
89
        $clientGroupData->addChild($this->getElement('tip', [
90
            'tip' => 'payment_method.tip.client_groups',
91
        ]));
92
        
93
        $clientGroupData->addChild($this->getElement('multi_select', [
94
            'name'        => 'clientGroups',
95
            'label'       => 'payment_method.label.client_groups',
96
            'options'     => $this->get('client_group.dataset.admin')->getResult('select'),
97
            'transformer' => $this->getRepositoryTransformer('collection', $this->get('client_group.repository')),
98
        ]));
99
        
100
        $statusesData = $form->addChild($this->getElement('nested_fieldset', [
101
            'name'  => 'statuses',
102
            'label' => 'payment_method.fieldset.order_statuses',
103
        ]));
104
        
105
        $statusesData->addChild($this->getElement('select', [
106
            'name'        => 'paymentPendingOrderStatus',
107
            'label'       => 'payment_method.label.payment_pending_order_status',
108
            'options'     => $orderStatuses,
109
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('order_status.repository')),
110
        ]));
111
        
112
        $statusesData->addChild($this->getElement('select', [
113
            'name'        => 'paymentSuccessOrderStatus',
114
            'label'       => 'payment_method.label.payment_success_order_status',
115
            'options'     => $orderStatuses,
116
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('order_status.repository')),
117
        ]));
118
        
119
        $statusesData->addChild($this->getElement('select', [
120
            'name'        => 'paymentFailureOrderStatus',
121
            'label'       => 'payment_method.label.payment_failure_order_status',
122
            'options'     => $orderStatuses,
123
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('order_status.repository')),
124
        ]));
125
        
126
        $shippingMethodsData = $form->addChild($this->getElement('nested_fieldset', [
127
            'name'  => 'shipping_methods_data',
128
            'label' => 'payment_method.fieldset.shipping_methods',
129
        ]));
130
        
131
        $shippingMethodsData->addChild($this->getElement('multi_select', [
132
            'name'        => 'shippingMethods',
133
            'label'       => 'payment_method.label.shipping_methods',
134
            'options'     => $this->get('shipping_method.dataset.admin')->getResult('select'),
135
            'transformer' => $this->getRepositoryTransformer('collection', $this->get('shipping_method.repository')),
136
        ]));
137
        
138
        $configurationData = $form->addChild($this->getElement('nested_fieldset', [
139
            'name'          => 'configuration',
140
            'property_path' => new PropertyPath('configuration'),
141
            'label'         => 'payment_method.fieldset.processor_configuration',
142
        ]));
143
        
144
        foreach ($processors->all() as $processor) {
145
            
146
            $dependency = $this->getDependency('show', [
147
                'form'      => $form,
148
                'field'     => $processorType,
149
                'condition' => new Equals($processor->getConfigurator()->getName()),
150
            ]);
151
            
152
            $processor->getConfigurator()->addConfigurationFields($this, $configurationData, $dependency);
153
        }
154
        
155
        $this->addShopsFieldset($form);
156
        
157
        $form->addFilter($this->getFilter('no_code'));
158
        $form->addFilter($this->getFilter('trim'));
159
        $form->addFilter($this->getFilter('secure'));
160
    }
161
    
162
    protected function getPaymentProcessorCollection(): PaymentProcessorCollection
163
    {
164
        return $this->container->get('payment.processor.collection');
165
    }
166
}
167