Completed
Push — master ( 5da019...46d695 )
by
unknown
19:03
created

getAdditionalFields()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 174
Code Lines 145

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 145
nc 196608
nop 3
dl 0
loc 174
rs 0
c 1
b 0
f 0

How to fix   Long Method    Complexity   

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
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Linkvalidator\Task;
19
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Core\Localization\LanguageService;
22
use TYPO3\CMS\Core\Messaging\FlashMessage;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
25
use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
26
use TYPO3\CMS\Scheduler\Task\AbstractTask;
27
use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
28
29
/**
30
 * This class provides Scheduler Additional Field plugin implementation
31
 * @internal This class is a specific Scheduler task implementation and is not part of the TYPO3's Core API.
32
 */
33
class ValidatorTaskAdditionalFieldProvider extends AbstractAdditionalFieldProvider
34
{
35
    /**
36
     * Default language file of the extension linkvalidator
37
     *
38
     * @var string
39
     */
40
    protected $languageFile = 'LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf';
41
42
    /**
43
     * Render additional information fields within the scheduler backend.
44
     *
45
     * @param array $taskInfo Array information of task to return
46
     * @param ValidatorTask|null $task The task object being edited. Null when adding a task!
47
     * @param SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
48
     * @return array Additional fields
49
     * @see AdditionalFieldProviderInterface::getAdditionalFields
50
     */
51
    public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule): array
52
    {
53
        $additionalFields = [];
54
        $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
55
        $lang = $this->getLanguageService();
56
57
        if (empty($taskInfo['configuration'])) {
58
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
59
                $taskInfo['configuration'] = $taskInfo['linkvalidator']['configuration'];
60
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
61
                $taskInfo['configuration'] = $task->getConfiguration();
0 ignored issues
show
Bug introduced by
The method getConfiguration() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
                /** @scrutinizer ignore-call */ 
62
                $taskInfo['configuration'] = $task->getConfiguration();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            } else {
63
                $taskInfo['configuration'] = $task->getConfiguration();
64
            }
65
        }
66
        if (empty($taskInfo['depth'])) {
67
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
68
                $taskInfo['depth'] = $taskInfo['linkvalidator']['depth'];
69
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
70
                $taskInfo['depth'] = $task->getDepth();
71
            } else {
72
                $taskInfo['depth'] = $task->getDepth();
73
            }
74
        }
75
        if (empty($taskInfo['page'])) {
76
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
77
                $taskInfo['page'] = $taskInfo['linkvalidator']['page'];
78
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
79
                $taskInfo['page'] = $task->getPage();
80
            } else {
81
                $taskInfo['page'] = $task->getPage();
82
            }
83
        }
84
        if (empty($taskInfo['languages'])) {
85
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
86
                $taskInfo['languages'] = $taskInfo['linkvalidator']['languages'];
87
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
88
                $taskInfo['languages'] = $task->getLanguages();
89
            } else {
90
                $taskInfo['languages'] = $task->getLanguages();
91
            }
92
        }
93
        if (empty($taskInfo['email'])) {
94
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
95
                $taskInfo['email'] = $taskInfo['linkvalidator']['email'];
96
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
97
                $taskInfo['email'] = $task->getEmail();
98
            } else {
99
                $taskInfo['email'] = $task->getEmail();
100
            }
101
        }
102
        if (empty($taskInfo['emailOnBrokenLinkOnly'])) {
103
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
104
                $taskInfo['emailOnBrokenLinkOnly'] = $taskInfo['linkvalidator']['emailOnBrokenLinkOnly'] ?: 1;
105
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
106
                $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
107
            } else {
108
                $taskInfo['emailOnBrokenLinkOnly'] = $task->getEmailOnBrokenLinkOnly();
109
            }
110
        }
111
        if (empty($taskInfo['emailTemplateName'])) {
112
            if ($currentSchedulerModuleAction->equals(Action::ADD)) {
113
                $taskInfo['emailTemplateName'] = $taskInfo['linkvalidator']['emailTemplateName'] ?: '';
114
            } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
115
                $taskInfo['emailTemplateName'] = $task->getEmailTemplateName();
116
            } else {
117
                $taskInfo['emailTemplateName'] = $task->getEmailTemplateName();
118
            }
119
        }
120
        $fieldId = 'task_page';
121
        $fieldCode = '<input type="number" min="0" class="form-control" name="tx_scheduler[linkvalidator][page]" id="'
122
            . $fieldId
123
            . '" value="'
124
            . htmlspecialchars((string)$taskInfo['page'])
125
            . '">';
126
        $label = $lang->sL($this->languageFile . ':tasks.validate.page');
127
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
128
        $pageTitle = '';
129
        if (!empty($taskInfo['page'])) {
130
            $pageTitle = $this->getPageTitle((int)$taskInfo['page']);
131
        }
132
        $additionalFields[$fieldId] = [
133
            'browser' => 'page',
134
            'pageTitle' => $pageTitle,
135
            'code' => $fieldCode,
136
            'label' => $label
137
        ];
138
        // input for depth
139
        $fieldId = 'task_depth';
140
        $fieldValueArray = [
141
            '0' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
142
            '1' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
143
            '2' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
144
            '3' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
145
            '4' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
146
            '999' => $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi')
147
        ];
148
        /** @var array<string, string> $fieldValueArray */
149
        $fieldCode = '<select class="form-control" name="tx_scheduler[linkvalidator][depth]" id="' . $fieldId . '">';
150
        foreach ($fieldValueArray as $depth => $label) {
151
            $fieldCode .= "\t" . '<option value="' . htmlspecialchars((string)$depth) . '"'
152
                . (($depth === $taskInfo['depth']) ? ' selected="selected"' : '') . '>'
153
                . $label
154
                . '</option>';
155
        }
156
        $fieldCode .= '</select>';
157
        $label = $lang->sL($this->languageFile . ':tasks.validate.depth');
158
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
159
        $additionalFields[$fieldId] = [
160
            'code' => $fieldCode,
161
            'label' => $label
162
        ];
163
        $fieldId = 'task_languages';
164
        $fieldCode = '<input class="form-control" type="text"  name="tx_scheduler[linkvalidator][languages]" '
165
            . 'id="'
166
            . $fieldId
167
            . '" value="'
168
            . htmlspecialchars((string)$taskInfo['languages'])
169
            . '">';
170
        $label = $lang->sL($this->languageFile . ':tasks.validate.languages');
171
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
172
        $additionalFields[$fieldId] = [
173
            'code' => $fieldCode,
174
            'label' => $label
175
        ];
176
        $fieldId = 'task_configuration';
177
        $fieldCode = '<textarea class="form-control" name="tx_scheduler[linkvalidator][configuration]" id="'
178
            . $fieldId
179
            . '" >'
180
            . htmlspecialchars((string)$taskInfo['configuration'])
181
            . '</textarea>';
182
        $label = $lang->sL($this->languageFile . ':tasks.validate.conf');
183
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
184
        $additionalFields[$fieldId] = [
185
            'code' => $fieldCode,
186
            'label' => $label
187
        ];
188
        $fieldId = 'task_email';
189
        $fieldCode = '<textarea class="form-control" rows="5" cols="50" name="tx_scheduler[linkvalidator][email]" id="'
190
            . $fieldId
191
            . '">'
192
            . htmlspecialchars((string)$taskInfo['email'])
193
            . '</textarea>';
194
        $label = $lang->sL($this->languageFile . ':tasks.validate.email');
195
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
196
        $additionalFields[$fieldId] = [
197
            'code' => $fieldCode,
198
            'label' => $label
199
        ];
200
        $fieldId = 'task_emailOnBrokenLinkOnly';
201
        $fieldCode = '<div class="checkbox"><label>'
202
            . '<input type="checkbox" name="tx_scheduler[linkvalidator][emailOnBrokenLinkOnly]" id="' . $fieldId . '" '
203
            . ((bool)$taskInfo['emailOnBrokenLinkOnly'] ? 'checked="checked"' : '')
204
            . '></label></div>';
205
        $label = $lang->sL($this->languageFile . ':tasks.validate.emailOnBrokenLinkOnly');
206
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
207
        $additionalFields[$fieldId] = [
208
            'code' => $fieldCode,
209
            'label' => $label
210
        ];
211
        $fieldId = 'task_emailTemplateName';
212
        $fieldCode = '<input class="form-control" type="text"  name="tx_scheduler[linkvalidator][emailTemplateName]" '
213
            . 'id="'
214
            . $fieldId
215
            . '" value="'
216
            . htmlspecialchars((string)$taskInfo['emailTemplateName'])
217
            . '">';
218
        $label = $lang->sL($this->languageFile . ':tasks.validate.emailTemplateName');
219
        $label = BackendUtility::wrapInHelp('linkvalidator', $fieldId, $label);
220
        $additionalFields[$fieldId] = [
221
            'code' => $fieldCode,
222
            'label' => $label
223
        ];
224
        return $additionalFields;
225
    }
226
227
    /**
228
     * This method checks any additional data that is relevant to the specific task.
229
     * If the task class is not relevant, the method is expected to return TRUE.
230
     *
231
     * @param array $submittedData Reference to the array containing the data submitted by the user
232
     * @param SchedulerModuleController $schedulerModule Reference to the BE module of the Scheduler
233
     * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
234
     */
235
    public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule): bool
236
    {
237
        $isValid = true;
238
        $lang = $this->getLanguageService();
239
        $email = (string)($submittedData['linkvalidator']['email'] ?? '');
240
        if ($email !== '') {
241
            $emailList = GeneralUtility::trimExplode((strpos($email, ',') !== false) ? ',' : LF, $email);
242
            foreach ($emailList as $emailAdd) {
243
                if (!GeneralUtility::validEmail($emailAdd)) {
244
                    $isValid = false;
245
                    $this->addMessage(
246
                        $lang->sL($this->languageFile . ':tasks.validate.invalidEmail'),
247
                        FlashMessage::ERROR
248
                    );
249
                }
250
            }
251
        }
252
253
        $row = BackendUtility::getRecord('pages', (int)$submittedData['linkvalidator']['page'], '*', '', false);
254
        if ($row === null) {
255
            $isValid = false;
256
            $this->addMessage(
257
                $lang->sL($this->languageFile . ':tasks.validate.invalidPage'),
258
                FlashMessage::ERROR
259
            );
260
        }
261
        if ((int)$submittedData['linkvalidator']['depth'] < 0) {
262
            $isValid = false;
263
            $this->addMessage(
264
                $lang->sL($this->languageFile . ':tasks.validate.invalidDepth'),
265
                FlashMessage::ERROR
266
            );
267
        }
268
        return $isValid;
269
    }
270
271
    /**
272
     * This method is used to save any additional input into the current task object
273
     * if the task class matches.
274
     *
275
     * @param array $submittedData Array containing the data submitted by the user
276
     * @param AbstractTask $task Reference to the current task object
277
     */
278
    public function saveAdditionalFields(array $submittedData, AbstractTask $task): void
279
    {
280
        $task
281
            ->setDepth((int)$submittedData['linkvalidator']['depth'])
0 ignored issues
show
Bug introduced by
The method setDepth() does not exist on TYPO3\CMS\Scheduler\Task\AbstractTask. It seems like you code against a sub-type of TYPO3\CMS\Scheduler\Task\AbstractTask such as TYPO3\CMS\Linkvalidator\Task\ValidatorTask. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

281
            ->/** @scrutinizer ignore-call */ 
282
              setDepth((int)$submittedData['linkvalidator']['depth'])
Loading history...
282
            ->setPage((int)$submittedData['linkvalidator']['page'])
283
            ->setLanguages($submittedData['linkvalidator']['languages'])
284
            ->setEmail($submittedData['linkvalidator']['email'])
285
            ->setEmailOnBrokenLinkOnly((bool)($submittedData['linkvalidator']['emailOnBrokenLinkOnly'] ?? false))
286
            ->setConfiguration($submittedData['linkvalidator']['configuration'])
287
            ->setEmailTemplateName($submittedData['linkvalidator']['emailTemplateName']);
288
    }
289
290
    /**
291
     * Get the title of the selected page
292
     *
293
     * @param int $pageId
294
     * @return string Page title or empty string
295
     */
296
    private function getPageTitle(int $pageId): string
297
    {
298
        return (string)(BackendUtility::getRecord('pages', $pageId, 'title', '', false)['title'] ?? '');
299
    }
300
301
    /**
302
     * @return LanguageService
303
     */
304
    protected function getLanguageService(): LanguageService
305
    {
306
        return $GLOBALS['LANG'];
307
    }
308
}
309