Passed
Pull Request — master (#909)
by Songda
01:57
created

AddPayloadBodyPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assembly() 0 9 1
A getBody() 0 5 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat;
6
7
use Closure;
8
use Yansongda\Pay\Contract\PluginInterface;
9
use Yansongda\Pay\Logger;
10
use Yansongda\Pay\Packer\JsonPacker;
11
use Yansongda\Pay\Rocket;
12
use Yansongda\Supports\Collection;
13
14
use function Yansongda\Pay\filter_params;
15
16
class AddPayloadBodyPlugin implements PluginInterface
17
{
18
    protected JsonPacker $jsonPacker;
19
20
    public function __construct(?JsonPacker $jsonPacker = null)
21
    {
22
        $this->jsonPacker = $jsonPacker ?? new JsonPacker();
23
    }
24
25
    public function assembly(Rocket $rocket, Closure $next): Rocket
26
    {
27
        Logger::debug('[Wechat][AddPayloadBodyPlugin] 插件开始装载', ['rocket' => $rocket]);
28
29
        $rocket->mergePayload(['_body' => $this->getBody($rocket->getPayload())]);
30
31
        Logger::info('[Wechat][AddPayloadBodyPlugin] 插件装载完毕', ['rocket' => $rocket]);
32
33
        return $next($rocket);
34
    }
35
36
    protected function getBody(?Collection $payload): string
37
    {
38
        $actualPayload = filter_params($payload->all());
0 ignored issues
show
Bug introduced by
The method all() does not exist on null. ( Ignorable by Annotation )

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

38
        $actualPayload = filter_params($payload->/** @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...
39
40
        return empty($actualPayload) ? '' : $this->jsonPacker->pack($actualPayload);
41
    }
42
}
43