Parameters::canShowTab()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Tkotosz\CommandScheduler\Block\Adminhtml\Schedule\Create\From\Tab;
4
5
use Tkotosz\CommandScheduler\Model\CommandProvider;
6
use Magento\Backend\Block\Widget\Form\Generic;
0 ignored issues
show
Bug introduced by
The type Magento\Backend\Block\Widget\Form\Generic was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Magento\Backend\Block\Widget\Tab\TabInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Backend\Block\Widget\Tab\TabInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class Parameters extends Generic implements TabInterface
10
{
11
    /**
12
     * @var CommandProvider
13
     */
14
    private $commandProvider;
15
16
    /**
17
     * @param \Magento\Backend\Block\Template\Context $context
18
     * @param \Magento\Framework\Registry             $registry
19
     * @param \Magento\Framework\Data\FormFactory     $formFactory
20
     * @param CommandProvider                         $commandProvider
21
     * @param array                                   $data
22
     */
23
    public function __construct(
24
        \Magento\Backend\Block\Template\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Backend\Block\Template\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
        \Magento\Framework\Registry $registry,
26
        \Magento\Framework\Data\FormFactory $formFactory,
27
        CommandProvider $commandProvider,
28
        array $data = []
29
    ) {
30
        $this->commandProvider = $commandProvider;
31
32
        parent::__construct($context, $registry, $formFactory, $data);
33
    }
34
35
    /**
36
     * Prepare label for tab
37
     *
38
     * @return \Magento\Framework\Phrase
39
     */
40
    public function getTabLabel()
41
    {
42
        return __('Parameters');
43
    }
44
45
    /**
46
     * Prepare title for tab
47
     *
48
     * @return \Magento\Framework\Phrase
49
     */
50
    public function getTabTitle()
51
    {
52
        return __('Parameters');
53
    }
54
55
    /**
56
     * Returns status flag about this tab can be showen or not
57
     *
58
     * @return true
59
     */
60
    public function canShowTab()
61
    {
62
        $commandName = $this->getFormData()->getData('command');
63
64
        return $commandName !== null && $this->commandProvider->getByName($commandName) !== null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $commandName !== ...($commandName) !== null returns the type boolean which is incompatible with the documented return type true.
Loading history...
65
    }
66
67
    /**
68
     * Returns status flag about this tab hidden or not
69
     *
70
     * @return bool
71
     */
72
    public function isHidden()
73
    {
74
        return false;
75
    }
76
77
    protected function _prepareForm()
78
    {
79
        /** @var \Magento\Framework\Data\Form $form */
80
        $form = $this->_formFactory->create(
81
            ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
82
        );
83
        $command = $this->commandProvider->getByName($this->getFormData()->getData('command'));
84
85
        $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Command')]);
86
87
        $baseFieldset->addField(
88
            'command_name',
89
            'hidden',
90
            [
91
                'name' => 'command_name',
92
                'label' => __('Command Name'),
93
                'title' => __('Command Name'),
94
                'value' => $command->getName()
95
            ]
96
        );
97
98
        $baseFieldset->addField(
99
            'info_command_name',
100
            'text',
101
            [
102
                'name' => 'info_command_name',
103
                'label' => __('Command Name'),
104
                'title' => __('Command Name'),
105
                'value' => $command->getName(),
106
                'disabled' => true
107
            ]
108
        );
109
110
        $baseFieldset->addField(
111
            'info_command_description',
112
            'text',
113
            [
114
                'name' => 'info_command_description',
115
                'label' => __('Command Description'),
116
                'title' => __('Command Description'),
117
                'value' => $command->getDescription(),
118
                'disabled' => true
119
            ]
120
        );
121
122
        if ($command->getDefinition()->getOptions()) {
123
            $optionsFieldset = $form->addFieldset('options_fieldset', ['legend' => __('Options')]);
124
            $dependencyMap = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Form\Element\Dependence');
125
            $this->setChild('form_after', $dependencyMap);
126
127
            foreach ($command->getDefinition()->getOptions() as $option) {
128
                if ($option->acceptValue()) {
129
                    $includeFieldName = sprintf('include_%s', $option->getName());
130
                    $valueFieldName = sprintf('command_params[--%s]', $option->getName());
131
                    $optionsFieldset->addField(
132
                        $includeFieldName,
133
                        'select',
134
                        [
135
                            'name' => $includeFieldName,
136
                            'label' => $option->getName(),
137
                            'title' => $option->getName(),
138
                            'required' => false,
139
                            'options' => ['0' => __('Exclude'), '1' => __('Include')],
140
                            'note' => $option->getDescription()
141
                        ]
142
                    );
143
144
                    $optionsFieldset->addField(
145
                        $valueFieldName,
146
                        'text',
147
                        [
148
                            'name' => $valueFieldName,
149
                            'label' => sprintf('"%s" option value', $option->getName()),
150
                            'title' => sprintf('"%s" option value', $option->getName()),
151
                            'required' => $option->isValueRequired(),
152
                            'value' => $option->acceptValue() ? ($option->getDefault() ?: '') : ''
153
                        ]
154
                    );
155
156
                    $dependencyMap->addFieldMap($includeFieldName, $includeFieldName);
157
                    $dependencyMap->addFieldMap($valueFieldName, $valueFieldName);
158
                    $dependencyMap->addFieldDependence($valueFieldName, $includeFieldName, '1');
159
                } else {
160
                    $valueFieldName = sprintf('command_params[--%s]', $option->getName());
161
                    $optionsFieldset->addField(
162
                        $valueFieldName,
163
                        'select',
164
                        [
165
                            'name' => $valueFieldName,
166
                            'label' => $option->getName(),
167
                            'title' => $option->getName(),
168
                            'required' => false,
169
                            'options' => ['0' => __('Exclude'), '1' => __('Include')],
170
                            'note' => $option->getDescription()
171
                        ]
172
                    );
173
                }
174
            }
175
        }
176
177
        if ($command->getDefinition()->getArguments()) {
178
            $argumentsFieldset = $form->addFieldset('arguments_fieldset', ['legend' => __('Arguments')]);
179
180
            foreach ($command->getDefinition()->getArguments() as $argument) {
181
                $argumentsFieldset->addField(
182
                    sprintf('command_params[%s]', $argument->getName()),
183
                    'text',
184
                    [
185
                        'name' => sprintf('command_params[%s]', $argument->getName()),
186
                        'label' => $argument->getName(),
187
                        'title' => $argument->getName(),
188
                        'required' => $argument->isRequired(),
189
                        'value' => $argument->getDefault() ?: '',
190
                        'note' => $argument->getDescription()
191
                    ]
192
                );            
193
            }
194
        }
195
196
        $this->setForm($form);
197
198
        return parent::_prepareForm();
199
    }
200
201
    public function getFormData()
202
    {
203
        return $this->_coreRegistry->registry('tkotosz_command_scheduler_data_container');
204
    }
205
}
206