Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( ba9fea...772e40 )
by Rafael
42:34
created

IndexQueueModuleController::canQueueSelectedSite()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 20
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
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
use ApacheSolrForTypo3\Solr\Backend\IndexingConfigurationSelectorField;
19
use ApacheSolrForTypo3\Solr\Domain\Index\IndexService;
20
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
21
use Psr\Http\Message\ResponseInterface;
22
use TYPO3\CMS\Backend\Form\Exception as BackendFormException;
23
use TYPO3\CMS\Core\Http\RedirectResponse;
24
use TYPO3\CMS\Core\Messaging\AbstractMessage;
25
use TYPO3\CMS\Core\Messaging\FlashMessage;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28
29
/**
30
 * Index Queue Module
31
 *
32
 * @author Ingo Renner <[email protected]>
33
 */
34
class IndexQueueModuleController extends AbstractModuleController
35
{
36
37
    /**
38
     * @var Queue
39
     */
40
    protected Queue $indexQueue;
41
42
    /**
43
     * Initializes the controller before invoking an action method.
44
     */
45
    protected function initializeAction()
46
    {
47
        parent::initializeAction();
48
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
49
    }
50
51
    /**
52
     * @param Queue $indexQueue
53
     */
54
    public function setIndexQueue(Queue $indexQueue)
55
    {
56
        $this->indexQueue = $indexQueue;
57
    }
58
59
    /**
60
     * Lists the available indexing configurations
61
     *
62
     * @return ResponseInterface
63
     * @throws BackendFormException
64
     */
65
    public function indexAction(): ResponseInterface
66
    {
67
        if (!$this->canQueueSelectedSite()) {
68
            $this->view->assign('can_not_proceed', true);
69
            $this->moduleTemplate->setContent($this->view->render());
70
            return $this->htmlResponse($this->moduleTemplate->renderContent());
71
        }
72
73
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
74
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
75
        $this->view->assign('indexqueue_statistics', $statistics);
76
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
77
        $this->moduleTemplate->setContent($this->view->render());
78
        return $this->htmlResponse($this->moduleTemplate->renderContent());
79 2
    }
80
81 2
    /**
82 2
     * Checks if selected site can be queued.
83
     *
84
     * @return bool
85
     */
86
    protected function canQueueSelectedSite(): bool
87
    {
88
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
0 ignored issues
show
Bug introduced by
The method getConnectionsBySite() 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

88
        if ($this->selectedSite === null || empty($this->solrConnectionManager->/** @scrutinizer ignore-call */ getConnectionsBySite($this->selectedSite))) {

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...
89
            return false;
90
        }
91
        $enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
92
        if (empty($enabledIndexQueueConfigurationNames)) {
93
            return false;
94
        }
95
        return true;
96
    }
97
98
    /**
99
     * Renders a field to select which indexing configurations to initialize.
100
     *
101
     * Uses TCEforms.
102
     *
103
     * @return string Markup for the select field
104
     * @throws BackendFormException
105
     */
106
    protected function getIndexQueueInitializationSelector(): string
107
    {
108
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, /** @scrutinizer ignore-type */ $this->selectedSite);
109
        $selector->setFormElementName('tx_solr-index-queue-initialization');
110
111
        return $selector->render();
112
    }
113
114
    /**
115
     * Initializes the Index Queue for selected indexing configurations
116
     *
117
     * @return ResponseInterface
118
     */
119
    public function initializeIndexQueueAction(): ResponseInterface
120
    {
121
        $initializedIndexingConfigurations = [];
122
123
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
124
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
0 ignored issues
show
introduced by
The condition is_array($indexingConfigurationsToInitialize) is always false.
Loading history...
125
            // initialize selected indexing configuration
126
            $initializedIndexingConfigurations = $this->indexQueue->getInitializationService()->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
127
        } else {
128
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
129
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
130
            $this->addFlashMessage(
131
                LocalizationUtility::translate($messageLabel, 'Solr'),
132
                LocalizationUtility::translate($titleLabel, 'Solr'),
133
                FlashMessage::WARNING
134
            );
135
        }
136
        $messagesForConfigurations = [];
137
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
138
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
0 ignored issues
show
Bug introduced by
It seems like $this->selectedSite can also be of type null; however, parameter $site of ApacheSolrForTypo3\Solr\...::getStatisticsBySite() does only seem to accept ApacheSolrForTypo3\Solr\Domain\Site\Site, 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

138
            $itemCount = $this->indexQueue->getStatisticsBySite(/** @scrutinizer ignore-type */ $this->selectedSite, $indexingConfigurationName)->getTotalCount();
Loading history...
139
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
140
        }
141
142
        if (!empty($initializedIndexingConfigurations)) {
143
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
144
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
145
            $this->addFlashMessage(
146
                LocalizationUtility::translate($messageLabel, 'Solr', [implode(', ', $messagesForConfigurations)]),
147
                LocalizationUtility::translate($titleLabel, 'Solr'),
148
                FlashMessage::OK
149
            );
150
        }
151
152
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
153
    }
154
155
    /**
156
     * Removes all errors in the index queue list. So that the items can be indexed again.
157
     *
158
     * @return ResponseInterface
159
     */
160
    public function resetLogErrorsAction(): ResponseInterface
161
    {
162
        $resetResult = $this->indexQueue->resetAllErrors();
163
164
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
165
        $severity = FlashMessage::OK;
166
        if (!$resetResult) {
167
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
168
            $severity = FlashMessage::ERROR;
169
        }
170
171
        $this->addIndexQueueFlashMessage($label, $severity);
172
173
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
174
    }
175
176
    /**
177
     * ReQueues a single item in the indexQueue.
178
     *
179
     * @param string $type
180
     * @param int $uid
181
     * @return ResponseInterface
182
     */
183
    public function requeueDocumentAction(string $type, int $uid): ResponseInterface
184
    {
185
        $label = 'solr.backend.index_queue_module.flashmessage.error.single_item_not_requeued';
186
        $severity = AbstractMessage::ERROR;
187
188
        $updateCount = $this->indexQueue->updateItem($type, $uid, time());
189
        if ($updateCount > 0) {
190
            $label = 'solr.backend.index_queue_module.flashmessage.success.single_item_was_requeued';
191
            $severity = AbstractMessage::OK;
192
        }
193
194
        $this->addIndexQueueFlashMessage($label, $severity);
195
196
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
197
    }
198
199
    /**
200
     * Shows the error message for one queue item.
201
     *
202
     * @param int $indexQueueItemId
203
     * @return ResponseInterface
204
     */
205
    public function showErrorAction(int $indexQueueItemId): ResponseInterface
206
    {
207
        $item = $this->indexQueue->getItem($indexQueueItemId);
208
        if ($item === null) {
209
            // add a flash message and quit
210
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
211
            $severity = FlashMessage::ERROR;
212
            $this->addIndexQueueFlashMessage($label, $severity);
213
214
            return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
215
        }
216
217 2
        $this->view->assign('indexQueueItem', $item);
218
        $this->moduleTemplate->setContent($this->view->render());
219 2
        return $this->htmlResponse($this->moduleTemplate->renderContent());
220 2
    }
221
222 2
    /**
223 2
     * Indexes a few documents with the index service.
224 1
     * @return ResponseInterface
225 1
     */
226
    public function doIndexingRunAction(): ResponseInterface
227
    {
228 2
        /* @var IndexService $indexService */
229
        $indexService = GeneralUtility::makeInstance(IndexService::class, /** @scrutinizer ignore-type */ $this->selectedSite);
230 2
        $indexWithoutErrors = $indexService->indexItems(1);
231 2
232
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
233
        $severity = FlashMessage::OK;
234
        if (!$indexWithoutErrors) {
235
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
236
            $severity = FlashMessage::ERROR;
237
        }
238
239
        $this->addFlashMessage(
240
            LocalizationUtility::translate($label, 'Solr'),
241
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
242
            $severity
243
        );
244
245
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
246
    }
247
248
    /**
249
     * Adds a flash message for the index queue module.
250
     *
251
     * @param string $label
252
     * @param int $severity
253
     */
254
    protected function addIndexQueueFlashMessage(string $label, int $severity)
255
    {
256
        $this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity);
257
    }
258
}
259