Completed
Push — master ( 56c44b...57c31b )
by Songda
05:33 queued 11s
created

LaunchPlugin::verifySign()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Alipay;
6
7
use Closure;
8
use Psr\Http\Message\ResponseInterface;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\InvalidConfigException;
11
use Yansongda\Pay\Exception\InvalidResponseException;
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
        if ($rocket->getDestination() instanceof ResponseInterface) {
30
            return $rocket;
31
        }
32
33
        $rocket->setDestination($this->getMethodResponse($rocket));
34
35
        return $rocket;
36
    }
37
38
    /**
39
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
40
     * @throws \Yansongda\Pay\Exception\ContainerException
41
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
42
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
43
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
44
     */
45
    protected function getMethodResponse(Rocket $rocket): Collection
46
    {
47
        $response = Collection::wrap(
48
            $rocket->getDestination()->get($this->getResponseKey($rocket))
0 ignored issues
show
Bug introduced by
The method get does only exist in Yansongda\Supports\Collection, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
        );
50
51
        $this->verifySign($rocket);
52
53
        if (10000 != $response->get('code', 10000)) {
54
            throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_CODE, 'Invalid response code', $response->all());
55
        }
56
57
        return $response;
58
    }
59
60
    /**
61
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
62
     * @throws \Yansongda\Pay\Exception\ContainerException
63
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
64
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
65
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
66
     */
67
    protected function verifySign(Rocket $rocket): void
68
    {
69
        $response = $rocket->getDestination()->get($this->getResponseKey($rocket));
0 ignored issues
show
Bug introduced by
The method get does only exist in Yansongda\Supports\Collection, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
        $sign = $rocket->getDestination()->get('sign', '');
71
72
        if ('' === $sign || is_null($response)) {
73
            throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN);
74
        }
75
76
        $result = openssl_verify(json_encode($response, JSON_UNESCAPED_UNICODE), base64_decode($sign), $this->getAlipayPublicKey($rocket), OPENSSL_ALGO_SHA256);
77
78
        if (1 !== $result) {
79
            throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN);
80
        }
81
    }
82
83
    protected function getResponseKey(Rocket $rocket): string
84
    {
85
        $method = $rocket->getPayload()->get('method');
86
87
        return str_replace('.', '_', $method).'_response';
88
    }
89
90
    /**
91
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
92
     * @throws \Yansongda\Pay\Exception\ContainerException
93
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
94
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
95
     */
96 View Code Duplication
    protected function getAlipayPublicKey(Rocket $rocket)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $public = get_alipay_config($rocket->getParams())->get('alipay_public_cert_path');
99
100
        if (is_null($public)) {
101
            throw new InvalidConfigException(InvalidConfigException::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [alipay_public_cert_path]');
102
        }
103
104
        return get_alipay_cert($public);
105
    }
106
}
107