Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( 14c9f4...2d3a36 )
by Rafael
23:17
created

IndexAdministrationModuleController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 70.45%

Importance

Changes 0
Metric Value
wmc 11
eloc 43
c 0
b 0
f 0
dl 0
loc 99
ccs 31
cts 44
cp 0.7045
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setSolrConnectionManager() 0 3 1
A indexAction() 0 7 3
A reloadIndexConfigurationAction() 0 34 4
A emptyIndexAction() 0 21 3
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\ConnectionManager;
19
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
20
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
21
use Psr\Http\Message\ResponseInterface;
22
use Throwable;
23
use TYPO3\CMS\Core\Http\RedirectResponse;
24
use TYPO3\CMS\Core\Messaging\FlashMessage;
25
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
26
27
/**
28
 * Index Administration Module
29
 *
30
 * @author Ingo Renner <[email protected]>
31
 */
32
class IndexAdministrationModuleController extends AbstractModuleController
33
{
34
    /**
35
     * @var Queue
36
     */
37
    protected Queue $indexQueue;
38
39
    /**
40
     * @var ConnectionManager|null
41
     */
42
    protected ?ConnectionManager $solrConnectionManager = null;
43
44
    /**
45
     * @param ConnectionManager $solrConnectionManager
46
     */
47 3
    public function setSolrConnectionManager(ConnectionManager $solrConnectionManager)
48
    {
49 3
        $this->solrConnectionManager = $solrConnectionManager;
50 3
    }
51
52
    /**
53
     * Index action, shows an overview of available index maintenance operations.
54
     *
55
     * @return void
56
     */
57
    public function indexAction(): ResponseInterface
58
    {
59
        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

59
        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...
60
            $this->view->assign('can_not_proceed', true);
61
        }
62
        $this->moduleTemplate->setContent($this->view->render());
63
        return $this->htmlResponse($this->moduleTemplate->renderContent());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->htmlRespon...plate->renderContent()) returns the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type void.
Loading history...
64
    }
65
66
    /**
67
     * Empties the site's indexes.
68
     */
69 1
    public function emptyIndexAction(): ResponseInterface
70
    {
71 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

71
        /** @scrutinizer ignore-call */ 
72
        $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...
72
73
        try {
74 1
            $affectedCores = [];
75 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

75
            $solrServers = $this->solrConnectionManager->getConnectionsBySite(/** @scrutinizer ignore-type */ $this->selectedSite);
Loading history...
76 1
            foreach ($solrServers as $solrServer) {
77 1
                $writeService = $solrServer->getWriteService();
78
                /* @var $solrServer SolrConnection */
79 1
                $writeService->deleteByQuery('siteHash:' . $siteHash);
80 1
                $writeService->commit(false, false, false);
81 1
                $affectedCores[] = $writeService->getPrimaryEndpoint()->getCore();
82
            }
83 1
            $message = LocalizationUtility::translate('solr.backend.index_administration.index_emptied_all', 'Solr', [$this->selectedSite->getLabel(), implode(', ', $affectedCores)]);
84 1
            $this->addFlashMessage($message);
85
        } catch (Throwable $e) {
86
            $this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.error.on_empty_index', 'Solr', [$e->__toString()]), '', FlashMessage::ERROR);
87
        }
88
89 1
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
90
    }
91
92
    /**
93
     * Reloads the site's Solr cores.
94
     *
95
     * @return ResponseInterface
96
     */
97 2
    public function reloadIndexConfigurationAction(): ResponseInterface
98
    {
99 2
        $coresReloaded = true;
100 2
        $reloadedCores = [];
101 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

101
        $solrServers = $this->solrConnectionManager->getConnectionsBySite(/** @scrutinizer ignore-type */ $this->selectedSite);
Loading history...
102
103 2
        foreach ($solrServers as $solrServer) {
104 2
            $coreAdmin = $solrServer->getAdminService();
105 2
            $coreReloaded = $coreAdmin->reloadCore()->getHttpStatus() === 200;
106
107 2
            $coreName = $coreAdmin->getPrimaryEndpoint()->getCore();
108 2
            if (!$coreReloaded) {
109
                $coresReloaded = false;
110
111
                $this->addFlashMessage(
112
                    'Failed to reload index configuration for core "' . $coreName . '"',
113
                    '',
114
                    FlashMessage::ERROR
115
                );
116
                break;
117
            }
118
119 2
            $reloadedCores[] = $coreName;
120
        }
121
122 2
        if ($coresReloaded) {
123 2
            $this->addFlashMessage(
124 2
                'Core configuration reloaded (' . implode(', ', $reloadedCores) . ').',
125 2
                '',
126 2
                FlashMessage::OK
127
            );
128
        }
129
130 2
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
131
    }
132
}
133