Passed
Push — release-11.5.x ( 39fc07...8ccd81 )
by Markus
34:52 queued 29:33
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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
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 ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
21
use Countable;
22
use stdClass;
23
24
/**
25
 * In EXT:solr 9 we have switched from the SolrPhpClient to the solarium api.
26
 *
27
 * In many places of the code the class Apache_Solr_Response and the property Apache_Solr_Response::response is used.
28
 * To be able to refactor this we need to have a replacement for Apache_Solr_Response that behaves like the original class,
29
 * to keep the old code working. This allows us to drop the old code of SolrPhpClient and refactor the other parts step by step.
30
 *
31
 * Class ResponseAdapter
32
 *
33
 * Search response
34
 *
35
 * @property stdClass|null facet_counts
36
 * @property stdClass|null facets
37
 * @property stdClass|null spellcheck
38
 * @property stdClass|null response
39
 * @property stdClass|null responseHeader
40
 * @property stdClass|null highlighting
41
 * @property stdClass|null debug
42
 * @property stdClass|null lucene
43
 * @property string file
44
 * @property array file_metadata
45
 *
46
 * Luke response
47
 *
48
 * @property stdClass index
49
 * @property stdClass fields
50
 * @property stdClass $plugins
51
 */
52
class ResponseAdapter implements Countable
53
{
54
    /**
55
     * @var ?string
56
     */
57
    protected ?string $responseBody = null;
58
59
    /**
60
     * @var stdClass|null
61
     */
62
    protected ?stdClass $data = null;
63
64
    /**
65
     * @var int
66
     */
67
    protected int $httpStatus = 200;
68
69
    /**
70
     * @var string
71
     */
72
    protected string $httpStatusMessage = '';
73
74
    /**
75
     * ResponseAdapter constructor.
76
     *
77
     * @param string|null $responseBody
78
     * @param int $httpStatus
79
     * @param string $httpStatusMessage
80
     */
81 183
    public function __construct(?string $responseBody, int $httpStatus = 500, string $httpStatusMessage = '')
82
    {
83 183
        $this->data = json_decode($responseBody ?? '');
84 183
        $this->responseBody = $responseBody;
85 183
        $this->httpStatus = $httpStatus;
86 183
        $this->httpStatusMessage = $httpStatusMessage;
87
88
        // @extensionScannerIgnoreLine
89 183
        if (isset($this->data->response) && is_array($this->data->response->docs ?? null)) {
90 82
            $documents = [];
91
92
            // @extensionScannerIgnoreLine
93 82
            foreach ($this->data->response->docs as $originalDocument) {
94 44
                $fields = get_object_vars($originalDocument);
95 44
                $document = new Document($fields);
96 44
                $documents[] = $document;
97
            }
98
99
            // @extensionScannerIgnoreLine
100 82
            $this->data->response->docs = $documents;
101
        }
102
    }
103
104
    /**
105
     * Magic get to expose the parsed data and to lazily load it
106
     *
107
     * @param string $key
108
     * @return mixed
109
     */
110 96
    public function __get(string $key)
111
    {
112 96
        if (isset($this->data->$key)) {
113 88
            return $this->data->$key;
114
        }
115
116 41
        return null;
117
    }
118
119
    /**
120
     * Magic function for isset function on parsed data
121
     *
122
     * @param string $key
123
     * @return bool
124
     */
125 78
    public function __isset(string $key)
126
    {
127 78
        return isset($this->data->$key);
128
    }
129
130
    /**
131
     * @return mixed
132
     */
133 49
    public function getParsedData()
134
    {
135 49
        return $this->data;
136
    }
137
138
    /**
139
     * @return ?string
140
     */
141 20
    public function getRawResponse(): ?string
142
    {
143 20
        return $this->responseBody;
144
    }
145
146
    /**
147
     * @return int
148
     */
149 132
    public function getHttpStatus(): int
150
    {
151 132
        return $this->httpStatus;
152
    }
153
154
    /**
155
     * @return string
156
     */
157 5
    public function getHttpStatusMessage(): string
158
    {
159 5
        return $this->httpStatusMessage;
160
    }
161
162
    /**
163
     * Counts the elements of
164
     */
165 4
    public function count(): int
166
    {
167 4
        if ($this->data !== null) {
168 3
            return count(get_object_vars($this->data));
169
        }
170 1
        return 0;
171
    }
172
}
173