SolrRequest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 144
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A factory() 0 4 1
A init() 0 1 1
A set() 0 8 3
A setHandlerName() 0 5 1
A getHandlerName() 0 4 1
A renderBody() 0 4 1
A escapeXml() 0 4 1
A stripCtrlChars() 0 6 1
A addComponent() 0 5 1
A sendRequest() 0 14 3
1
<?php
2
3
namespace PSolr\Request;
4
5
use Guzzle\Http\QueryString;
6
use PSolr\Client\SolrClient;
7
8
/**
9
 * Base class for PSolr request objects.
10
 */
11
class SolrRequest extends QueryString
12
{
13
    /**
14
     * @var string|null
15
     */
16
    protected $handlerName = null;
17
18
    /**
19
     * $var string
20
     */
21
    protected $responseClass = '\PSolr\Response\Response';
22
23
    /**
24
     * @param array $params
25
     */
26
    public function __construct(array $params = array())
27
    {
28
        parent::__construct($params);
29
        $this->init();
30
    }
31
32
    /**
33
     * @param array $params
34
     *
35
     * @return \PSolr\Request\SolrRequest
36
     */
37
    static public function factory(array $params = array())
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
38
    {
39
        return new static($params);
40
    }
41
42
    /**
43
     * Initialization hook. Useful for setting default params.
44
     */
45
    public function init() {}
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * Converts booleans to strings.
51
     */
52
    public function set($key, $value)
53
    {
54
        if (is_bool($value)) {
55
            $value = $value ? 'true' : 'false';
56
        }
57
58
        return parent::set($key, $value);
59
    }
60
61
    /**
62
     * @param string $handlerName
63
     *
64
     * @return \PSolr\Request\SolrRequest
65
     */
66
    public function setHandlerName($handlerName)
67
    {
68
        $this->handlerName = $handlerName;
69
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getHandlerName()
76
    {
77
        return $this->handlerName;
78
    }
79
80
    /**
81
     * Renders the body, most often overridden by request objects that generate
82
     * JSON or XML, e.g. update requests.
83
     *
84
     * @return string|null
85
     */
86
    public function renderBody()
87
    {
88
        return null;
89
    }
90
91
    /**
92
     * Escape a value for use in XML.
93
     *
94
     * @param string $value
95
     *
96
     * @return string
97
     */
98
    public static function escapeXml($value)
99
    {
100
        return htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
101
    }
102
103
    /**
104
     * Replace control (non-printable) characters from string that are invalid
105
     * to Solr's XML parser with a space.
106
     *
107
     * @param string $rawXml
108
     *
109
     * @return string
110
     *
111
     * @see http://drupalcode.org/project/apachesolr.git/blob/1dc510227f5077ccbc047be13dcf0de3120b100c:/Apache_Solr_Document.php#l399
112
     */
113
    public static function stripCtrlChars($rawXml)
114
    {
115
        // @see http://w3.org/International/questions/qa-forms-utf-8.html
116
        // Printable utf-8 does not include any of these chars below x7F
117
        return preg_replace('@[\x00-\x08\x0B\x0C\x0E-\x1F]@', ' ', $rawXml);
118
    }
119
120
    /**
121
     * @param \Psolr\Component\ComponentInterface $component
122
     *
123
     * @return \PSolr\Request\SolrRequest
124
     */
125
    public function addComponent(ComponentInterface $component)
126
    {
127
        $component->preMergeParams($this);
128
        return $this->overwriteWith($component->toArray());
129
    }
130
131
    /**
132
     * @param \PSolr\Request\SolrClient $solr
133
     * @param array|null $headers
134
     * @param array $options
135
     *
136
     * @return \PSolr\Response\Response|\SimpleXMLElement
137
     *
138
     * @throws \UnexpectedValueException
139
     */
140
    public function sendRequest(SolrClient $solr, $headers = null, array $options = array())
141
    {
142
        // If we don't have a request handler, then the passed request is only a
143
        // component and we cannot execute the search.
144
        if (null === $this->handlerName) {
145
            throw new \UnexpectedValueException('Unable to send request: handler name missing');
146
        }
147
148
        // @todo Add a method in \PSolr\Response\Response to normalize the XML
149
        // in to an array. Just use JSON and all is right in the world.
150
        $params = $this->toArray();
151
        $data = $solr->sendRequest($this->handlerName, $params, $this->renderBody(), $headers, $options);
152
        return is_array($data) ? new $this->responseClass($data, $params) : $data;
153
    }
154
}
155