Completed
Push — bugfix/task-null ( 754cd5 )
by Tomas Norre
33:10 queued 27:51
created

getAdditionalFields()   C

Complexity

Conditions 9
Paths 108

Size

Total Lines 64
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 42
nc 108
nop 3
dl 0
loc 64
rs 6.3396
c 0
b 0
f 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
namespace AOE\Crawler\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Backend\Utility\BackendUtility;
29
use TYPO3\CMS\Core\Messaging\FlashMessage;
30
use TYPO3\CMS\Core\Utility\MathUtility;
31
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
32
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
33
use TYPO3\CMS\Scheduler\Task\AbstractTask;
34
35
/**
36
 * Class CrawlerQueueTaskAdditionalFieldProvider
37
 *
38
 * @package AOE\Crawler\Task
39
 * @codeCoverageIgnore
40
 */
41
class CrawlerQueueTaskAdditionalFieldProvider implements AdditionalFieldProviderInterface
42
{
43
    /**
44
     * Gets additional fields to render in the form to add/edit a task
45
     *
46
     * @param array $taskInfo
47
     * @param CrawlerQueueTask $task
48
     * @param SchedulerModuleController $schedulerModule
49
     *
50
     * @return array
51
     */
52
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
53
    {
54
        $additionalFields = [];
55
56
        if ($schedulerModule->CMD == 'add') {
57
            $taskInfo['startPage'] = $taskInfo['startPage'] ?: 0;
58
            $taskInfo['configuration'] = $taskInfo['configuration'] ?: [];
59
            $taskInfo['depth'] = $taskInfo['depth'] ?: 0;
60
        }
61
62
        if ($schedulerModule->CMD == 'edit') {
63
            $taskInfo['startPage'] = $task->startPage;
64
            $taskInfo['configuration'] = $task->configuration;
65
            $taskInfo['depth'] = $task->depth;
66
        }
67
68
        // input for startPage
69
        $fieldId = 'task_startPage';
70
        $fieldCode = '<input name="tx_scheduler[startPage]" type="text" id="' . $fieldId . '" value="' . $task->startPage . '" class="form-control" />';
71
        $additionalFields[$fieldId] = [
72
            'code' => $fieldCode,
73
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.startPage'
74
        ];
75
76
        // input for depth
77
        $fieldId = 'task_depth';
78
        $fieldValueArray = [
79
            '0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
80
            '1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
81
            '2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
82
            '3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
83
            '4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
84
            '99' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
85
        ];
86
        $fieldCode = '<select name="tx_scheduler[depth]" id="' . $fieldId . '" class="form-control">';
87
88
        foreach ($fieldValueArray as $key => $label) {
89
            $fieldCode .= "\t" . '<option value="' . $key . '"' . (($key == $task->depth) ? ' selected="selected"' : '') . '>' . $label . '</option>';
90
        }
91
92
        $fieldCode .= '</select>';
93
        $additionalFields[$fieldId] = [
94
            'code' => $fieldCode,
95
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.depth'
96
        ];
97
98
        // combobox for configuration records
99
        $recordsArray = $this->getCrawlerConfigurationRecords();
100
        $fieldId = 'task_configuration';
101
        $fieldCode = '<select name="tx_scheduler[configuration][]" multiple="multiple" id="' . $fieldId . '" class="form-control">';
102
        $fieldCode .= "\t" . '<option value=""></option>';
103
        $arraySize = count($recordsArray);
104
        for ($i = 0; $i < $arraySize; $i++) {
105
            $fieldCode .= "\t" . '<option ' . $this->getSelectedState($task->configuration, $recordsArray[$i]['name']) . 'value="' . $recordsArray[$i]['name'] . '">' . $recordsArray[$i]['name'] . '</option>';
0 ignored issues
show
Documentation introduced by
$task->configuration is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
        }
107
        $fieldCode .= '</select>';
108
109
        $additionalFields[$fieldId] = [
110
            'code' => $fieldCode,
111
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.conf'
112
        ];
113
114
        return $additionalFields;
115
    }
116
117
    /**
118
     * Mark current value as selected by returning the "selected" attribute
119
     *
120
     * @param array $configurationArray
121
     * @param string $currentValue
122
     *
123
     * @return string
124
     */
125
    protected function getSelectedState(array $configurationArray, $currentValue)
126
    {
127
        $selected = '';
128
        $arraySize = count($configurationArray);
129
        for ($i = 0; $i < $arraySize; $i++) {
130
            if (strcmp($configurationArray[$i], $currentValue) === 0) {
131
                $selected = 'selected="selected" ';
132
            }
133
        }
134
135
        return $selected;
136
    }
137
138
    /**
139
     * Get all available configuration records.
140
     *
141
     * @return array which contains the available configuration records.
142
     */
143
    protected function getCrawlerConfigurationRecords()
144
    {
145
        $records = [];
146
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
147
            '*',
148
            'tx_crawler_configuration',
149
            '1=1' . BackendUtility::deleteClause('tx_crawler_configuration')
150
        );
151
152
        while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
153
            $records[] = $row;
154
        }
155
        $GLOBALS['TYPO3_DB']->sql_free_result($result);
156
157
        return $records;
158
    }
159
160
    /**
161
     * Validates the additional fields' values
162
     *
163
     * @param array $submittedData
164
     * @param SchedulerModuleController $schedulerModule
165
     * @return bool
166
     */
167
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
168
    {
169
        $isValid = false;
170
171
        //!TODO add validation to validate the $submittedData['configuration'] which is normally a comma separated string
172
        if (is_array($submittedData['configuration'])) {
173
            $isValid = true;
174
        } else {
175
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidConfiguration'), FlashMessage::ERROR);
176
        }
177
178
        if ($submittedData['depth'] < 0) {
179
            $isValid = false;
180
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidDepth'), FlashMessage::ERROR);
181
        }
182
183
        if (!MathUtility::canBeInterpretedAsInteger($submittedData['startPage']) || $submittedData['startPage'] < 0) {
184
            $isValid = false;
185
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidStartPage'), FlashMessage::ERROR);
186
        }
187
188
        return $isValid;
189
    }
190
191
    /**
192
     * Takes care of saving the additional fields' values in the task's object
193
     *
194
     * @param array $submittedData
195
     * @param CrawlerQueueTask|AbstractTask $task
196
     *
197
     * @return void
198
     */
199
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
200
    {
201
        $task->depth = intval($submittedData['depth']);
0 ignored issues
show
Bug introduced by
The property depth does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
202
        $task->configuration = $submittedData['configuration'];
0 ignored issues
show
Bug introduced by
The property configuration does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
203
        $task->startPage = intval($submittedData['startPage']);
0 ignored issues
show
Bug introduced by
The property startPage does not seem to exist in TYPO3\CMS\Scheduler\Task\AbstractTask.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
204
    }
205
}
206