Passed
Push — master ( fb70ba...785b5c )
by
unknown
34:59
created

Node::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 29
ccs 13
cts 13
cp 1
rs 9.7666
cc 1
nc 1
nop 6
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Solr;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use Solarium\Core\Client\Endpoint;
18
19
/**
20
 * Represent a server node of solr, in the most setups you would only have one, but sometimes
21
 * multiple for reading and writing.
22
 *
23
 * @author Timo Hund <[email protected]>
24
 * @copyright Copyright (c) 2009-2020 Timo Hund <[email protected]>
25
 *
26
 * @deprecated Class will removed with Ext:solr 12.x. Use class \Solarium\Core\Client\Endpoint instead.
27
 */
28
class Node extends Endpoint
29
{
30
    /**
31
     * Node constructor.
32
     * @param string $scheme
33
     * @param string $host
34
     * @param int $port
35
     * @param string $path
36
     * @param ?string $username
37
     * @param ?string $password
38
     */
39 76
    public function __construct(
40
        string $scheme = 'http',
41
        string $host = 'localhost',
42
        int $port = 8983,
43
        string $path = '/solr/core_en/',
44
        ?string $username = null,
45
        ?string $password = null
46
    ) {
47 76
        $path = (string)$path;
48 76
        $elements = explode('/', trim($path, '/'));
49 76
        $coreName = (string)array_pop($elements);
50
        // Remove API version
51 76
        array_pop($elements);
52
53
        // The path should always have the same format!
54 76
        $path = trim(implode('/', $elements), '/');
55
56
        $options = [
57 76
            'scheme' => $scheme,
58 76
            'host' => $host,
59 76
            'port' => $port,
60 76
            'path' => '/' . $path,
61
            'collection' => null,
62 76
            'core' => $coreName,
63
            'leader' => false,
64
        ];
65
66 76
        parent::__construct($options);
67 76
        $this->setAuthentication($username, $password);
0 ignored issues
show
Bug introduced by
It seems like $username can also be of type null; however, parameter $username of Solarium\Core\Client\Endpoint::setAuthentication() does only seem to accept string, 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

67
        $this->setAuthentication(/** @scrutinizer ignore-type */ $username, $password);
Loading history...
Bug introduced by
It seems like $password can also be of type null; however, parameter $password of Solarium\Core\Client\Endpoint::setAuthentication() does only seem to accept string, 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

67
        $this->setAuthentication($username, /** @scrutinizer ignore-type */ $password);
Loading history...
68 76
    }
69
70
    /**
71
     * @param array $configuration
72
     * @return Node
73
     */
74 77
    public static function fromArray(array $configuration): Node
75
    {
76 77
        static::checkIfRequiredKeyIsSet($configuration, 'scheme');
77 76
        static::checkIfRequiredKeyIsSet($configuration, 'host');
78 76
        static::checkIfRequiredKeyIsSet($configuration, 'port');
79 76
        static::checkIfRequiredKeyIsSet($configuration, 'path');
80
81 76
        $scheme = $configuration['scheme'];
82 76
        $host = $configuration['host'];
83 76
        $port = $configuration['port'];
84 76
        $path = $configuration['path'];
85
86 76
        $username = $configuration['username'] ?? '';
87 76
        $password = $configuration['password'] ?? '';
88 76
        return new Node($scheme, $host, $port, $path, $username, $password);
0 ignored issues
show
Deprecated Code introduced by
The class ApacheSolrForTypo3\Solr\System\Solr\Node has been deprecated: Class will removed with Ext:solr 12.x. Use class \Solarium\Core\Client\Endpoint instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        return /** @scrutinizer ignore-deprecated */ new Node($scheme, $host, $port, $path, $username, $password);
Loading history...
89
    }
90
91
    /**
92
     * Checks if the required configuration option is set.
93
     *
94
     * @param array  $configuration
95
     * @param string $name
96
     * @throws |UnexpectedValueException
97
     */
98 77
    protected static function checkIfRequiredKeyIsSet(array $configuration, string $name)
99
    {
100 77
        if (empty($configuration[$name])) {
101 1
            throw new \UnexpectedValueException('Required solr connection property ' . $name. ' is missing.');
102
        }
103 76
    }
104
105
    /**
106
     * @return string
107
     */
108 68
    public function getUsername(): string
109
    {
110 68
        return (string)$this->getOption('username');
111
    }
112
113
    /**
114
     * @return string
115
     */
116 1
    public function getPassword(): string
117
    {
118 1
        return (string)$this->getOption('password');
119
    }
120
121
    /**
122
     * Returns the path including api path.
123
     *
124
     * @return string
125
     */
126
    public function getCoreBasePath(): string
127
    {
128
        $pathWithoutLeadingAndTrailingSlashes = trim(trim($this->getPath()), "/");
0 ignored issues
show
Bug introduced by
It seems like $this->getPath() can also be of type null; however, parameter $string of trim() does only seem to accept string, 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

128
        $pathWithoutLeadingAndTrailingSlashes = trim(trim(/** @scrutinizer ignore-type */ $this->getPath()), "/");
Loading history...
129
        $pathWithoutLastSegment = substr($pathWithoutLeadingAndTrailingSlashes, 0, strrpos($pathWithoutLeadingAndTrailingSlashes, "/"));
130
        return ($pathWithoutLastSegment === '') ? '/' : '/' . $pathWithoutLastSegment . '/';
131
    }
132
133
    /**
134
     * Returns the core name from the configured path.
135
     *
136
     * @return string
137
     * @deprecated Will be remove with Ext:solr 12.x. Use method getCore() instead.
138
     */
139
    public function getCoreName(): string
140
    {
141
        return $this->getCore();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getCore() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
142
    }
143
144
    /**
145
     * @return array
146
     */
147 66
    public function getSolariumClientOptions(): array
148
    {
149
        return [
150 66
            'host' => $this->getHost(),
151 66
            'port' => $this->getPort(),
152 66
            'scheme' => $this->getScheme(),
153 66
            'path' => $this->getPath(),
154 66
            'core' => $this->getCore()
155
        ];
156
    }
157
158
    /**
159
     * @return string
160
     * @deprecated Will be removed with Ext:solr 12.x. Use methods getCoreBaseUri() for API version 1 instead
161
     */
162 11
    public function __toString(): string
163
    {
164 11
        return $this->getCoreBaseUri();
165
    }
166
}
167