Passed
Push — master ( 0f0c33...04715c )
by Timo
49:20 queued 18:20
created

OverviewModuleController::initializeAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 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\Api;
28
use ApacheSolrForTypo3\Solr\ConnectionManager;
29
use ApacheSolrForTypo3\Solr\System\Validator\Path;
30
use TYPO3\CMS\Core\Messaging\FlashMessage;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Overview Module
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class OverviewModuleController extends AbstractModuleController
39
{
40
41
    /**
42
     * Module name, used to identify a module f.e. in URL parameters.
43
     *
44
     * @var string
45
     */
46
    protected $moduleName = 'Overview';
47
48
    /**
49
     * Module title, shows up in the module menu.
50
     *
51
     * @var string
52
     */
53
    protected $moduleTitle = 'Overview';
54
55
    protected $connections = [];
56
57
    /**
58
     * Index action, shows an overview of the state of the Solr index
59
     *
60
     * @return void
61
     */
62
    public function indexAction()
63
    {
64
        $this->checkConnections();
65
66
        $this->view->assign('site', $this->request->getArgument('site'));
67
        $this->view->assign('apiKey', Api::getApiKey());
68
    }
69
70
    /**
71
     * Checks whether the configured Solr server can be reached and provides a
72
     * flash message according to the result of the check.
73
     *
74
     * @return void
75
     */
76
    protected function checkConnections()
77
    {
78
        $connectedHosts = [];
79
        $missingHosts = [];
80
        $invalidPaths = [];
81
82
        $path = GeneralUtility::makeInstance(Path::class);
83
84
        foreach ($this->connections as $connection) {
85
            $coreUrl = $connection->getScheme() . '://' . $connection->getHost() . ':' . $connection->getPort() . $connection->getPath();
86
87
            if ($connection->ping()) {
88
                $connectedHosts[] = $coreUrl;
89
            } else {
90
                $missingHosts[] = $coreUrl;
91
            }
92
93
            // Check that path is valid
94
            if (!$path->isValidSolrPath($connection->getPath())) {
95
                $invalidPaths[] = $connection->getPath();
96
            }
97
        }
98
99
        if (!empty($connectedHosts)) {
100
            $this->addFlashMessage(
101
                'Hosts contacted:' . PHP_EOL . implode(PHP_EOL, $connectedHosts),
102
                'Your Apache Solr server has been contacted.',
103
                FlashMessage::OK
104
            );
105
        }
106
107
        if (!empty($missingHosts)) {
108
            $this->addFlashMessage(
109
                'Hosts missing:' . PHP_EOL . implode(PHP_EOL, $missingHosts),
110
                'Unable to contact your Apache Solr server.',
111
                FlashMessage::ERROR
112
            );
113
        }
114
115
        if (!empty($invalidPaths)) {
116
            $this->addFlashMessage(
117
                'Path should not contain the characters "*, ?, <, >, |, :, or #":' . PHP_EOL . implode(PHP_EOL,
118
                    $invalidPaths),
119
                'Path is not valid',
120
                FlashMessage::WARNING
121
            );
122
        }
123
    }
124
125
    /**
126
     * Initializes resources commonly needed for several actions
127
     *
128
     * @return void
129
     */
130
    protected function initializeAction()
131
    {
132
        parent::initializeAction();
133
134
        $connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
135
        $this->connections = $connectionManager->getConnectionsBySite($this->site);
136
    }
137
}
138