Passed
Pull Request — master (#896)
by Songda
01:56
created

CollectionDirection::guide()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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