Passed
Branch main (7dbc49)
by Rafael
04:08
created

Node::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

71
        $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

71
        $this->setAuthentication($username, /** @scrutinizer ignore-type */ $password);
Loading history...
72
    }
73
74
    /**
75
     * @param array $configuration
76
     * @return Node
77
     */
78 128
    public static function fromArray(array $configuration): Node
79
    {
80 128
        static::checkIfRequiredKeyIsSet($configuration, 'scheme');
81 127
        static::checkIfRequiredKeyIsSet($configuration, 'host');
82 127
        static::checkIfRequiredKeyIsSet($configuration, 'port');
83 127
        static::checkIfRequiredKeyIsSet($configuration, 'path');
84
85 127
        $scheme = $configuration['scheme'];
86 127
        $host = $configuration['host'];
87 127
        $port = $configuration['port'];
88 127
        $path = $configuration['path'];
89
90 127
        $username = $configuration['username'] ?? '';
91 127
        $password = $configuration['password'] ?? '';
92 127
        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 be 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

92
        return /** @scrutinizer ignore-deprecated */ new Node($scheme, $host, $port, $path, $username, $password);
Loading history...
93
    }
94
95
    /**
96
     * Checks if the required configuration option is set.
97
     *
98
     * @param array  $configuration
99
     * @param string $name
100
     * @throws UnexpectedValueException
101
     */
102 128
    protected static function checkIfRequiredKeyIsSet(array $configuration, string $name)
103
    {
104 128
        if (empty($configuration[$name])) {
105 1
            throw new UnexpectedValueException('Required solr connection property ' . $name . ' is missing.');
106
        }
107
    }
108
109
    /**
110
     * @return string
111
     */
112 112
    public function getUsername(): string
113
    {
114 112
        return (string)$this->getOption('username');
115
    }
116
117
    /**
118
     * @return string
119
     */
120 1
    public function getPassword(): string
121
    {
122 1
        return (string)$this->getOption('password');
123
    }
124
125
    /**
126
     * Returns the path including api path.
127
     *
128
     * @return string
129
     */
130
    public function getCoreBasePath(): string
131
    {
132
        $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

132
        $pathWithoutLeadingAndTrailingSlashes = trim(trim(/** @scrutinizer ignore-type */ $this->getPath()), '/');
Loading history...
133
        $pathWithoutLastSegment = substr($pathWithoutLeadingAndTrailingSlashes, 0, strrpos($pathWithoutLeadingAndTrailingSlashes, '/'));
134
        return ($pathWithoutLastSegment === '') ? '/' : '/' . $pathWithoutLastSegment . '/';
135
    }
136
137
    /**
138
     * Returns the core name from the configured path.
139
     *
140
     * @return string
141
     * @deprecated Will be removed with Ext:solr 12.x. Use method getCore() instead.
142
     */
143
    public function getCoreName(): string
144
    {
145
        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...
146
    }
147
148
    /**
149
     * @return array
150
     */
151 110
    public function getSolariumClientOptions(): array
152
    {
153
        return [
154 110
            'host' => $this->getHost(),
155 110
            'port' => $this->getPort(),
156 110
            'scheme' => $this->getScheme(),
157 110
            'path' => $this->getPath(),
158 110
            'core' => $this->getCore(),
159
        ];
160
    }
161
162
    /**
163
     * @return string
164
     * @deprecated Will be removed with Ext:solr 12.x. Use methods getCoreBaseUri() for API version 1 instead
165
     */
166 11
    public function __toString(): string
167
    {
168 11
        return $this->getCoreBaseUri();
169
    }
170
}
171