Passed
Pull Request — master (#1014)
by Songda
12:36
created

ResponsePlugin::validateResponse()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Douyin\V1\Pay;
6
7
use Closure;
8
use Psr\Http\Message\ResponseInterface;
9
use Yansongda\Artful\Contract\PluginInterface;
10
use Yansongda\Artful\Exception\InvalidResponseException;
11
use Yansongda\Artful\Logger;
12
use Yansongda\Artful\Rocket;
13
use Yansongda\Pay\Exception\Exception;
14
use Yansongda\Supports\Collection;
15
16
class ResponsePlugin implements PluginInterface
17
{
18
    /**
19
     * @throws InvalidResponseException
20
     */
21
    public function assembly(Rocket $rocket, Closure $next): Rocket
22
    {
23
        /* @var Rocket $rocket */
24
        $rocket = $next($rocket);
25
26
        Logger::debug('[Douyin][ResponsePlugin] 插件开始装载', ['rocket' => $rocket]);
27
28
        $rocket->setDestination(new Collection($this->validateResponse($rocket)));
29
30
        Logger::info('[Douyin][ResponsePlugin] 插件装载完毕', ['rocket' => $rocket]);
31
32
        return $rocket;
33
    }
34
35
    /**
36
     * @throws InvalidResponseException
37
     */
38
    protected function validateResponse(Rocket $rocket): array
39
    {
40
        $destination = $rocket->getDestination();
41
        $response = $rocket->getDestinationOrigin();
42
43
        if ($response instanceof ResponseInterface
44
            && ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300)) {
45
            throw new InvalidResponseException(Exception::RESPONSE_CODE_WRONG, '抖音返回状态码异常,请检查参数是否错误', $destination);
46
        }
47
48
        if (0 !== $destination->get('err_no')) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on Psr\Http\Message\MessageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        if (0 !== $destination->/** @scrutinizer ignore-call */ get('err_no')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            throw new InvalidResponseException(Exception::RESPONSE_BUSINESS_CODE_WRONG, '抖音返回业务异常: '.$destination->get('err_tips'), $destination);
50
        }
51
52
        return $destination->get('data', []);
53
    }
54
}
55