Passed
Push — master ( 96d02d...d08d15 )
by Songda
02:13 queued 10s
created

ArrayParser::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Parser;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Yansongda\Pay\Contract\ParserInterface;
9
use Yansongda\Pay\Exception\InvalidResponseException;
10
11
class ArrayParser implements ParserInterface
12
{
13
    /**
14
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
15
     */
16
    public function parse(?ResponseInterface $response): array
17
    {
18
        if (is_null($response)) {
19
            throw new InvalidResponseException(InvalidResponseException::RESPONSE_NONE);
20
        }
21
22
        $contents = $response->getBody()->getContents();
23
24
        $result = json_decode($contents, true);
25
26
        if (JSON_ERROR_NONE !== json_last_error()) {
27
            throw new InvalidResponseException(InvalidResponseException::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', [$contents]);
28
        }
29
30
        return $result;
31
    }
32
}
33