Passed
Pull Request — release-11.5.x (#3165)
by Rafael
23:22
created

anager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
        return $this->getModuleTemplateResponse();
43
    }
44
45
    /**
46
     * Empties the site's indexes.
47
     */
48 1
    public function emptyIndexAction(): ResponseInterface
49
    {
50 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

50
        /** @scrutinizer ignore-call */ 
51
        $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...
51
52
        try {
53 1
            $affectedCores = [];
54 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

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

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