Completed
Pull Request — master (#22)
by
unknown
03:39
created

CsvResponse::getRawData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Collmex API Response
4
 *
5
 * @author    Marcus Jaschen <[email protected]>
6
 * @license   http://www.opensource.org/licenses/mit-license MIT License
7
 * @link      https://github.com/mjaschen/collmex
8
 */
9
10
namespace MarcusJaschen\Collmex\Response;
11
12
use MarcusJaschen\Collmex\Csv\ParserInterface;
13
use MarcusJaschen\Collmex\Filter\Windows1252ToUtf8;
14
use MarcusJaschen\Collmex\TypeFactory;
15
16
/**
17
 * Collmex API Response
18
 *
19
 * @author   Marcus Jaschen <[email protected]>
20
 * @license  http://www.opensource.org/licenses/mit-license MIT License
21
 * @link     https://github.com/mjaschen/collmex
22
 */
23
class CsvResponse implements ResponseInterface
24
{
25
    /**
26
     * @var \MarcusJaschen\Collmex\Csv\ParserInterface
27
     */
28
    protected $parser;
29
30
    /**
31
     * The response CSV string
32
     *
33
     * @var string
34
     */
35
    protected $rawData;
36
37
    /**
38
     * The response CSV parsed into an array (each CSV line an array element)
39
     *
40
     * @var array
41
     */
42
    protected $response;
43
44
    /**
45
     * @var array of raw response data
46
     */
47
    protected $data;
48
49
    /**
50
     * @var array of Type records
51
     */
52
    protected $records;
53
54
    /**
55
     * Whether the response contains an error message or not
56
     *
57
     * @var bool
58
     */
59
    protected $isError;
60
61
    /**
62
     * Collmex error-code
63
     * @var string
64
     */
65
    protected $errorCode;
66
67
    /**
68
     * @var string
69
     */
70
    protected $errorMessage;
71
72
    /**
73
     * Line of request CSV where the error occurred.
74
     *
75
     * @var int
76
     */
77
    protected $errorLine;
78
79
    /**
80
     * @param \MarcusJaschen\Collmex\Csv\ParserInterface $parser
81
     * @param string $responseBody
82
     *
83
     * @throws \MarcusJaschen\Collmex\Exception\RequestErrorException
84
     */
85
    public function __construct(ParserInterface $parser, $responseBody)
86
    {
87
        $this->parser   = $parser;
88
        $this->rawData  = $responseBody;
89
        $this->response = $this->parseCsv($responseBody);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseCsv($responseBody) can be null. However, the property $response is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
90
    }
91
92
    /**
93
     * @param string $responseBody
94
     *
95
     * @throws \MarcusJaschen\Collmex\Exception\RequestErrorException
96
     *
97
     * @return array
98
     */
99
    public function parseCsv($responseBody)
100
    {
101
        $this->data = $this->convertEncodingFromCollmex(
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->convertEncodingFr...->parse($responseBody)) of type string is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102
            $this->parser->parse(
0 ignored issues
show
Documentation introduced by
$this->parser->parse($responseBody) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
                $responseBody
104
            )
105
        );
106
    }
107
108
    /**
109
     * Checks if the API request returned an error
110
     */
111
    public function isError()
112
    {
113
        // do not process the response data (again) if an error was
114
        // already detected
115
        if (null !== $this->isError) {
116
            return $this->isError;
117
        }
118
119
        foreach ($this->data as $data) {
120
            if ($data[0] === 'MESSAGE' && $data[1] === 'E') {
121
                $this->isError      = true;
122
                $this->errorCode    = $data[2];
123
                $this->errorMessage = $data[3];
124
125
                if (isset($data[4])) {
126
                    $this->errorLine = $data[4];
127
                }
128
129
                return true;
130
            }
131
        }
132
133
        $this->isError = false;
134
135
        return false;
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function getErrorMessage()
142
    {
143
        return $this->errorMessage;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getErrorCode()
150
    {
151
        return $this->errorCode;
152
    }
153
154
    /**
155
     * @return int
156
     */
157
    public function getErrorLine()
158
    {
159
        return $this->errorLine;
160
    }
161
162
    /**
163
     * Returns the original csv-string from the collmex request.
164
     *
165
     * @return string|null
166
     */
167
    public function getRawData()
168
    {
169
        if (empty($this->rawData)) {
170
            return null;
171
        }
172
173
        return $this->rawData;
174
    }
175
176
    /**
177
     * Returns an array of all response records as Type objects.
178
     *
179
     * @return array|null
180
     *
181
     * @throws \MarcusJaschen\Collmex\Exception\InvalidTypeIdentifierException
182
     */
183
    public function getRecords()
184
    {
185
        if ($this->isError()) {
186
            return null;
187
        }
188
189
        if (empty($this->data)) {
190
            return null;
191
        }
192
193
        if (null === $this->records) {
194
            $this->createTypeRecords();
195
        }
196
197
        return $this->records;
198
    }
199
200
    /**
201
     * Returns the Type object of the first record (or null if response
202
     * contains no records).
203
     *
204
     * @return array|null
205
     */
206
    public function getFirstRecord()
207
    {
208
        $records = $this->getRecords();
209
210
        if (empty($records)) {
211
            return null;
212
        }
213
214
        return $records[0];
215
    }
216
217
    /**
218
     * Populates the $records attribute with Type objects, created
219
     * from $data attribute.
220
     *
221
     * @return void
222
     *
223
     * @throws \MarcusJaschen\Collmex\Exception\InvalidTypeIdentifierException
224
     */
225
    protected function createTypeRecords()
226
    {
227
        $typeFactory = new TypeFactory();
228
229
        foreach ($this->data as $data) {
230
            $this->records[] = $typeFactory->getType($data);
231
        }
232
    }
233
234
    /**
235
     * Converts response from Collmex API to UTF-8
236
     *
237
     * @param string $text
238
     *
239
     * @return string
240
     */
241
    protected function convertEncodingFromCollmex($text)
242
    {
243
        $filter = new Windows1252ToUtf8();
244
245
        return $filter->filter($text);
246
    }
247
}
248