JsonResponse::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 2.0078
rs 10
1
<?php
2
3
namespace CommerceGuys\AuthNet\Response;
4
5
use CommerceGuys\AuthNet\Exception\AuthNetException;
6
use Psr\Http\Message\ResponseInterface as HttpResponseInterface;
7
8
class JsonResponse extends BaseResponse
9
{
10
11 8
    public function __construct(HttpResponseInterface $response)
12
    {
13 8
        parent::__construct($response);
14 8
        $content = $response->getBody()->getContents();
15
        // The API returns hidden Byte Order Mark characters, causing the
16
        // JSON decoding to fail.
17 8
        $content = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $content);
18 8
        $content = json_decode($content);
19
20 8
        if (json_last_error() !== JSON_ERROR_NONE) {
21
            throw new AuthNetException(sprintf("JSON returned from API was not decoded: %s", json_last_error_msg()));
22
        }
23 8
        $this->contents = $content;
24 8
    }
25
}
26