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

LaunchPlugin::getAlipayPublicKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Alipay;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Exception\InvalidResponseException;
10
use Yansongda\Pay\Rocket;
11
use Yansongda\Supports\Collection;
12
13
class LaunchPlugin implements PluginInterface
14
{
15
    /**
16
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
17
     * @throws \Yansongda\Pay\Exception\ContainerException
18
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
19
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
20
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
21
     */
22
    public function assembly(Rocket $rocket, Closure $next): Rocket
23
    {
24
        /* @var Rocket $rocket */
25
        $rocket = $next($rocket);
26
27
        if (!should_http_request($rocket)) {
28
            return $rocket;
29
        }
30
31
        $rocket->setDestination($this->getMethodResponse($rocket));
32
33
        return $rocket;
34
    }
35
36
    /**
37
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
38
     * @throws \Yansongda\Pay\Exception\ContainerException
39
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
40
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
41
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
42
     */
43
    protected function getMethodResponse(Rocket $rocket): Collection
44
    {
45
        $response = Collection::wrap(
46
            $rocket->getDestination()->get($this->getResponseKey($rocket))
0 ignored issues
show
Bug introduced by
The method get() does not exist on Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

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

46
            $rocket->getDestination()->/** @scrutinizer ignore-call */ get($this->getResponseKey($rocket))

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...
47
        );
48
49
        $this->verifySign($rocket);
50
51
        if (10000 != $response->get('code', 10000)) {
52
            throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_CODE, 'Invalid response code', $response->all());
53
        }
54
55
        return $response;
56
    }
57
58
    /**
59
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
60
     * @throws \Yansongda\Pay\Exception\ContainerException
61
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
62
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
63
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
64
     */
65
    protected function verifySign(Rocket $rocket): void
66
    {
67
        $response = $rocket->getDestination()->get($this->getResponseKey($rocket));
68
        $sign = $rocket->getDestination()->get('sign', '');
69
70
        if ('' === $sign || is_null($response)) {
71
            throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN, '', $response);
72
        }
73
74
        if (!verify_alipay_response($rocket->getParams(), json_encode($response, JSON_UNESCAPED_UNICODE), base64_decode($sign))) {
75
            throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN, '', $response);
76
        }
77
    }
78
79
    protected function getResponseKey(Rocket $rocket): string
80
    {
81
        $method = $rocket->getPayload()->get('method');
82
83
        return str_replace('.', '_', $method).'_response';
84
    }
85
}
86