Test Failed
Push — 6-0 ( cfb4d5...b26d37 )
by
unknown
04:59
created

CrawlerQueueTaskAdditionalFieldProvider   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 181
rs 10
c 0
b 0
f 0
wmc 25

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectedState() 0 10 3
A getCrawlerConfigurationRecords() 0 15 2
F getAdditionalFields() 0 83 14
A saveAdditionalFields() 0 5 1
B validateAdditionalFields() 0 22 5
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;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Utility\BackendUtility 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...
29
use TYPO3\CMS\Core\Messaging\FlashMessage;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Messaging\FlashMessage 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...
30
use TYPO3\CMS\Core\Utility\MathUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Utility\MathUtility 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...
31
use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Scheduler\Addi...lFieldProviderInterface 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...
32
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Scheduler\Cont...hedulerModuleController 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...
33
use TYPO3\CMS\Scheduler\Task\AbstractTask;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Scheduler\Task\AbstractTask 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...
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 AbstractTask $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 (empty($taskInfo['configuration'])) {
57
            if ($schedulerModule->CMD == 'add') {
58
                $taskInfo['configuration'] = [];
59
            } elseif ($schedulerModule->CMD == 'edit') {
60
                $taskInfo['configuration'] = $task->configuration;
61
            } else {
62
                $taskInfo['configuration'] = $task->configuration;
63
            }
64
        }
65
66
        if (empty($taskInfo['startPage'])) {
67
            if ($schedulerModule->CMD == 'add') {
68
                $taskInfo['startPage'] = 0;
69
                if ($task instanceof \TYPO3\CMS\Scheduler\Task\AbstractTask) {
70
                    $task->startPage = 0;
71
                }
72
            } elseif ($schedulerModule->CMD == 'edit') {
73
                $taskInfo['startPage'] = $task->startPage;
74
            } else {
75
                $taskInfo['startPage'] = $task->startPage;
76
            }
77
        }
78
79
        if (empty($taskInfo['depth'])) {
80
            if ($schedulerModule->CMD == 'add') {
81
                $taskInfo['depth'] = [];
82
            } elseif ($schedulerModule->CMD == 'edit') {
83
                $taskInfo['depth'] = $task->depth;
84
            } else {
85
                $taskInfo['depth'] = $task->depth;
86
            }
87
        }
88
89
        // input for startPage
90
        $fieldId = 'task_startPage';
91
        $fieldCode = '<input name="tx_scheduler[startPage]" type="text" id="' . $fieldId . '" value="' . $task->startPage . '" class="form-control" />';
92
        $additionalFields[$fieldId] = [
93
            'code' => $fieldCode,
94
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.startPage'
95
        ];
96
97
        // input for depth
98
        $fieldId = 'task_depth';
99
        $fieldValueArray = [
100
            '0' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
101
            '1' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
102
            '2' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
103
            '3' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
104
            '4' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
105
            '99' => $GLOBALS['LANG']->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
106
        ];
107
        $fieldCode = '<select name="tx_scheduler[depth]" id="' . $fieldId . '" class="form-control">';
108
109
        foreach ($fieldValueArray as $key => $label) {
110
            $fieldCode .= "\t" . '<option value="' . $key . '"' . (($key == $task->depth) ? ' selected="selected"' : '') . '>' . $label . '</option>';
111
        }
112
113
        $fieldCode .= '</select>';
114
        $additionalFields[$fieldId] = [
115
            'code' => $fieldCode,
116
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.depth'
117
        ];
118
119
        // combobox for configuration records
120
        $recordsArray = $this->getCrawlerConfigurationRecords();
121
        $fieldId = 'task_configuration';
122
        $fieldCode = '<select name="tx_scheduler[configuration][]" multiple="multiple" id="' . $fieldId . '" class="form-control">';
123
        $fieldCode .= "\t" . '<option value=""></option>';
124
        for ($i = 0; $i < count($recordsArray); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
125
            $fieldCode .= "\t" . '<option ' . $this->getSelectedState($task->configuration, $recordsArray[$i]['name']) . 'value="' . $recordsArray[$i]['name'] . '">' . $recordsArray[$i]['name'] . '</option>';
126
        }
127
        $fieldCode .= '</select>';
128
129
        $additionalFields[$fieldId] = [
130
            'code' => $fieldCode,
131
            'label' => 'LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.conf'
132
        ];
133
134
        return $additionalFields;
135
    }
136
137
    /**
138
     * Mark current value as selected by returning the "selected" attribute
139
     *
140
     * @param $configurationArray
141
     * @param $currentValue
142
     *
143
     * @return string
144
     */
145
    protected function getSelectedState($configurationArray, $currentValue)
146
    {
147
        $selected = '';
148
        for ($i = 0; $i < count($configurationArray); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
149
            if (strcmp($configurationArray[$i], $currentValue) === 0) {
150
                $selected = 'selected="selected" ';
151
            }
152
        }
153
154
        return $selected;
155
    }
156
157
    /**
158
     * Get all available configuration records.
159
     *
160
     * @return array which contains the available configuration records.
161
     */
162
    protected function getCrawlerConfigurationRecords()
163
    {
164
        $records = [];
165
        $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
166
            '*',
167
            'tx_crawler_configuration',
168
            '1=1' . BackendUtility::deleteClause('tx_crawler_configuration')
169
        );
170
171
        while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
172
            $records[] = $row;
173
        }
174
        $GLOBALS['TYPO3_DB']->sql_free_result($result);
175
176
        return $records;
177
    }
178
179
    /**
180
     * Validates the additional fields' values
181
     *
182
     * @param array $submittedData
183
     * @param SchedulerModuleController $schedulerModule
184
     * @return bool
185
     */
186
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
187
    {
188
        $isValid = false;
189
190
        //!TODO add validation to validate the $submittedData['configuration'] which is normally a comma separated string
191
        if (is_array($submittedData['configuration'])) {
192
            $isValid = true;
193
        } else {
194
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidConfiguration'), FlashMessage::ERROR);
195
        }
196
197
        if ($submittedData['depth'] < 0) {
198
            $isValid = false;
199
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidDepth'), FlashMessage::ERROR);
200
        }
201
202
        if (!MathUtility::canBeInterpretedAsInteger($submittedData['startPage']) || $submittedData['startPage'] < 0) {
203
            $isValid = false;
204
            $schedulerModule->addMessage($GLOBALS['LANG']->sL('LLL:EXT:crawler/Resources/Private/Language/Backend.xlf:crawler_im.invalidStartPage'), FlashMessage::ERROR);
205
        }
206
207
        return $isValid;
208
    }
209
210
    /**
211
     * Takes care of saving the additional fields' values in the task's object
212
     *
213
     * @param array $submittedData
214
     * @param AbstractTask $task
215
     * @return void
216
     */
217
    public function saveAdditionalFields(array $submittedData, AbstractTask $task)
218
    {
219
        $task->depth = intval($submittedData['depth']);
220
        $task->configuration = $submittedData['configuration'];
221
        $task->startPage = intval($submittedData['startPage']);
222
    }
223
}
224