Passed
Push — master ( cc3f84...4a930e )
by Timo
23:43
created

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