Passed
Pull Request — master (#458)
by Songda
02:12
created

CallbackPlugin::decrypt()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 22
rs 9.5222
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat;
6
7
use Closure;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yansongda\Pay\Contract\PluginInterface;
10
use Yansongda\Pay\Exception\InvalidParamsException;
11
use Yansongda\Pay\Logger;
12
use Yansongda\Pay\Parser\NoHttpRequestParser;
13
use Yansongda\Pay\Rocket;
14
use Yansongda\Supports\Collection;
15
16
class CallbackPlugin implements PluginInterface
17
{
18
    /**
19
     * @throws \Yansongda\Pay\Exception\ContainerDependencyException
20
     * @throws \Yansongda\Pay\Exception\ContainerException
21
     * @throws \Yansongda\Pay\Exception\InvalidConfigException
22
     * @throws \Yansongda\Pay\Exception\ServiceNotFoundException
23
     * @throws \Yansongda\Pay\Exception\InvalidResponseException
24
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
25
     */
26
    public function assembly(Rocket $rocket, Closure $next): Rocket
27
    {
28
        Logger::info('[wechat][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]);
29
30
        $this->assertRequestAndParams($rocket);
31
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
        $body = json_decode($rocket->getDestination()->getBody()->getContents(), true);
0 ignored issues
show
Bug introduced by
The method getBody() does not exist on Yansongda\Supports\Collection. ( Ignorable by Annotation )

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

34
        $body = json_decode($rocket->getDestination()->/** @scrutinizer ignore-call */ getBody()->getContents(), true);

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...
35
        $destination = decrypt_wechat_resource($body['resource'] ?? [], $rocket->getParams());
36
37
        $rocket->setDirection(NoHttpRequestParser::class)
38
            ->setPayload($body)
39
            ->setDestination(new Collection($destination));
40
41
        Logger::info('[wechat][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]);
42
43
        return $next($rocket);
44
    }
45
46
    /**
47
     * @throws \Yansongda\Pay\Exception\InvalidParamsException
48
     */
49
    protected function assertRequestAndParams(Rocket $rocket): void
50
    {
51
        $request = $rocket->getParams()['request'] ?? null;
52
53
        if (is_null($request) || !($request instanceof ServerRequestInterface)) {
54
            throw new InvalidParamsException(InvalidParamsException::REQUEST_NULL_ERROR);
55
        }
56
57
        $rocket->setDestinationOrigin($request);
58
        $rocket->setDestination($request);
59
        $rocket->setParams($rocket->getParams()['params'] ?? []);
60
    }
61
}
62