CreatePlugin::assembly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 16
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Plugin\Wechat\V3\Marketing\Fapiao\Blockchain;
6
7
use Closure;
8
use Yansongda\Artful\Contract\PluginInterface;
9
use Yansongda\Artful\Exception\ContainerException;
10
use Yansongda\Artful\Exception\InvalidConfigException;
11
use Yansongda\Artful\Exception\InvalidParamsException;
12
use Yansongda\Artful\Exception\ServiceNotFoundException;
13
use Yansongda\Artful\Logger;
14
use Yansongda\Artful\Rocket;
15
use Yansongda\Pay\Exception\DecryptException;
16
use Yansongda\Supports\Collection;
17
18
use function Yansongda\Pay\encrypt_wechat_contents;
19
use function Yansongda\Pay\get_provider_config;
20
use function Yansongda\Pay\get_wechat_public_key;
21
use function Yansongda\Pay\get_wechat_serial_no;
22
23
/**
24
 * @see https://pay.weixin.qq.com/docs/merchant/apis/fapiao/fapiao-applications/issue-fapiao-applications.html
25
 */
26
class CreatePlugin implements PluginInterface
27
{
28
    /**
29
     * @throws ContainerException
30
     * @throws DecryptException
31
     * @throws InvalidConfigException
32
     * @throws InvalidParamsException
33
     * @throws ServiceNotFoundException
34
     */
35
    public function assembly(Rocket $rocket, Closure $next): Rocket
36
    {
37
        Logger::debug('[Wechat][V3][Marketing][Fapiao][Blockchain][CreatePlugin] 插件开始装载', ['rocket' => $rocket]);
38
39
        $params = $rocket->getParams();
40
        $payload = $rocket->getPayload();
41
        $config = get_provider_config('wechat', $params);
42
43
        $rocket->mergePayload(array_merge([
44
            '_method' => 'POST',
45
            '_url' => 'v3/new-tax-control-fapiao/fapiao-applications',
46
        ], $this->encryptSensitiveData($payload, $params, $config)));
47
48
        Logger::info('[Wechat][V3][Marketing][Fapiao][Blockchain][CreatePlugin] 插件装载完毕', ['rocket' => $rocket]);
49
50
        return $next($rocket);
51
    }
52
53
    /**
54
     * @throws ContainerException
55
     * @throws InvalidConfigException
56
     * @throws InvalidParamsException
57
     * @throws ServiceNotFoundException
58
     * @throws DecryptException
59
     */
60
    protected function encryptSensitiveData(?Collection $payload, array $params, array $config): array
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

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

60
    protected function encryptSensitiveData(?Collection $payload, array $params, /** @scrutinizer ignore-unused */ array $config): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        $data['_serial_no'] = get_wechat_serial_no($params);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
63
64
        $config = get_provider_config('wechat', $params);
65
        $publicKey = get_wechat_public_key($config, $data['_serial_no']);
66
67
        $phone = $payload?->get('buyer_information.phone') ?? null;
68
        $email = $payload?->get('buyer_information.email') ?? null;
69
70
        if (!is_null($phone)) {
71
            $data['buyer_information']['phone'] = encrypt_wechat_contents($phone, $publicKey);
72
        }
73
74
        if (!is_null($email)) {
75
            $data['buyer_information']['email'] = encrypt_wechat_contents($email, $publicKey);
76
        }
77
78
        return $data;
79
    }
80
}
81