Passed
Push — task/2976_TYPO3.11_compatibili... ( 9f9205...0f123d )
by Rafael
27:04
created

IndexAdministrationModuleController::indexAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
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\System\Solr\SolrConnection;
19
use Psr\Http\Message\ResponseInterface;
20
use Throwable;
21
use TYPO3\CMS\Core\Http\RedirectResponse;
22
use TYPO3\CMS\Core\Messaging\FlashMessage;
23
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
24
25
/**
26
 * Index Administration Module
27
 *
28
 * @author Ingo Renner <[email protected]>
29
 */
30
class IndexAdministrationModuleController extends AbstractModuleController
31
{
32
    /**
33
     * Index action, shows an overview of available index maintenance operations.
34
     *
35
     * @return ResponseInterface
36
     */
37
    public function indexAction(): ResponseInterface
38
    {
39
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
40
            $this->view->assign('can_not_proceed', true);
41
        }
42
        $this->moduleTemplate->setContent($this->view->render());
43
        return $this->htmlResponse($this->moduleTemplate->renderContent());
44
    }
45
46
    /**
47
     * Empties the site's indexes.
48
     */
49 1
    public function emptyIndexAction(): ResponseInterface
50
    {
51 1
        $siteHash = $this->selectedSite->getSiteHash();
0 ignored issues
show
Bug introduced by
The method getSiteHash() 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

51
        /** @scrutinizer ignore-call */ 
52
        $siteHash = $this->selectedSite->getSiteHash();

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...
52
53
        try {
54 1
            $affectedCores = [];
55 1
            $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
0 ignored issues
show
Bug introduced by
It seems like $this->selectedSite can also be of type null; however, parameter $site of ApacheSolrForTypo3\Solr\...:getConnectionsBySite() 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

55
            $solrServers = $this->solrConnectionManager->getConnectionsBySite(/** @scrutinizer ignore-type */ $this->selectedSite);
Loading history...
56 1
            foreach ($solrServers as $solrServer) {
57 1
                $writeService = $solrServer->getWriteService();
58
                /* @var $solrServer SolrConnection */
59 1
                $writeService->deleteByQuery('siteHash:' . $siteHash);
60 1
                $writeService->commit(false, false, false);
61 1
                $affectedCores[] = $writeService->getPrimaryEndpoint()->getCore();
62
            }
63 1
            $message = LocalizationUtility::translate('solr.backend.index_administration.index_emptied_all', 'Solr', [$this->selectedSite->getLabel(), implode(', ', $affectedCores)]);
64 1
            $this->addFlashMessage($message);
65
        } catch (Throwable $e) {
66
            $this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.error.on_empty_index', 'Solr', [$e->__toString()]), '', FlashMessage::ERROR);
67
        }
68
69 1
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
70
    }
71
72
    /**
73
     * Reloads the site's Solr cores.
74
     *
75
     * @return ResponseInterface
76
     */
77 2
    public function reloadIndexConfigurationAction(): ResponseInterface
78
    {
79 2
        $coresReloaded = true;
80 2
        $reloadedCores = [];
81 2
        $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
0 ignored issues
show
Bug introduced by
It seems like $this->selectedSite can also be of type null; however, parameter $site of ApacheSolrForTypo3\Solr\...:getConnectionsBySite() 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

81
        $solrServers = $this->solrConnectionManager->getConnectionsBySite(/** @scrutinizer ignore-type */ $this->selectedSite);
Loading history...
82
83 2
        foreach ($solrServers as $solrServer) {
84 2
            $coreAdmin = $solrServer->getAdminService();
85 2
            $coreReloaded = $coreAdmin->reloadCore()->getHttpStatus() === 200;
86
87 2
            $coreName = $coreAdmin->getPrimaryEndpoint()->getCore();
88 2
            if (!$coreReloaded) {
89
                $coresReloaded = false;
90
91
                $this->addFlashMessage(
92
                    'Failed to reload index configuration for core "' . $coreName . '"',
93
                    '',
94
                    FlashMessage::ERROR
95
                );
96
                break;
97
            }
98
99 2
            $reloadedCores[] = $coreName;
100
        }
101
102 2
        if ($coresReloaded) {
103 2
            $this->addFlashMessage(
104 2
                'Core configuration reloaded (' . implode(', ', $reloadedCores) . ').',
105 2
                '',
106 2
                FlashMessage::OK
107
            );
108
        }
109
110 2
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
111
    }
112
}
113