Passed
Push — master ( 29c231...ae3f68 )
by Timo
25:07
created

ResponseAdapter::count()   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
namespace ApacheSolrForTypo3\Solr\System\Solr;
3
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
4
use Countable;
5
6
/**
7
 * In EXT:solr 9 we have switched from the SolrPhpClient to the solarium api.
8
 *
9
 * In many places of the code the class Apache_Solr_Response and the property Apache_Solr_Response::reponse is used.
10
 * To be able to refactor this we need to have a replacement for Apache_Solr_Response that behaves like the original class,
11
 * to keep the old code working. This allows us to drop the old code of SolrPhpClient and refactore the other parts step by step.
12
 *
13
 * Class ResponseAdapter
14
 *
15
 * Search response
16
 *
17
 * @property \stdClass facet_counts
18
 * @property \stdClass facets
19
 * @property \stdClass spellcheck
20
 * @property \stdClass response
21
 * @property \stdClass responseHeader
22
 * @property \stdClass highlighting
23
 * @property \stdClass debug
24
 * @property \stdClass lucene
25
 * 
26
 * Luke response
27
 *
28
 * @property \stdClass index
29
 * @property \stdClass fields
30
 */
31
class ResponseAdapter implements Countable
32
{
33
    /**
34
     * @var string
35
     */
36
    protected $responseBody;
37
38
    /**
39
     * @var \stdClass
40
     */
41
    protected $data = null;
42
43
    /**
44
     * @var int
45
     */
46
    protected $httpStatus = 200;
47
48
    /**
49
     * @var string
50
     */
51
    protected $httpStatusMessage = '';
52
53
    /**
54
     * ResponseAdapter constructor.
55
     *
56
     * @param string $responseBody
57
     * @param int $httpStatus
58
     * @param string $httpStatusMessage
59
     */
60 168
    public function __construct($responseBody, $httpStatus = 500, $httpStatusMessage = '')
61
    {
62 168
        $this->data = json_decode($responseBody);
63 168
        $this->responseBody = $responseBody;
64 168
        $this->httpStatus = $httpStatus;
65 168
        $this->httpStatusMessage = $httpStatusMessage;
66
67 168
        if (isset($this->data->response) && is_array($this->data->response->docs)) {
68 84
            $documents = array();
69
70 84
            foreach ($this->data->response->docs as $originalDocument) {
71 46
                $fields = get_object_vars($originalDocument);
72 46
                $document = new Document($fields);
73 46
                $documents[] = $document;
74
            }
75
76 84
            $this->data->response->docs = $documents;
77
        }
78 168
    }
79
80
    /**
81
     * Magic get to expose the parsed data and to lazily load it
82
     *
83
     * @param string $key
84
     * @return mixed
85
     */
86 92
    public function __get($key)
87
    {
88 92
        if (isset($this->data->$key)) {
89 91
            return $this->data->$key;
90
        }
91
92 61
        return null;
93
    }
94
95
    /**
96
     * Magic function for isset function on parsed data
97
     *
98
     * @param string $key
99
     * @return boolean
100
     */
101 71
    public function __isset($key)
102
    {
103 71
        return isset($this->data->$key);
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109 52
    public function getParsedData()
110
    {
111 52
        return $this->data;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 12
    public function getRawResponse()
118
    {
119 12
        return $this->responseBody;
120
    }
121
122
    /**
123
     * @return int
124
     */
125 110
    public function getHttpStatus(): int
126
    {
127 110
        return $this->httpStatus;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getHttpStatusMessage(): string
134
    {
135
        return $this->httpStatusMessage;
136
    }
137
138
    /**
139
     * Counts the elements of
140
     */
141 2
    public function count()
142
    {
143 2
        return count(get_object_vars($this->data));
144
    }
145
}