Passed
Pull Request — master (#530)
by Songda
02:28
created

LaunchPlugin::getResultKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
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\Exception;
10
use Yansongda\Pay\Exception\InvalidResponseException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Rocket;
13
use Yansongda\Supports\Collection;
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
     */
24
    public function assembly(Rocket $rocket, Closure $next): Rocket
25
    {
26
        /* @var Rocket $rocket */
27
        $rocket = $next($rocket);
28
29
        Logger::info('[alipay][LaunchPlugin] 插件开始装载', ['rocket' => $rocket]);
30
31
        if (should_do_http_request($rocket)) {
32
            $this->verifySign($rocket);
33
34
            $rocket->setDestination(
35
                Collection::wrap(
36
                    $rocket->getDestination()->get($this->getResultKey($rocket->getPayload()))
0 ignored issues
show
Bug introduced by
The method get() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Yansongda\Pay\Request. ( Ignorable by Annotation )

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

36
                    $rocket->getDestination()->/** @scrutinizer ignore-call */ get($this->getResultKey($rocket->getPayload()))
Loading history...
Bug introduced by
It seems like $rocket->getPayload() can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Ali...hPlugin::getResultKey() does only seem to accept Yansongda\Supports\Collection, 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

36
                    $rocket->getDestination()->get($this->getResultKey(/** @scrutinizer ignore-type */ $rocket->getPayload()))
Loading history...
37
                )
38
            );
39
        }
40
41
        Logger::info('[alipay][LaunchPlugin] 插件装载完毕', ['rocket' => $rocket]);
42
43
        return $rocket;
44
    }
45
46
    /**
47
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
48
     * @throws \Yansongda\Pay\Exception\ContainerException
49
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
50
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
51
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
52
     */
53
    protected function verifySign(Rocket $rocket): void
54
    {
55
        $response = $rocket->getDestination();
56
        $result = $response->get($this->getResultKey($rocket->getPayload()));
0 ignored issues
show
Bug introduced by
It seems like $rocket->getPayload() can also be of type null; however, parameter $payload of Yansongda\Pay\Plugin\Ali...hPlugin::getResultKey() does only seem to accept Yansongda\Supports\Collection, 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

56
        $result = $response->get($this->getResultKey(/** @scrutinizer ignore-type */ $rocket->getPayload()));
Loading history...
57
        $sign = $response->get('sign', '');
58
59
        if ('' === $sign || is_null($result)) {
60
            throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Verify Alipay Response Sign Failed', $response);
61
        }
62
63
        verify_alipay_sign($rocket->getParams(), json_encode($result, JSON_UNESCAPED_UNICODE), base64_decode($sign));
64
    }
65
66
    protected function getResultKey(Collection $payload): string
67
    {
68
        $method = $payload->get('method');
69
70
        return str_replace('.', '_', $method).'_response';
71
    }
72
}
73