Passed
Pull Request — master (#894)
by Songda
01:58
created

VerifySignaturePlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 25 4
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\ContainerException;
10
use Yansongda\Pay\Exception\Exception;
11
use Yansongda\Pay\Exception\InvalidConfigException;
12
use Yansongda\Pay\Exception\InvalidResponseException;
13
use Yansongda\Pay\Exception\ServiceNotFoundException;
14
use Yansongda\Pay\Logger;
15
use Yansongda\Pay\Rocket;
16
17
use function Yansongda\Pay\should_do_http_request;
18
use function Yansongda\Pay\verify_alipay_sign;
19
20
class VerifySignaturePlugin implements PluginInterface
21
{
22
    /**
23
     * @throws ContainerException
24
     * @throws InvalidConfigException
25
     * @throws InvalidResponseException
26
     * @throws ServiceNotFoundException
27
     */
28
    public function assembly(Rocket $rocket, Closure $next): Rocket
29
    {
30
        /* @var Rocket $rocket */
31
        $rocket = $next($rocket);
32
33
        Logger::debug('[Alipay][VerifySignaturePlugin] 插件开始装载', ['rocket' => $rocket]);
34
35
        if (!should_do_http_request($rocket->getDirection())) {
36
            return $rocket;
37
        }
38
39
        $destination = $rocket->getDestination();
40
41
        $sign = $destination->get('_sign', '');
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

41
        /** @scrutinizer ignore-call */ 
42
        $sign = $destination->get('_sign', '');
Loading history...
42
        $result = $destination->except('_sign')->all();
0 ignored issues
show
Bug introduced by
The method all() 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

42
        $result = $destination->except('_sign')->/** @scrutinizer ignore-call */ all();

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...
Bug introduced by
The method except() 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

42
        $result = $destination->/** @scrutinizer ignore-call */ except('_sign')->all();

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...
43
44
        if ('' === $sign || empty($result)) {
45
            throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, 'Verify Alipay Response Sign Failed: sign is empty', $destination);
46
        }
47
48
        verify_alipay_sign($rocket->getParams(), json_encode($result, JSON_UNESCAPED_UNICODE), $sign);
49
50
        Logger::info('[Alipay][VerifySignaturePlugin] 插件装载完毕', ['rocket' => $rocket]);
51
52
        return $rocket;
53
    }
54
}
55