Passed
Push — master ( ae3f68...4bca36 )
by Timo
24:00
created

Node::getSolariumClientOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2018 Timo Hund <[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 3 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
/**
28
 * Represent a server node of solr, in the most setups you would only have one, but sometimes
29
 * mulitple for reading and writing.
30
 *
31
 * @package ApacheSolrForTypo3\Solr\System\Solr
32
 */
33
class Node {
34
35
    /**
36
     * @var string
37
     */
38
    protected $scheme;
39
40
    /**
41
     * @var string
42
     */
43
    protected $host;
44
45
    /**
46
     * @var int
47
     */
48
    protected $port;
49
50
    /**
51
     * @var string
52
     */
53
    protected $path;
54
55
    /**
56
     * @var string
57
     */
58
    protected $username;
59
60
    /**
61
     * @var string
62
     */
63
    protected $password;
64
65
    /**
66
     * @var int
67
     */
68
    protected $timeout;
69
70
    /**
71
     * Node constructor.
72
     * @param string $scheme
73
     * @param string $host
74
     * @param int $port
75
     * @param string $path
76
     * @param string $username
77
     * @param string $password
78
     * @param int $timeout
79
     */
80 118
    public function __construct(string $scheme = 'http', string $host = 'localhost', int $port = 8983, string $path = '/solr/core_en/', string $username, string $password, int $timeout = 0)
81
    {
82 118
        $this->scheme = $scheme;
83 118
        $this->host = $host;
84 118
        $this->port = $port;
85 118
        $this->path = $path;
86 118
        $this->username = $username;
87 118
        $this->password = $password;
88 118
        $this->timeout = $timeout;
89 118
    }
90
91
    /**
92
     * @return string
93
     */
94 108
    public function getScheme(): string
95
    {
96 108
        return $this->scheme;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 108
    public function getHost(): string
103
    {
104 108
        return $this->host;
105
    }
106
107
    /**
108
     * @return int
109
     */
110 108
    public function getPort(): int
111
    {
112 108
        return $this->port;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getPath(): string
119
    {
120
        return $this->path;
121
    }
122
123
    /**
124
     * @return string
125
     */
126 109
    public function getUsername(): string
127
    {
128 109
        return $this->username;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 1
    public function getPassword(): string
135
    {
136 1
        return $this->password;
137
    }
138
139
    /**
140
     * @return int
141
     */
142 107
    public function getTimeout(): int
143
    {
144 107
        return $this->timeout;
145
    }
146
147
    /**
148
     * @param $configuration
149
     * @return Node
150
     */
151 119
    public static function fromArray($configuration)
152
    {
153 119
        static::checkIfRequiredKeyIsSet($configuration, 'scheme');
154 118
        static::checkIfRequiredKeyIsSet($configuration, 'host');
155 118
        static::checkIfRequiredKeyIsSet($configuration, 'port');
156 118
        static::checkIfRequiredKeyIsSet($configuration, 'path');
157
158 118
        $scheme = $configuration['scheme'];
159 118
        $host = $configuration['host'];
160 118
        $port = $configuration['port'];
161 118
        $path = $configuration['path'];
162
163 118
        $username = $configuration['username'] ?? '';
164 118
        $password = $configuration['password'] ?? '';
165 118
        $timeout = $configuration['timeout'] ?? 0;
166 118
        return new Node($scheme, $host, $port, $path, $username, $password, $timeout);
167
    }
168
169
    /**
170
     * Checks if the required configuration option is set.
171
     *
172
     * @param array  $configuration
173
     * @param string $name
174
     * @throws |UnexpectedValueException
175
     */
176 119
    protected static function checkIfRequiredKeyIsSet(array $configuration, $name)
177
    {
178 119
        if (empty($configuration[$name])) {
179 1
            throw new \UnexpectedValueException('Required solr connection property ' . $name. ' is missing.');
180
        }
181 118
    }
182
183
    /**
184
     * Returns the core name from the configured path without the core name.
185
     *
186
     * @return string
187
     */
188 108
    public function getCoreBasePath()
189
    {
190 108
        $pathWithoutLeadingAndTrailingSlashes = trim(trim($this->path), "/");
191 108
        $pathWithoutLastSegment = substr($pathWithoutLeadingAndTrailingSlashes, 0, strrpos($pathWithoutLeadingAndTrailingSlashes, "/"));
192 108
        return '/' . $pathWithoutLastSegment . '/';
193
    }
194
195
    /**
196
     * Returns the core name from the configured path.
197
     *
198
     * @return string
199
     */
200 108
    public function getCoreName()
201
    {
202 108
        $paths = explode('/', trim($this->path, '/'));
203 108
        return (string)array_pop($paths);
204
    }
205
206
    /**
207
     * @return array
208
     */
209 107
    public function getSolariumClientOptions()
210
    {
211
        return [
212 107
            'host' => $this->getHost(),
213 107
            'port' => $this->getPort(),
214 107
            'scheme' => $this->getScheme(),
215 107
            'path' => $this->getCoreBasePath(),
216 107
            'core' => $this->getCoreName(),
217 107
            'timeout' => $this->getTimeout()
218
        ];
219
    }
220
221
    /**
222
     * @return string
223
     */
224 11
    public function __toString()
225
    {
226 11
        return $this->getScheme() . '://' . $this->getHost() . ':' . $this->getPort() . $this->getCoreBasePath() . $this->getCoreName() . '/';
227
    }
228
}