Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( cade9c...80ea4b )
by Rafael
31:38 queued 15:56
created

ResponseAdapter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 27
c 2
b 0
f 0
dl 0
loc 119
ccs 29
cts 31
cp 0.9355
rs 10

8 Methods

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