Passed
Pull Request — master (#1308)
by Timo
22:26
created

IndexAdministrationModuleController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 150
ccs 0
cts 86
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAction() 0 5 1
A indexAction() 0 9 2
A emptyIndexAction() 0 20 3
A clearIndexQueueAction() 0 9 1
B reloadIndexConfigurationAction() 0 35 4
A redirectToReferrerModule() 0 20 2
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 2 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\IndexQueue\Queue;
28
use ApacheSolrForTypo3\Solr\SolrService;
29
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
30
use TYPO3\CMS\Core\Messaging\FlashMessage;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Extbase\Mvc\Web\ReferringRequest;
33
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
34
35
/**
36
 * Index Administration Module
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class IndexAdministrationModuleController extends AbstractModuleController
41
{
42
43
    /**
44
     * @var Queue
45
     */
46
    protected $indexQueue;
47
48
    /**
49
     * @var \ApacheSolrForTypo3\Solr\ConnectionManager
50
     * @inject
51
     */
52
    protected $solrConnectionManager = null;
53
54
    /**
55
     * Initializes the controller before invoking an action method.
56
     */
57
    protected function initializeAction()
58
    {
59
        parent::initializeAction();
60
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
61
    }
62
63
    /**
64
     * Index action, shows an overview of available index maintenance operations.
65
     *
66
     * @return void
67
     */
68
    public function indexAction()
69
    {
70
        if ($this->selectedSite === null) {
71
            $this->view->assignMultiple([
72
                'can_not_proceed' => true,
73
                'pageUID' => $this->selectedPageUID
74
            ]);
75
        }
76
    }
77
78
    /**
79
     * Empties the site's indexes.
80
     *
81
     * @return void
82
     */
83
    public function emptyIndexAction()
84
    {
85
        $siteHash = $this->selectedSite->getSiteHash();
86
87
        try {
88
            $affectedCores = [];
89
            $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
90
            foreach ($solrServers as $solrServer) {
91
                /* @var $solrServer SolrService */
92
                $solrServer->deleteByQuery('siteHash:' . $siteHash);
93
                $solrServer->commit(false, false, false);
94
                $affectedCores[] = $solrServer->getCoreName();
95
            }
96
            $this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.index_emptied_all', 'Solr', [$this->selectedSite->getLabel(), implode(', ', $affectedCores)]));
97
        } catch (\Exception $e) {
98
            $this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.error.on_empty_index', 'Solr', [$e->__toString()]), '', FlashMessage::ERROR);
99
        }
100
101
        $this->redirect('index');
102
    }
103
104
    /**
105
     * Empties the Index Queue
106
     *
107
     * @return void
108
     */
109
    public function clearIndexQueueAction()
110
    {
111
        $this->indexQueue->deleteItemsBySite($this->selectedSite);
112
        $this->addFlashMessage(
113
            LocalizationUtility::translate('solr.backend.index_administration.success.queue_emptied', 'Solr',
114
                [$this->selectedSite->getLabel()])
115
        );
116
        $this->redirectToReferrerModule();
117
    }
118
119
    /**
120
     * Reloads the site's Solr cores.
121
     *
122
     * @return void
123
     */
124
    public function reloadIndexConfigurationAction()
125
    {
126
        $coresReloaded = true;
127
        $reloadedCores = [];
128
        $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
129
130
        foreach ($solrServers as $solrServer) {
131
            /* @var $solrServer SolrService */
132
            $coreReloaded = $solrServer->reloadCore()->getHttpStatus() === 200;
133
            $coreName = $solrServer->getCoreName();
134
135
            if (!$coreReloaded) {
136
                $coresReloaded = false;
137
138
                $this->addFlashMessage(
139
                    'Failed to reload index configuration for core "' . $coreName . '"',
140
                    '',
141
                    FlashMessage::ERROR
142
                );
143
                break;
144
            }
145
146
            $reloadedCores[] = $coreName;
147
        }
148
149
        if ($coresReloaded) {
150
            $this->addFlashMessage(
151
                'Core configuration reloaded ('.implode(', ', $reloadedCores).').',
152
                '',
153
                FlashMessage::OK
154
            );
155
        }
156
157
        $this->redirect('index');
158
    }
159
160
    /**
161
     * Redirects to the referrer module index Action.
162
     *
163
     * Fluids <f:form VH can not make urls to other modules properly.
164
     * The module name/key is not provided in the hidden fields __referrer by bulding form.
165
     * So this is currently the single way to make it possible.
166
     *
167
     * @todo: remove this method if f:form works properly between backend modules.
168
     */
169
    protected function redirectToReferrerModule()
170
    {
171
        /* @var ReferringRequest $referringRequest */
172
        $referringRequest = $this->request->getReferringRequest();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class TYPO3\CMS\Extbase\Mvc\Request as the method getReferringRequest() does only exist in the following sub-classes of TYPO3\CMS\Extbase\Mvc\Request: ApacheSolrForTypo3\Solr\Widget\WidgetRequest, TYPO3\CMS\Extbase\Mvc\Web\ReferringRequest, TYPO3\CMS\Extbase\Mvc\Web\Request, TYPO3\CMS\Fluid\Core\Widget\WidgetRequest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
173
        $controllerName = $this->request->getControllerName();
174
        $referrerControllerName = $referringRequest->getControllerName();
175
        if ($controllerName === $referrerControllerName) {
176
            $this->redirect('index');
177
            return;
178
        }
179
        /* @var BackendUriBuilder $backendUriBuilder */
180
        $backendUriBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class);
181
        $referrerUriFromBackendUriBuilder = $backendUriBuilder->buildUriFromModule(
182
            'searchbackend_SolrIndexqueue',
183
            [
184
                'id' => $this->selectedPageUID
185
            ]
186
        );
187
        $this->redirectToUri($referrerUriFromBackendUriBuilder);
188
    }
189
}
190