Passed
Push — wip/remove-deprecations-for-v1... ( f97f5b )
by Tomas Norre
05:15
created

StartRequestForm   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 62.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 97
c 1
b 0
f 0
dl 0
loc 200
ccs 77
cts 124
cp 0.621
rs 10
wmc 26

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getScheduledTime() 0 15 4
A render() 0 3 1
B showCrawlerInformationAction() 0 67 7
A generateConfigurationSelectors() 0 39 3
B selectorBox() 0 14 10
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Backend\RequestForm;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
use AOE\Crawler\Controller\CrawlerController;
23
use AOE\Crawler\Domain\Model\Reason;
24
use AOE\Crawler\Utility\MessageUtility;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Fluid\View\StandaloneView;
27
use TYPO3\CMS\Info\Controller\InfoModuleController;
28
29
final class StartRequestForm extends AbstractRequestForm implements RequestFormInterface
30
{
31
    /**
32
     * @var StandaloneView
33
     */
34
    private $view;
35
36
    /**
37
     * @var InfoModuleController
38
     */
39
    private $infoModuleController;
40
41
    /**
42
     * @var int
43
     */
44
    private $reqMinute = 1000;
45
46
    /**
47
     * @var array holds the selection of configuration from the configuration selector box
48
     */
49
    private $incomingConfigurationSelection = [];
50
51 2
    public function __construct(StandaloneView $view, InfoModuleController $infoModuleController, array $extensionSettings)
52
    {
53 2
        $this->view = $view;
54 2
        $this->infoModuleController = $infoModuleController;
55 2
        $this->extensionSettings = $extensionSettings;
56 2
    }
57
58 1
    public function render($id, string $elementName, array $menuItems): string
59
    {
60 1
        return $this->showCrawlerInformationAction($id);
61
    }
62
63
    /*******************************
64
     *
65
     * Generate URLs for crawling:
66
     *
67
     ******************************/
68
69
    /**
70
     * Show a list of URLs to be crawled for each page
71
     */
72 1
    private function showCrawlerInformationAction(int $pageId): string
73
    {
74 1
        if (empty($pageId)) {
75
            $this->isErrorDetected = true;
76
            MessageUtility::addErrorMessage($this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.noPageSelected'));
77
            return '';
78
        }
79
80 1
        $this->view->setTemplate('ShowCrawlerInformation');
81
82 1
        $crawlParameter = GeneralUtility::_GP('_crawl');
83 1
        $downloadParameter = GeneralUtility::_GP('_download');
84
85 1
        $submitCrawlUrls = isset($crawlParameter);
86 1
        $downloadCrawlUrls = isset($downloadParameter);
87 1
        $this->makeCrawlerProcessableChecks($this->extensionSettings);
88
89 1
        $scheduledTime = $this->getScheduledTime((string) GeneralUtility::_GP('tstamp'));
90
91 1
        $this->incomingConfigurationSelection = GeneralUtility::_GP('configurationSelection');
92 1
        $this->incomingConfigurationSelection = is_array($this->incomingConfigurationSelection) ? $this->incomingConfigurationSelection : [];
93
94 1
        $this->crawlerController = GeneralUtility::makeInstance(CrawlerController::class);
95 1
        $this->crawlerController->setID = GeneralUtility::md5int(microtime());
96
97 1
        $code = '';
98 1
        $noConfigurationSelected = empty($this->incomingConfigurationSelection)
99 1
            || (count($this->incomingConfigurationSelection) === 1 && empty($this->incomingConfigurationSelection[0]));
100 1
        if ($noConfigurationSelected) {
101 1
            MessageUtility::addWarningMessage($this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.noConfigSelected'));
102
        } else {
103
            $code = $this->crawlerController->getPageTreeAndUrls(
104
                $pageId,
105
                $this->infoModuleController->MOD_SETTINGS['depth'],
106
                $scheduledTime,
107
                $this->reqMinute,
108
                $submitCrawlUrls,
109
                $downloadCrawlUrls,
110
                // Do not filter any processing instructions
111
                [],
112
                $this->incomingConfigurationSelection
113
            );
114
        }
115
116 1
        $downloadUrls = $this->crawlerController->downloadUrls;
117
118 1
        $duplicateTrack = $this->crawlerController->duplicateTrack;
119
120 1
        $this->view->assign('noConfigurationSelected', $noConfigurationSelected);
121 1
        $this->view->assign('submitCrawlUrls', $submitCrawlUrls);
122 1
        $this->view->assign('amountOfUrls', count($duplicateTrack));
123 1
        $this->view->assign('selectors', $this->generateConfigurationSelectors($pageId));
124 1
        $this->view->assign('code', $code);
125 1
        $this->view->assign('displayActions', 0);
126
127
        // Download Urls to crawl:
128 1
        if ($downloadCrawlUrls) {
129
            // Creating output header:
130
            header('Content-Type: application/octet-stream');
131
            header('Content-Disposition: attachment; filename=CrawlerUrls.txt');
132
133
            // Printing the content of the CSV lines:
134
            echo implode(chr(13) . chr(10), $downloadUrls);
135
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
136
        }
137
138 1
        return $this->view->render();
139
    }
140
141
    /**
142
     * Generates the configuration selectors for compiling URLs:
143
     */
144 1
    private function generateConfigurationSelectors(int $pageId): array
145
    {
146 1
        $selectors = [];
147 1
        $selectors['depth'] = $this->selectorBox(
148
            [
149 1
                0 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
150 1
                1 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
151 1
                2 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
152 1
                3 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
153 1
                4 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
154 1
                99 => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.depth_infi'),
155
            ],
156 1
            'SET[depth]',
157 1
            $this->infoModuleController->MOD_SETTINGS['depth'],
158 1
            false
159
        );
160
161
        // Configurations
162 1
        $availableConfigurations = $this->crawlerController->getConfigurationsForBranch($pageId, (int) $this->infoModuleController->MOD_SETTINGS['depth'] ?: 0);
163 1
        $selectors['configurations'] = $this->selectorBox(
164 1
            empty($availableConfigurations) ? [] : array_combine($availableConfigurations, $availableConfigurations),
0 ignored issues
show
Bug introduced by
It seems like empty($availableConfigur...vailableConfigurations) can also be of type false; however, parameter $optArray of AOE\Crawler\Backend\Requ...uestForm::selectorBox() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

164
            /** @scrutinizer ignore-type */ empty($availableConfigurations) ? [] : array_combine($availableConfigurations, $availableConfigurations),
Loading history...
165 1
            'configurationSelection',
166 1
            $this->incomingConfigurationSelection,
167 1
            true
168
        );
169
170
        // Scheduled time:
171 1
        $selectors['scheduled'] = $this->selectorBox(
172
            [
173 1
                'now' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.time.now'),
174 1
                'midnight' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.time.midnight'),
175 1
                '04:00' => $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.time.4am'),
176
            ],
177 1
            'tstamp',
178 1
            GeneralUtility::_POST('tstamp'),
179 1
            false
180
        );
181
182 1
        return $selectors;
183
    }
184
185
    /**
186
     * Create selector box
187
     *
188
     * @param array $optArray Options key(value) => label pairs
189
     * @param string $name Selector box name
190
     * @param string|array $value Selector box value (array for multiple...)
191
     * @param boolean $multiple If set, will draw multiple box.
192
     *
193
     * @return string HTML select element
194
     */
195 1
    private function selectorBox($optArray, $name, $value, bool $multiple): string
196
    {
197 1
        if (! is_string($value) || ! is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always false.
Loading history...
198 1
            $value = '';
199
        }
200
201 1
        $options = [];
202 1
        foreach ($optArray as $key => $val) {
203 1
            $selected = (! $multiple && ! strcmp($value, (string) $key)) || ($multiple && in_array($key, (array) $value, true));
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $selected = (! $multiple..., (array)$value, true)), Probably Intended Meaning: $selected = ! $multiple ..., (array)$value, true))
Loading history...
204
            $options[] = '
205 1
                <option value="' . $key . '" ' . ($selected ? ' selected="selected"' : '') . '>' . htmlspecialchars($val, ENT_QUOTES | ENT_HTML5) . '</option>';
206
        }
207
208 1
        return '<select class="form-control" name="' . htmlspecialchars($name . ($multiple ? '[]' : ''), ENT_QUOTES | ENT_HTML5) . '"' . ($multiple ? ' multiple' : '') . '>' . implode('', $options) . '</select>';
209
    }
210
211
    /**
212
     * @return int
213
     */
214 1
    private function getScheduledTime(string $time)
215
    {
216 1
        switch ($time) {
217 1
            case 'midnight':
218
                $scheduledTime = mktime(0, 0, 0);
219
                break;
220 1
            case '04:00':
221
                $scheduledTime = mktime(0, 0, 0) + 4 * 3600;
222
                break;
223 1
            case 'now':
224
            default:
225 1
                $scheduledTime = time();
226 1
                break;
227
        }
228 1
        return $scheduledTime;
229
    }
230
}
231