Passed
Push — refactor/backendModule-ValueOb... ( 355cec...0f77d0 )
by Tomas Norre
13:40
created

LogRequestForm::getDepthDropDownHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace AOE\Crawler\Backend\RequestForm;
5
6
use AOE\Crawler\Controller\CrawlerController;
7
use AOE\Crawler\Csv\CsvWriter;
0 ignored issues
show
Bug introduced by
The type AOE\Crawler\Csv\CsvWriter 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
use AOE\Crawler\Utility\MessageUtility;
9
use TYPO3\CMS\Backend\Tree\View\PageTreeView;
10
use TYPO3\CMS\Backend\Utility\BackendUtility;
11
use TYPO3\CMS\Core\Imaging\Icon;
12
use TYPO3\CMS\Core\Localization\LanguageService;
13
use TYPO3\CMS\Core\Utility\DebugUtility;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Fluid\View\StandaloneView;
16
17
final class LogRequestForm implements RequestForm
18
{
19
    /** @var StandaloneView */
20
    private $view;
21
22
    public function __construct(StandaloneView $view)
23
    {
24
        $this->view = $view;
25
        // TODO: Implement CSV Writer
26
27
    }
28
29
    public function render($id, string $currentValue, array $menuItems): string
30
    {
31
        $quiPart = GeneralUtility::_GP('qid_details') ? '&qid_details=' . (int)GeneralUtility::_GP('qid_details') : '';
32
        $setId = (int)GeneralUtility::_GP('setID');
33
34
        return $this->getDepthDropDownHtml($id, $currentValue, $menuItems)
35
            . $this->showLogAction($setId, $quiPart);
36
    }
37
38
    private function getDepthDropDownHtml($id, string $currentValue, array $menuItems): string
39
    {
40
        return BackendUtility::getFuncMenu(
41
            $id,
42
            'SET[depth]',
43
            $currentValue,
44
            $menuItems
45
        );
46
    }
47
48
    /*******************************
49
     *
50
     * Shows log of indexed URLs
51
     *
52
     ******************************/
53
54
    /**
55
     * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
56
     */
57
    private function showLogAction(int $setId, string $quiPath): string
58
    {
59
        $this->view->setTemplate('ShowLog');
60
        if (empty($this->id)) {
61
            $this->isErrorDetected = true;
0 ignored issues
show
Bug Best Practice introduced by
The property isErrorDetected does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
            MessageUtility::addErrorMessage($this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.noPageSelected'));
63
        } else {
64
            $this->crawlerController = GeneralUtility::makeInstance(CrawlerController::class);
0 ignored issues
show
Bug Best Practice introduced by
The property crawlerController does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
65
            $this->crawlerController->setAccessMode('gui');
66
            $this->crawlerController->setID = GeneralUtility::md5int(microtime());
67
68
            $csvExport = GeneralUtility::_POST('_csv');
69
            $this->CSVExport = isset($csvExport);
0 ignored issues
show
Bug Best Practice introduced by
The property CSVExport does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
71
            // Read URL:
72
            if (GeneralUtility::_GP('qid_read')) {
73
                $this->crawlerController->readUrl((int) GeneralUtility::_GP('qid_read'), true);
74
            }
75
76
            // Look for set ID sent - if it is, we will display contents of that set:
77
            $showSetId = (int) GeneralUtility::_GP('setID');
78
79
            $queueId = GeneralUtility::_GP('qid_details');
80
            $this->view->assign('queueId', $queueId);
81
            $this->view->assign('setId', $showSetId);
82
            // Show details:
83
            if ($queueId) {
84
                // Get entry record:
85
                $q_entry = $this->queryBuilder
0 ignored issues
show
Bug Best Practice introduced by
The property queryBuilder does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
86
                    ->from('tx_crawler_queue')
87
                    ->select('*')
88
                    ->where(
89
                        $this->queryBuilder->expr()->eq('qid', $this->queryBuilder->createNamedParameter($queueId))
90
                    )
91
                    ->execute()
92
                    ->fetch();
93
94
                // Explode values
95
                $q_entry['parameters'] = $this->jsonCompatibilityConverter->convert($q_entry['parameters']);
0 ignored issues
show
Bug Best Practice introduced by
The property jsonCompatibilityConverter does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
96
                $q_entry['result_data'] = $this->jsonCompatibilityConverter->convert($q_entry['result_data']);
97
                $resStatus = $this->getResStatus($q_entry['result_data']);
0 ignored issues
show
Bug introduced by
The method getResStatus() does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. ( Ignorable by Annotation )

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

97
                /** @scrutinizer ignore-call */ 
98
                $resStatus = $this->getResStatus($q_entry['result_data']);

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...
98
                if (is_array($q_entry['result_data'])) {
99
                    $q_entry['result_data']['content'] = $this->jsonCompatibilityConverter->convert($q_entry['result_data']['content']);
100
                    if (! $this->pObj->MOD_SETTINGS['log_resultLog']) {
0 ignored issues
show
Bug Best Practice introduced by
The property pObj does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
101
                        unset($q_entry['result_data']['content']['log']);
102
                    }
103
                }
104
105
                $this->view->assign('queueStatus', $resStatus);
106
                $this->view->assign('queueDetails', DebugUtility::viewArray($q_entry));
107
            } else {
108
                // Show list
109
                // Drawing tree:
110
                $tree = GeneralUtility::makeInstance(PageTreeView::class);
111
                $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
112
                $tree->init('AND ' . $perms_clause);
113
114
                // Set root row:
115
                $pageinfo = BackendUtility::readPageAccess(
116
                    $this->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
117
                    $perms_clause
118
                );
119
                $HTML = $this->iconFactory->getIconForRecord('pages', $pageinfo, Icon::SIZE_SMALL)->render();
0 ignored issues
show
Bug Best Practice introduced by
The property iconFactory does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
120
                $tree->tree[] = [
121
                    'row' => $pageinfo,
122
                    'HTML' => $HTML,
123
                ];
124
125
                // Get branch beneath:
126
                if ($this->pObj->MOD_SETTINGS['depth']) {
127
                    $tree->getTree($this->id, $this->pObj->MOD_SETTINGS['depth']);
128
                }
129
130
                // If Flush button is pressed, flush tables instead of selecting entries:
131
                if (GeneralUtility::_POST('_flush')) {
132
                    $doFlush = true;
133
                    $doFullFlush = false;
134
                } elseif (GeneralUtility::_POST('_flush_all')) {
135
                    $doFlush = true;
136
                    $doFullFlush = true;
137
                } else {
138
                    $doFlush = false;
139
                    $doFullFlush = false;
140
                }
141
                $itemsPerPage = (int) $this->pObj->MOD_SETTINGS['itemsPerPage'];
142
                // Traverse page tree:
143
                $code = '';
144
                $count = 0;
145
                foreach ($tree->tree as $data) {
146
                    // Get result:
147
                    $logEntriesOfPage = $this->crawlerController->getLogEntriesForPageId(
148
                        (int) $data['row']['uid'],
149
                        $this->pObj->MOD_SETTINGS['log_display'],
150
                        $doFlush,
151
                        $doFullFlush,
152
                        $itemsPerPage
153
                    );
154
155
                    $code .= $this->drawLog_addRows(
0 ignored issues
show
Bug introduced by
The method drawLog_addRows() does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. ( Ignorable by Annotation )

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

155
                    $code .= $this->/** @scrutinizer ignore-call */ drawLog_addRows(

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...
156
                        $logEntriesOfPage,
157
                        $data['HTML'] . BackendUtility::getRecordTitle('pages', $data['row'], true)
158
                    );
159
                    if (++$count === 1000) {
160
                        break;
161
                    }
162
                }
163
                $this->view->assign('code', $code);
164
            }
165
166
            if ($this->CSVExport) {
167
                $this->outputCsvFile();
168
            }
169
        }
170
        $this->view->assign('showResultLog', (bool) $this->pObj->MOD_SETTINGS['log_resultLog']);
171
        $this->view->assign('showFeVars', (bool) $this->pObj->MOD_SETTINGS['log_feVars']);
172
        $this->view->assign('displayActions', 1);
173
        $this->view->assign('displayLogFilterHtml', $this->getDisplayLogFilterHtml($setId));
174
        $this->view->assign('itemPerPageHtml', $this->getItemsPerPageDropDownHtml());
175
        $this->view->assign('showResultLogHtml', $this->getShowResultLogCheckBoxHtml($setId, $quiPath));
176
        $this->view->assign('showFeVarsHtml', $this->getShowFeVarsCheckBoxHtml($setId, $quiPath));
177
        return $this->view->render();
178
    }
179
180
    /**
181
     * Outputs the CSV file and sets the correct headers
182
     */
183
    private function outputCsvFile(): void
184
    {
185
        if (! count($this->CSVaccu)) {
0 ignored issues
show
Bug Best Practice introduced by
The property CSVaccu does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
186
            MessageUtility::addWarningMessage($this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:message.canNotExportEmptyQueueToCsvText'));
187
            return;
188
        }
189
190
        $csvString = $this->csvWriter->arrayToCsv($this->CSVaccu);
0 ignored issues
show
Bug Best Practice introduced by
The property csvWriter does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
191
192
        header('Content-Type: application/octet-stream');
193
        header('Content-Disposition: attachment; filename=CrawlerLog.csv');
194
        echo $csvString;
195
196
        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...
197
    }
198
199
    /**
200
     * @return LanguageService
201
     */
202
    private function getLanguageService(): LanguageService
203
    {
204
        return $GLOBALS['LANG'];
205
    }
206
207
    private function getDisplayLogFilterHtml(int $setId): string
208
    {
209
        return $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.display') . ': ' . BackendUtility::getFuncMenu(
210
                $this->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
211
                'SET[log_display]',
212
                $this->pObj->MOD_SETTINGS['log_display'],
0 ignored issues
show
Bug Best Practice introduced by
The property pObj does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
213
                $this->pObj->MOD_MENU['log_display'],
214
                'index.php',
215
                '&setID=' . $setId
216
            );
217
    }
218
219
    private function getItemsPerPageDropDownHtml(): string
220
    {
221
        return $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.itemsPerPage') . ': ' .
222
            BackendUtility::getFuncMenu(
223
                $this->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
224
                'SET[itemsPerPage]',
225
                $this->pObj->MOD_SETTINGS['itemsPerPage'],
0 ignored issues
show
Bug Best Practice introduced by
The property pObj does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
226
                $this->pObj->MOD_MENU['itemsPerPage']
227
            );
228
    }
229
230
    private function getShowResultLogCheckBoxHtml(int $setId, string $quiPart): string
231
    {
232
        return BackendUtility::getFuncCheck(
233
                $this->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
234
                'SET[log_resultLog]',
235
                $this->pObj->MOD_SETTINGS['log_resultLog'],
0 ignored issues
show
Bug Best Practice introduced by
The property pObj does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
236
                'index.php',
237
                '&setID=' . $setId . $quiPart
238
            ) . '&nbsp;' . $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.showresultlog');
239
    }
240
241
    private function getShowFeVarsCheckBoxHtml(int $setId, string $quiPart): string
242
    {
243
        return BackendUtility::getFuncCheck(
244
                $this->id,
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
245
                'SET[log_feVars]',
246
                $this->pObj->MOD_SETTINGS['log_feVars'],
0 ignored issues
show
Bug Best Practice introduced by
The property pObj does not exist on AOE\Crawler\Backend\RequestForm\LogRequestForm. Did you maybe forget to declare it?
Loading history...
247
                'index.php',
248
                '&setID=' . $setId . $quiPart
249
            ) . '&nbsp;' . $this->getLanguageService()->sL('LLL:EXT:crawler/Resources/Private/Language/locallang.xlf:labels.showfevars');
250
    }
251
}
252