Form::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 5
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\PurgeSingle\Form\Edit;
13
14
use Magento\Backend\Block\Template\Context;
15
use Magento\Backend\Block\Widget\Form\Generic;
16
use Magento\Framework\Data\FormFactory;
17
use Magento\Framework\Exception\LocalizedException;
18
use Magento\Framework\Registry;
19
use Magento\Store\Model\System\Store;
20
21
/**
22
 * Class Form
23
 * @package LizardMedia\VarnishWarmer\Block\Adminhtml\PurgeSingle\Form\Edit
24
 * @codeCoverageIgnore
25
 */
26
class Form extends Generic
27
{
28
    /**
29
     * @var string
30
     */
31
    public const URL_FORM_PARAM = 'url';
32
33
    /**
34
     * @var string
35
     */
36
    public const STORE_VIEW_FORM_PARAM = 'store_id';
37
38
    /**
39
     * @var string
40
     */
41
    public const FORCE_PURGE_FORM_PARAM = 'force_purge';
42
43
    /**
44
     * @var Store
45
     */
46
    private $systemStore;
47
48
    /**
49
     * Form constructor.
50
     * @param Context $context
51
     * @param Registry $registry
52
     * @param FormFactory $formFactory
53
     * @param Store $systemStore
54
     * @param array $data
55
     */
56
    public function __construct(
57
        Context $context,
58
        Registry $registry,
59
        FormFactory $formFactory,
60
        Store $systemStore,
61
        array $data = []
62
    ) {
63
        parent::__construct($context, $registry, $formFactory, $data);
64
        $this->systemStore = $systemStore;
65
    }
66
67
    /**
68
     * @return void
69
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
70
     */
71
    protected function _construct(): void
72
    {
73
        parent::_construct();
74
        $this->setId('purgesingle_form');
75
        $this->setTitle(__('Varnish: purge single URL'));
76
    }
77
78
    /**
79
     * @return Form
80
     * @throws LocalizedException
81
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
82
     */
83
    protected function _prepareForm(): self
84
    {
85
        /** @var \Magento\Framework\Data\Form $form */
86
        $form = $this->_formFactory->create(
87
            [
88
                'data' => [
89
                    'id' => 'edit_form',
90
                    'action' => $this->getFormTargetUrl(),
91
                    'method' => 'post'
92
                ]
93
            ]
94
        );
95
96
        $form->setHtmlIdPrefix('purgesingle_');
97
98
        $fieldset = $form->addFieldset(
99
            'base_fieldset',
100
            [
101
                'class' => 'fieldset-wide'
102
            ]
103
        );
104
105
        $fieldset->addField(
106
            self::URL_FORM_PARAM,
107
            'text',
108
            [
109
                'name' => self::URL_FORM_PARAM,
110
                'label' => __('URL to purge'),
111
                'title' => __('URL to purge'),
112
                'required' => false,
113
                'note' => __('Relative URL, e.g. * or bizuteria. If empty, homepage will be purged')
114
115
            ]
116
        );
117
118
        $fieldset->addField(
119
            self::STORE_VIEW_FORM_PARAM,
120
            'select',
121
            [
122
                'name' => self::STORE_VIEW_FORM_PARAM,
123
                'label' => __('Store View'),
124
                'title' => __('Store View'),
125
                'required' => true,
126
                'values' => $this->systemStore->getStoreValuesForForm(false, true)
127
            ]
128
        );
129
130
        $fieldset->addField(
131
            self::FORCE_PURGE_FORM_PARAM,
132
            'checkbox',
133
            [
134
                'name' => self::FORCE_PURGE_FORM_PARAM,
135
                'label' => __('Force purge'),
136
                'title' => __('Force purge'),
137
                'required' => false,
138
            ]
139
        );
140
141
        $form->setUseContainer(true);
142
        $this->setForm($form);
143
144
        return parent::_prepareForm();
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    private function getFormTargetUrl(): string
151
    {
152
        return $this->_urlBuilder->getUrl('lizardmediavarnish/purgesingle/run');
153
    }
154
}
155