SolrClient   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 78.31%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 6
dl 0
loc 215
ccs 65
cts 83
cp 0.7831
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 48 1
A hasRequestHandler() 0 4 1
A setRequestHandler() 0 6 1
A getRequestHandler() 0 7 2
A removeRequestHandler() 0 5 1
B sendRequest() 0 23 5
A normalizeParams() 0 9 3
A createRequest() 0 10 3
A usePostMethod() 0 8 2
A parseResponse() 0 5 3
A __call() 0 5 1
1
<?php
2
3
namespace PSolr\Client;
4
5
use Guzzle\Common\Collection;
6
use Guzzle\Http\Message\Response;
7
use Guzzle\Http\Url;
8
use Guzzle\Service\Client;
9
10
/**
11
 * @method array luke(   $params = array(), $body = null, $headers = null, array $options = array())
12
 * @method array mbeans( $params = array(), $body = null, $headers = null, array $options = array())
13
 * @method array mlt(    $params = array(), $body = null, $headers = null, array $options = array())
14
 * @method array ping(   $params = array(), $body = null, $headers = null, array $options = array())
15
 * @method array select( $params = array(), $body = null, $headers = null, array $options = array())
16
 * @method array spell(  $params = array(), $body = null, $headers = null, array $options = array())
17
 * @method array stats(  $params = array(), $body = null, $headers = null, array $options = array())
18
 * @method array suggest($params = array(), $body = null, $headers = null, array $options = array())
19
 * @method array system( $params = array(), $body = null, $headers = null, array $options = array())
20
 * @method array update( $params = array(), $body = null, $headers = null, array $options = array())
21
 */
22
class SolrClient extends Client
23
{
24
    const MAX_QUERY_LENGTH = 3600;
25
26
    /**
27
     * @var \PSolr\Client\RequestHandler[]
28
     */
29
    protected $handlers = array();
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @return \PSolr\Client\SolrClient
35
     */
36 28
    public static function factory($config = array())
37
    {
38
        $defaults = array(
39 28
            'base_url' => 'http://localhost:8983',
40 28
            'base_path' => '/solr',
41 28
            'max_query_length' => self::MAX_QUERY_LENGTH,
42 28
        );
43
44
        $required = array(
45 28
            'base_url',
46 28
            'base_path',
47 28
            'max_query_length',
48 28
        );
49
50
        // Instantiate and return the Solr client.
51 28
        $config = Collection::fromConfig($config, $defaults, $required);
52 28
        $solr = new static($config->get('base_url'), $config);
53
54
        // Use URI template expansion in a way that doesn't break Solr.
55 28
        $solr->setUriTemplate(new SolrUriTemplate());
56
57
        // Use JSON whenever possible.
58
        // @see http://code.google.com/p/solr-php-client/issues/detail?id=6#c1
59
        $jsonParams = array(
60 28
            'wt'      => 'json',
61 28
            'json.nl' => 'map',
62 28
        );
63
64
        // Sometimes we have to use XML :-(.
65
        $xmlParams = array(
66 28
            'wt' => 'xml',
67 28
        );
68
69
        $solr
70 28
            ->setRequestHandler(new RequestHandler('luke',    'admin/luke',      'GET',  $jsonParams))
0 ignored issues
show
Documentation introduced by
'luke' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'admin/luke' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71 28
            ->setRequestHandler(new RequestHandler('mbeans',  'admin/mbeans',    'GET',  $xmlParams + array('stats' => 'true')))
0 ignored issues
show
Documentation introduced by
'mbeans' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'admin/mbeans' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72 28
            ->setRequestHandler(new RequestHandler('mlt',     'mlt',             'GET',  $jsonParams))
0 ignored issues
show
Documentation introduced by
'mlt' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73 28
            ->setRequestHandler(new RequestHandler('ping',    'admin/ping',      'HEAD', $jsonParams))
0 ignored issues
show
Documentation introduced by
'ping' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'admin/ping' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74 28
            ->setRequestHandler(new RequestHandler('select',  'select',          'GET',  $jsonParams))
0 ignored issues
show
Documentation introduced by
'select' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75 28
            ->setRequestHandler(new RequestHandler('spell',   'spell',           'GET',  $jsonParams))
0 ignored issues
show
Documentation introduced by
'spell' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76 28
            ->setRequestHandler(new RequestHandler('stats',   'admin/stats.jsp', 'GET',  $xmlParams))
0 ignored issues
show
Documentation introduced by
'stats' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'admin/stats.jsp' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77 28
            ->setRequestHandler(new RequestHandler('suggest', 'suggest',         'GET',  $jsonParams))
0 ignored issues
show
Documentation introduced by
'suggest' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78 28
            ->setRequestHandler(new RequestHandler('system',  'admin/system',    'GET',  $jsonParams))
0 ignored issues
show
Documentation introduced by
'system' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'admin/system' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79 28
            ->setRequestHandler(new RequestHandler('update',  'update',          'POST', $jsonParams))
0 ignored issues
show
Documentation introduced by
'update' is of type string, but the function expects a object<PSolr\Client\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
        ;
81
82 28
        return $solr;
83
    }
84
85
    /**
86
     * @param string $handlerName
87
     *
88
     * @return boolean
89
     */
90 8
    public function hasRequestHandler($handlerName)
91
    {
92 8
        return isset($this->handlers[$handlerName]);
93
    }
94
95
    /**
96
     * @param \PSolr\Client\RequestHandler $handler
97
     *
98
     * @return \PSolr\Client\SolrClient
99
     */
100 28
    public function setRequestHandler(RequestHandler $handler)
101
    {
102 28
        $handlerName = $handler->getName();
103 28
        $this->handlers[$handlerName] = $handler;
104 28
        return $this;
105
    }
106
107
    /**
108
     * @param string $handlerName
109
     *
110
     * @return \PSolr\Client\RequestHandler
111
     *
112
     * @throws \OutOfBoundsException
113
     */
114 16
    public function getRequestHandler($handlerName)
115
    {
116 16
        if (!isset($this->handlers[$handlerName])) {
117 4
            throw new \OutOfBoundsException('Request handler not registered: ' . $handlerName);
118
        }
119 12
        return $this->handlers[$handlerName];
120
    }
121
122
    /**
123
     * @param string $handlerName
124
     *
125
     * @return \PSolr\Client\SolrClient
126
     */
127 4
    public function removeRequestHandler($handlerName)
128
    {
129 4
        unset($this->handlers[$handlerName]);
130 4
        return $this;
131
    }
132
133
    /**
134
     * @param string $handlerName
135
     * @param mixed $params
136
     * @param string|null $body
137
     * @param array|null $headers
138
     * @param array $options
139
     *
140
     * @return array|\SimpleXMLElement
141
     */
142 4
    public function sendRequest($handlerName, $params = array(), $body = null, $headers = null, array $options = array())
143
    {
144 4
        $handler = $this->getRequestHandler($handlerName);
145 4
        $params = $this->normalizeParams($handler, $params);
146
147
        // For GETs and HEADs, the params are the query string. For POSTs
148
        // without a body, the params are post data. For POSTs with a body, the
149
        // params are the qstring.
150 4
        $method = $handler->getMethod();
151 4
        if ('GET' == $method || 'HEAD' == $method) {
152 4
            $options['query'] = $params;
153 4
        } elseif ('POST' == $method) {
154
            if (null === $body) {
155
                $body = $params;
156
            } else {
157
                $options['query'] = $params;
158
            }
159
        }
160
161 4
        $request = $this->createRequest($method, $handler->getPath(), $headers, $body, $options);
162 4
        $response = $request->send();
163
        return $this->parseResponse($response, $params);
164
    }
165
166
    /**
167
     * Normalizes and merges default params.
168
     *
169
     * @param \PSolr\Client\RequestHandler $handler
170
     * @param mixed $params
171
     *
172
     * @return array
173
     */
174 8
    public function normalizeParams(RequestHandler $handler, $params)
175
    {
176 8
        if (is_string($params)) {
177 4
            $params = array('q' => $params);
178 8
        } elseif (!is_array($params)) {
179 4
            $params = (array) $params;
180 4
        }
181 8
        return array_merge($handler->getDefaultParams(), $params);
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     *
187
     * Prepends the {+base_path} expressions to the URI, converts the GET to a
188
     * POST if the query string is too long.
189
     */
190 4
    public function createRequest($method = 'GET', $uri = null, $headers = null, $body = null, array $options = array())
191
    {
192 4
        $uri = '{+base_path}/' . ltrim($uri, '/');
193 4
        if ('GET' == $method && $this->usePostMethod($uri, $options)) {
194
            $method = 'POST';
195
            $body = $options['query'];
196
            unset($options['query']);
197
        }
198 4
        return parent::createRequest($method, $uri, $headers, $body, $options);
199 4
    }
200
201
    /**
202
     * @param string $uri
203
     * @param array $options
204
     *
205
     * @return boolean
206
     */
207
    public function usePostMethod($uri, array $options)
208
    {
209
        if (isset($options['query'])) {
210
            $url = Url::factory($this->getBaseUrl())->combine($this->expandTemplate($uri, $options['query']));
0 ignored issues
show
Bug introduced by
It seems like $this->getBaseUrl() targeting Guzzle\Http\Client::getBaseUrl() can also be of type object<Guzzle\Http\Url>; however, Guzzle\Http\Url::factory() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
211
            return strlen($url) > $this->getConfig('max_query_length');
0 ignored issues
show
Documentation introduced by
'max_query_length' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
        }
213
        return false;
214
    }
215
216
    /**
217
     * @param \Guzzle\Http\Message\Response $response
218
     * @param array $params
219
     *
220
     * @return array|\SimpleXMLElement
221
     */
222
    public function parseResponse(Response $response, array $params)
223
    {
224
        $method = (isset($params['wt']) && 'json' == $params['wt']) ? 'json' : 'xml';
225
        return $response->$method();
226
    }
227
228
    /**
229
     * Allows request handlers to be called as methods.
230
     */
231 4
    public function __call($name, $arguments)
232
    {
233 4
        array_unshift($arguments, $name);
234 4
        return call_user_func_array(array($this, 'sendRequest'), $arguments);
235
    }
236
}
237