Passed
Push — master ( ee59b5...01c6ee )
by Songda
02:33
created

LaunchPlugin::validateResponse()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat;
6
7
use Closure;
8
use Psr\Http\Message\ResponseInterface;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidResponseException;
12
use Yansongda\Pay\Logger;
13
use Yansongda\Pay\Rocket;
14
15
class LaunchPlugin implements PluginInterface
16
{
17
    /**
18
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
19
     * @throws \Yansongda\Pay\Exception\ContainerException
20
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
21
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
22
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
23
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
24
     */
25
    public function assembly(Rocket $rocket, Closure $next): Rocket
26
    {
27
        /* @var Rocket $rocket */
28
        $rocket = $next($rocket);
29
30
        Logger::info('[wechat][LaunchPlugin] 插件开始装载', ['rocket' => $rocket]);
31
32
        if (should_do_http_request($rocket)) {
33
            verify_wechat_sign($rocket->getDestinationOrigin(), $rocket->getParams());
0 ignored issues
show
Bug introduced by
It seems like $rocket->getDestinationOrigin() can also be of type null; however, parameter $message of verify_wechat_sign() does only seem to accept Psr\Http\Message\MessageInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

33
            verify_wechat_sign(/** @scrutinizer ignore-type */ $rocket->getDestinationOrigin(), $rocket->getParams());
Loading history...
34
35
            $rocket->setDestination($this->validateResponse($rocket));
36
        }
37
38
        Logger::info('[wechat][LaunchPlugin] 插件装载完毕', ['rocket' => $rocket]);
39
40
        return $rocket;
41
    }
42
43
    /**
44
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
45
     *
46
     * @return array|\Psr\Http\Message\MessageInterface|\Yansongda\Supports\Collection|null
47
     */
48
    protected function validateResponse(Rocket $rocket)
49
    {
50
        $response = $rocket->getDestination();
51
52
        if ($response instanceof ResponseInterface &&
53
            ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300)) {
54
            throw new InvalidResponseException(Exception::INVALID_RESPONSE_CODE);
55
        }
56
57
        return $response;
58
    }
59
}
60