Passed
Pull Request — master (#662)
by Songda
02:09 queued 23s
created

LaunchPlugin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 1
b 0
f 0
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 16 2
A validateResponse() 0 10 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Unipay;
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\ContainerException
19
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
20
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
21
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
22
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
23
     */
24
    public function assembly(Rocket $rocket, Closure $next): Rocket
25
    {
26
        /* @var Rocket $rocket */
27
        $rocket = $next($rocket);
28
29
        Logger::info('[unipay][LaunchPlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        if (should_do_http_request($rocket->getDirection())) {
32
            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

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