Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( 38a128...0b41c4 )
by Rafael
25:41 queued 19:10
created

emptyIndexAction()   A

Complexity

Conditions 3
Paths 12

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.0261

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 22
ccs 12
cts 14
cp 0.8571
rs 9.7998
cc 3
nc 12
nop 0
crap 3.0261
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2013-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\ConnectionManager;
28
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
29
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
30
use Psr\Http\Message\ResponseInterface;
31
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
32
use TYPO3\CMS\Core\Messaging\FlashMessage;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
35
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
36
37
/**
38
 * Index Administration Module
39
 *
40
 * @author Ingo Renner <[email protected]>
41
 */
42
class IndexAdministrationModuleController extends AbstractModuleController
0 ignored issues
show
Bug introduced by
The type ApacheSolrForTypo3\Solr\...bstractModuleController 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...
43
{
44
45
    /**
46
     * @var Queue
47
     */
48
    protected Queue $indexQueue;
49
50
    /**
51
     * @var ConnectionManager
52
     */
53
    protected ?ConnectionManager $solrConnectionManager = null;
54
55
    /**
56
     * @param ConnectionManager $solrConnectionManager
57
     */
58 3
    public function setSolrConnectionManager(ConnectionManager $solrConnectionManager)
59
    {
60 3
        $this->solrConnectionManager = $solrConnectionManager;
61 3
    }
62
63
    /**
64
     * Index action, shows an overview of available index maintenance operations.
65
     *
66
     * @return void
67
     */
68
    public function indexAction(): ResponseInterface
69
    {
70
        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

70
        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...
71
            $this->view->assign('can_not_proceed', true);
72
        }
73
        return $this->htmlResponse();
74
    }
75
76
    /**
77
     * Empties the site's indexes.
78
     *
79
     * @return void
80
     * @throws StopActionException
81
     */
82 1
    public function emptyIndexAction()
83
    {
84 1
        $siteHash = $this->selectedSite->getSiteHash();
85
86
        try {
87 1
            $affectedCores = [];
88 1
            $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
89 1
            foreach ($solrServers as $solrServer) {
90 1
                $writeService = $solrServer->getWriteService();
91
                /* @var $solrServer SolrConnection */
92 1
                $writeService->deleteByQuery('siteHash:' . $siteHash);
93 1
                $writeService->commit(false, false, false);
94 1
                $affectedCores[] = $writeService->getPrimaryEndpoint()->getCore();
95
            }
96 1
            $message = LocalizationUtility::translate('solr.backend.index_administration.index_emptied_all', 'Solr', [$this->selectedSite->getLabel(), implode(', ', $affectedCores)]);
97 1
            $this->addFlashMessage($message);
98
        } catch (\Exception $e) {
99
            $this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.error.on_empty_index', 'Solr', [$e->__toString()]), '', FlashMessage::ERROR);
100
        }
101
102
        /** @noinspection PhpUnhandledExceptionInspection */
103 1
        $this->redirect('index');
104 1
    }
105
106
    /**
107
     * Reloads the site's Solr cores.
108
     *
109
     * @return void
110
     * @throws StopActionException
111
     */
112 2
    public function reloadIndexConfigurationAction()
113
    {
114 2
        $coresReloaded = true;
115 2
        $reloadedCores = [];
116 2
        $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
117
118 2
        foreach ($solrServers as $solrServer) {
119 2
            $coreAdmin = $solrServer->getAdminService();
120 2
            $coreReloaded = $coreAdmin->reloadCore()->getHttpStatus() === 200;
121
122 2
            $coreName = $coreAdmin->getPrimaryEndpoint()->getCore();
123 2
            if (!$coreReloaded) {
124
                $coresReloaded = false;
125
126
                $this->addFlashMessage(
127
                    'Failed to reload index configuration for core "' . $coreName . '"',
128
                    '',
129
                    FlashMessage::ERROR
130
                );
131
                break;
132
            }
133
134 2
            $reloadedCores[] = $coreName;
135
        }
136
137 2
        if ($coresReloaded) {
138 2
            $this->addFlashMessage(
139 2
                'Core configuration reloaded (' . implode(', ', $reloadedCores) . ').',
140 2
                '',
141 2
                FlashMessage::OK
142
            );
143
        }
144
145
        /** @noinspection PhpUnhandledExceptionInspection */
146 2
        $this->redirect('index');
147 2
    }
148
}
149