CategoryFormBuilder::buildForm()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 108
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 8.2857
c 0
b 0
f 0
cc 2
eloc 72
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
13
namespace WellCommerce\Bundle\CatalogBundle\Form\Admin;
14
15
use WellCommerce\Bundle\CatalogBundle\Entity\Category;
16
use WellCommerce\Bundle\CatalogBundle\Request\Storage\CategoryStorageInterface;
17
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder;
18
use WellCommerce\Component\Form\Elements\FormInterface;
19
20
/**
21
 * Class CategoryForm
22
 *
23
 * @author  Adam Piotrowski <[email protected]>
24
 */
25
class CategoryFormBuilder extends AbstractFormBuilder
26
{
27
    public function getAlias(): string
28
    {
29
        return 'admin.category';
30
    }
31
    
32
    public function buildForm(FormInterface $form)
33
    {
34
        $currentCategory = $this->getCurrentCategory();
35
        
36
        $requiredData = $form->addChild($this->getElement('nested_fieldset', [
37
            'name'  => 'required_data',
38
            'label' => 'common.fieldset.general',
39
        ]));
40
        
41
        $languageData = $requiredData->addChild($this->getElement('language_fieldset', [
42
            'name'        => 'translations',
43
            'label'       => 'common.fieldset.translations',
44
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('category.repository')),
45
        ]));
46
        
47
        $name = $languageData->addChild($this->getElement('text_field', [
48
            'name'  => 'name',
49
            'label' => 'common.label.name',
50
            'rules' => [
51
                $this->getRule('required'),
52
            ],
53
        ]));
54
        
55
        $languageData->addChild($this->getElement('slug_field', [
56
            'name'            => 'slug',
57
            'label'           => 'category.label.slug',
58
            'name_field'      => $name,
59
            'generate_route'  => 'route.generate',
60
            'translatable_id' => $this->getRequestHelper()->getAttributesBagParam('id'),
61
            'rules'           => [
62
                $this->getRule('required'),
63
            ],
64
        ]));
65
        
66
        $requiredData->addChild($this->getElement('checkbox', [
67
            'name'    => 'enabled',
68
            'label'   => 'category.label.enabled',
69
            'comment' => 'category.comment.enabled',
70
        ]));
71
        
72
        $requiredData->addChild($this->getElement('text_field', [
73
            'name'  => 'hierarchy',
74
            'label' => 'common.label.hierarchy',
75
            'rules' => [
76
                $this->getRule('required'),
77
            ],
78
        ]));
79
        
80
        $requiredData->addChild($this->getElement('text_field', [
81
            'name'  => 'symbol',
82
            'label' => 'common.label.symbol',
83
        ]));
84
        
85
        $requiredData->addChild($this->getElement('tree', [
86
            'name'        => 'parent',
87
            'label'       => 'category.label.parent',
88
            'choosable'   => true,
89
            'selectable'  => false,
90
            'sortable'    => false,
91
            'clickable'   => false,
92
            'items'       => $this->getCategoryParentOptions($currentCategory),
93
            'restrict'    => $currentCategory instanceof Category ? $currentCategory->getId() : 0,
94
            'transformer' => $this->getRepositoryTransformer('entity', $this->get('category.repository')),
95
        ]));
96
        
97
        $descriptionData = $form->addChild($this->getElement('nested_fieldset', [
98
            'name'  => 'description_data',
99
            'label' => 'category.form.fieldset.description',
100
        ]));
101
        
102
        $languageData = $descriptionData->addChild($this->getElement('language_fieldset', [
103
            'name'        => 'translations',
104
            'label'       => 'common.fieldset.translations',
105
            'transformer' => $this->getRepositoryTransformer('translation', $this->get('category.repository')),
106
        ]));
107
        
108
        $languageData->addChild($this->getElement('rich_text_editor', [
109
            'name'  => 'shortDescription',
110
            'label' => 'common.label.short_description',
111
        ]));
112
        
113
        $languageData->addChild($this->getElement('rich_text_editor', [
114
            'name'  => 'description',
115
            'label' => 'common.label.description',
116
        ]));
117
        
118
        $mediaData = $form->addChild($this->getElement('nested_fieldset', [
119
            'name'  => 'media_data',
120
            'label' => 'common.fieldset.photos',
121
        ]));
122
        
123
        $mediaData->addChild($this->getElement('image', [
124
            'name'         => 'photo',
125
            'label'        => 'form.media_data.image_id',
126
            'repeat_min'   => 0,
127
            'repeat_max'   => 1,
128
            'transformer'  => $this->getRepositoryTransformer('media_entity', $this->get('media.repository')),
129
            'session_id'   => $this->getRequestHelper()->getSessionId(),
130
            'session_name' => $this->getRequestHelper()->getSessionName(),
131
        ]));
132
        
133
        $this->addMetadataFieldset($form, $this->get('category.repository'));
134
        
135
        $this->addShopsFieldset($form);
136
        
137
        $form->addFilter($this->getFilter('trim'));
138
        $form->addFilter($this->getFilter('secure'));
139
    }
140
    
141
    protected function getCategoryParentOptions(Category $category = null): array
142
    {
143
        $options = $this->get('category.dataset.admin')->getResult('flat_tree', ['limit' => 10000]);
144
        
145
        if ($category instanceof Category && $category->getParent() instanceof Category) {
146
            array_push($options, [
147
                'id'             => 0,
148
                'hierarchy'      => 0,
149
                'parent'         => null,
150
                'children_count' => 0,
151
                'products_count' => 0,
152
                'name'           => $this->trans('category.label.empty_parent'),
153
            ]);
154
        }
155
        
156
        return $options;
157
    }
158
    
159
    protected function getCurrentCategory()
160
    {
161
        /** @var CategoryStorageInterface $storage */
162
        $storage = $this->get('category.storage');
163
        
164
        if ($storage->hasCurrentCategory()) {
165
            return $storage->getCurrentCategory();
166
        }
167
        
168
        return null;
169
    }
170
}
171