Form::_prepareForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
c 0
b 0
f 0
rs 9.0472
cc 1
nc 1
nop 0

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
declare(strict_types=1);
4
5
/**
6
 * File: Form.php
7
 *
8
 * @author Maciej Sławik <[email protected]>
9
 * @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\VarnishWarmer\Block\Adminhtml\PurgeMultiple\Form\Edit;
13
14
use LizardMedia\VarnishWarmer\Model\FormDataProvider\MultipleUrlsPurgeCommandsProvider;
15
use Magento\Backend\Block\Template\Context;
16
use Magento\Backend\Block\Widget\Form as WidgetForm;
17
use Magento\Backend\Block\Widget\Form\Generic;
18
use Magento\Framework\Data\FormFactory;
19
use Magento\Framework\Exception\LocalizedException;
20
use Magento\Framework\Registry;
21
use Magento\Store\Model\System\Store;
22
23
/**
24
 * Class Form
25
 * @package LizardMedia\VarnishWarmer\Block\Adminhtml\Form\PurgeMultiple
26
 * @SuppressWarnings(PHPMD.LongVariable)
27
 * @codeCoverageIgnore
28
 */
29
class Form extends Generic
30
{
31
    /**
32
     * @var string
33
     */
34
    public const PROCESS_URL_FORM_PARAM = 'process_url';
35
36
    /**
37
     * @var string
38
     */
39
    public const STORE_VIEW_FORM_PARAM = 'store_id';
40
41
    /**
42
     * @var Store
43
     */
44
    private $systemStore;
45
46
    /**
47
     * @var MultipleUrlsPurgeCommandsProvider
48
     */
49
    private $multipleUrlsPurgeCommandsProvider;
50
51
    /**
52
     * Form constructor.
53
     * @param Context $context
54
     * @param Registry $registry
55
     * @param FormFactory $formFactory
56
     * @param Store $systemStore
57
     * @param MultipleUrlsPurgeCommandsProvider $multipleUrlsPurgeCommandsProvider
58
     * @param array $data
59
     */
60
    public function __construct(
61
        Context $context,
62
        Registry $registry,
63
        FormFactory $formFactory,
64
        Store $systemStore,
65
        MultipleUrlsPurgeCommandsProvider $multipleUrlsPurgeCommandsProvider,
66
        array $data = []
67
    ) {
68
        parent::__construct($context, $registry, $formFactory, $data);
69
        $this->systemStore = $systemStore;
70
        $this->multipleUrlsPurgeCommandsProvider = $multipleUrlsPurgeCommandsProvider;
71
    }
72
73
    /**
74
     * @return void
75
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
76
     */
77
    protected function _construct(): void
78
    {
79
        parent::_construct();
80
        $this->setId('purgemultiple_form');
81
        $this->setTitle(__('Varnish: purge a group of URLs'));
82
    }
83
84
    /**
85
     * @return Form
86
     * @throws LocalizedException
87
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
88
     */
89
    protected function _prepareForm(): self
90
    {
91
        /** @var \Magento\Framework\Data\Form $form */
92
        $form = $this->_formFactory->create(
93
            [
94
                'data' => [
95
                    'id' => 'edit_form',
96
                    'action' => $this->getFormTargetUrl(),
97
                    'method' => 'post'
98
                ]
99
            ]
100
        );
101
102
        $form->setHtmlIdPrefix('purgemultiple_');
103
104
        $fieldset = $form->addFieldset(
105
            'base_fieldset',
106
            [
107
                'class' => 'fieldset-wide'
108
            ]
109
        );
110
111
        $fieldset->addField(
112
            self::PROCESS_URL_FORM_PARAM,
113
            'select',
114
            [
115
                'name' => self::PROCESS_URL_FORM_PARAM,
116
                'label' => __('Process to run'),
117
                'title' => __('Process to run'),
118
                'required' => true,
119
                'values' => $this->multipleUrlsPurgeCommandsProvider->getCommandArray()
120
121
            ]
122
        );
123
124
        $fieldset->addField(
125
            self::STORE_VIEW_FORM_PARAM,
126
            'select',
127
            [
128
                'name' => self::STORE_VIEW_FORM_PARAM,
129
                'label' => __('Store View'),
130
                'title' => __('Store View'),
131
                'required' => true,
132
                'values' => $this->systemStore->getStoreValuesForForm(false, true)
133
            ]
134
        );
135
136
        $form->setUseContainer(true);
137
        $this->setForm($form);
138
139
        return parent::_prepareForm();
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    private function getFormTargetUrl(): string
146
    {
147
        return $this->_urlBuilder->getUrl('lizardmediavarnish/purgemultiple/run');
148
    }
149
}
150