Completed
Pull Request — master (#1105)
by
unknown
24:00 queued 18:51
created

AbstractModuleController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 3
cbo 5
dl 0
loc 245
ccs 0
cts 94
cp 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getTitle() 0 4 1
A setExtensionKey() 0 4 1
A getExtensionKey() 0 4 1
A injectStringHelper() 0 4 1
B initializeAction() 0 25 4
A getFallbackSite() 0 7 1
A initializeView() 0 4 1
A getRequestArgumentOrDefaultValue() 0 14 3
A forwardToIndex() 0 18 3
B getSelectedCoreSolrConnection() 0 22 4
A addFlashMessagesByQueueIdentifier() 0 7 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Backend\SolrModule;
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\Site;
28
use ApacheSolrForTypo3\Solr\Utility\StringUtility;
29
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
30
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
31
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
32
33
/**
34
 * Abstract Module
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
abstract class AbstractModuleController extends ActionController implements AdministrationModuleInterface
39
{
40
41
    /**
42
     * Module name, used to identify a module f.e. in URL parameters.
43
     *
44
     * @var string
45
     */
46
    protected $moduleName = '';
47
48
    /**
49
     * Module title, shows up in the module menu.
50
     *
51
     * @var string
52
     */
53
    protected $moduleTitle = '';
54
55
    /**
56
     * @var \ApacheSolrForTypo3\Solr\Service\ModuleDataStorageService
57
     * @inject
58
     */
59
    protected $moduleDataStorageService;
60
61
    /**
62
     * Extension key
63
     *
64
     * @var string
65
     */
66
    protected $extensionKey = '';
67
68
    /**
69
     * @var \ApacheSolrForTypo3\Solr\ConnectionManager
70
     * @inject
71
     */
72
    protected $connectionManager = null;
73
74
    /**
75
     * The currently selected Site.
76
     *
77
     * @var Site
78
     */
79
    protected $site;
80
81
    /**
82
     * @var \ApacheSolrForTypo3\Solr\Utility\StringUtility
83
     */
84
    protected $stringUtility;
85
86
    /**
87
     * Gets the module name.
88
     *
89
     * @return string Module name
90
     */
91
    public function getName()
92
    {
93
        return $this->moduleName;
94
    }
95
96
    /**
97
     * Gets the module title.
98
     *
99
     * @return string Module title
100
     */
101
    public function getTitle()
102
    {
103
        return $this->moduleTitle;
104
    }
105
106
    /**
107
     * Sets the extension key
108
     *
109
     * @param string $extensionKey Extension key
110
     */
111
    public function setExtensionKey($extensionKey)
112
    {
113
        $this->extensionKey = $extensionKey;
114
    }
115
116
    /**
117
     * Gets the extension key
118
     *
119
     * @return string Extension key
120
     */
121
    public function getExtensionKey()
122
    {
123
        return $this->extensionKey;
124
    }
125
126
    /**
127
     * Method to pass a StringUtil object.
128
     * Use to overwrite injected object in unit test context.
129
     *
130
     * @param \ApacheSolrForTypo3\Solr\Utility\StringUtility $stringUtility
131
     */
132
    public function injectStringHelper(StringUtility $stringUtility)
133
    {
134
        $this->stringUtility = $stringUtility;
135
    }
136
137
    /**
138
     * Initializes resources commonly needed for several actions
139
     *
140
     * @return void
141
     */
142
    protected function initializeAction()
143
    {
144
        try {
145
            $site = $this->request->getArgument('site');
146
147
            if (is_numeric($site)) {
148
                $siteRootPageId = $this->request->getArgument('site');
149
                $this->site = Site::getSiteByPageId($siteRootPageId);
150
            } else {
151
                if ($site instanceof Site) {
152
                    $this->site = $site;
153
                } else {
154
                    $this->site = $this->getFallbackSite();
155
                }
156
            }
157
        } catch (NoSuchArgumentException $nsae) {
158
            $this->site = $this->getFallbackSite();
159
        }
160
161
        $this->request->setArgument('site', $this->site);
162
163
        $moduleData = $this->moduleDataStorageService->loadModuleData();
164
        $moduleData->setSite($this->site);
0 ignored issues
show
Bug introduced by
It seems like $this->site can be null; however, setSite() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
165
        $this->moduleDataStorageService->persistModuleData($moduleData);
166
    }
167
168
    /**
169
     * Return Fallback site
170
     *
171
     * @return Site
172
     */
173
    protected function getFallbackSite()
174
    {
175
        $sites = Site::getAvailableSites();
176
177
        $site = array_shift($sites);
178
        return $site;
179
    }
180
181
    /**
182
     * Initializes the view before invoking an action method.
183
     *
184
     * Assigns the current module to the view
185
     *
186
     * @param \TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view The view to be initialized
187
     * @return void
188
     */
189
    protected function initializeView(ViewInterface $view)
190
    {
191
        $view->assign('module', $this);
192
    }
193
194
    /**
195
     * Checks if the request argument is present an returns it. If not it returns the default value.
196
     *
197
     * @param string $argumentKey
198
     * @param mixed $default
199
     * @return mixed
200
     */
201
    protected function getRequestArgumentOrDefaultValue($argumentKey, $default)
202
    {
203
        // no request -> return default
204
        if (!isset($this->request)) {
205
            return $default;
206
        }
207
208
        // argument not present -> return default value
209
        if (!$this->request->hasArgument($argumentKey)) {
210
            return $default;
211
        }
212
213
        return $this->request->getArgument($argumentKey);
214
    }
215
216
    /**
217
     * Forwards to the index action after resetting module and moduleAction
218
     * arguments to prevent execution of module actions.
219
     *
220
     * @return void
221
     */
222
    protected function forwardToIndex()
223
    {
224
        $requestArguments = $this->request->getArguments();
225
226
        foreach ($requestArguments as $argumentName => $_) {
227
            if (!in_array($argumentName,
228
                ['module', 'controller', 'site'])
229
            ) {
230
                unset($requestArguments[$argumentName]);
231
                unset($_GET['tx_solr_tools_solradministration'][$argumentName]);
232
                unset($this->arguments[$argumentName]);
233
            }
234
        }
235
236
        $this->request->setArguments($requestArguments);
237
238
        $this->forward('index');
239
    }
240
241
    /**
242
     * Finds the Solr connection to use for the currently selected core.
243
     *
244
     * @return \ApacheSolrForTypo3\Solr\SolrService Solr connection
245
     */
246
    protected function getSelectedCoreSolrConnection()
247
    {
248
        $currentCoreConnection = null;
249
250
        $solrConnections = $this->connectionManager->getConnectionsBySite($this->site);
251
        $currentCore = $this->moduleDataStorageService->loadModuleData()->getCore();
252
253
        foreach ($solrConnections as $solrConnection) {
254
            if ($solrConnection->getPath() == $currentCore) {
255
                $currentCoreConnection = $solrConnection;
256
                break;
257
            }
258
        }
259
260
        if (is_null($currentCoreConnection)) {
261
            // when switching sites $currentCore is empty and nothing matched
262
            // fall back to the connection's first core
263
            $currentCoreConnection = $solrConnections[0];
264
        }
265
266
        return $currentCoreConnection;
267
    }
268
269
    /**
270
     * Adds flash massages from another flash message queue, e.g. solr.queue.initializer
271
     *
272
     * @param string $identifier
273
     * @return void
274
     */
275
    protected function addFlashMessagesByQueueIdentifier($identifier)
276
    {
277
        $flashMessages = $this->controllerContext->getFlashMessageQueue($identifier)->getAllMessages();
278
        foreach ($flashMessages as $message) {
279
            $this->addFlashMessage($message->getMessage(), $message->getTitle(), $message->getSeverity());
280
        }
281
    }
282
}
283