Passed
Pull Request — master (#1014)
by Songda
12:36
created

Douyin   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 124
rs 10
c 1
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A callback() 0 9 1
A refund() 0 5 1
A success() 0 6 1
A getCallbackParams() 0 15 5
A close() 0 7 1
A query() 0 5 1
A mergeCommonPlugins() 0 6 1
A cancel() 0 3 1
A __call() 0 5 1
A pay() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Provider;
6
7
use GuzzleHttp\Psr7\Response;
8
use GuzzleHttp\Psr7\ServerRequest;
9
use Psr\Http\Message\MessageInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Yansongda\Artful\Artful;
13
use Yansongda\Artful\Event;
14
use Yansongda\Artful\Exception\ContainerException;
15
use Yansongda\Artful\Exception\InvalidParamsException;
16
use Yansongda\Artful\Exception\ServiceNotFoundException;
17
use Yansongda\Artful\Plugin\AddPayloadBodyPlugin;
18
use Yansongda\Artful\Plugin\AddRadarPlugin;
19
use Yansongda\Artful\Plugin\ParserPlugin;
20
use Yansongda\Artful\Plugin\StartPlugin;
21
use Yansongda\Artful\Rocket;
22
use Yansongda\Pay\Contract\ProviderInterface;
23
use Yansongda\Pay\Event\CallbackReceived;
24
use Yansongda\Pay\Event\MethodCalled;
25
use Yansongda\Pay\Exception\Exception;
26
use Yansongda\Pay\Pay;
27
use Yansongda\Pay\Plugin\Douyin\V1\Pay\AddPayloadSignaturePlugin;
28
use Yansongda\Pay\Plugin\Douyin\V1\Pay\ResponsePlugin;
29
use Yansongda\Supports\Collection;
30
use Yansongda\Supports\Str;
31
32
/**
33
 * @method Collection|Rocket mini(array $order) 小程序支付
34
 */
35
class Douyin implements ProviderInterface
36
{
37
    public const URL = [
38
        Pay::MODE_NORMAL => 'https://developer.toutiao.com/',
39
        Pay::MODE_SANDBOX => 'https://open-sandbox.douyin.com/',
40
        Pay::MODE_SERVICE => 'https://developer.toutiao.com/',
41
    ];
42
43
    /**
44
     * @throws ContainerException
45
     * @throws InvalidParamsException
46
     * @throws ServiceNotFoundException
47
     */
48
    public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket
49
    {
50
        $plugin = '\Yansongda\Pay\Shortcut\Douyin\\'.Str::studly($shortcut).'Shortcut';
51
52
        return Artful::shortcut($plugin, ...$params);
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $params of Yansongda\Artful\Artful::shortcut() does not expect variable arguments. ( Ignorable by Annotation )

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

52
        return Artful::shortcut($plugin, /** @scrutinizer ignore-type */ ...$params);
Loading history...
53
    }
54
55
    /**
56
     * @throws ContainerException
57
     * @throws InvalidParamsException
58
     */
59
    public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket
60
    {
61
        return Artful::artful($plugins, $params);
62
    }
63
64
    /**
65
     * @throws ContainerException
66
     * @throws InvalidParamsException
67
     * @throws ServiceNotFoundException
68
     */
69
    public function query(array $order): Collection|Rocket
70
    {
71
        Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null));
72
73
        return $this->__call('query', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('query', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    /**
77
     * @throws InvalidParamsException
78
     */
79
    public function cancel(array $order): Collection|Rocket
80
    {
81
        throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 微信不支持 cancel API');
82
    }
83
84
    /**
85
     * @throws ContainerException
86
     * @throws InvalidParamsException
87
     * @throws ServiceNotFoundException
88
     */
89
    public function close(array $order): Collection|Rocket
90
    {
91
        Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null));
92
93
        $this->__call('close', [$order]);
94
95
        return new Collection();
96
    }
97
98
    /**
99
     * @throws ContainerException
100
     * @throws InvalidParamsException
101
     * @throws ServiceNotFoundException
102
     */
103
    public function refund(array $order): Collection|Rocket
104
    {
105
        Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null));
106
107
        return $this->__call('refund', [$order]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->__call('refund', array($order)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
108
    }
109
110
    /**
111
     * @throws ContainerException
112
     * @throws InvalidParamsException
113
     */
114
    public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket
115
    {
116
        $request = $this->getCallbackParams($contents);
117
118
        Event::dispatch(new CallbackReceived('douyin', clone $request, $params, null));
119
120
        return $this->pay(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pay(array(... '_params' => $params)) could return the type Psr\Http\Message\MessageInterface|null which is incompatible with the type-hinted return Yansongda\Artful\Rocket|...gda\Supports\Collection. Consider adding an additional type-check to rule them out.
Loading history...
121
            [CallbackPlugin::class],
0 ignored issues
show
Bug introduced by
The type Yansongda\Pay\Provider\CallbackPlugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
122
            ['_request' => $request, '_params' => $params]
123
        );
124
    }
125
126
    public function success(): ResponseInterface
127
    {
128
        return new Response(
129
            200,
130
            ['Content-Type' => 'application/json'],
131
            json_encode(['code' => 'SUCCESS', 'message' => '成功']),
132
        );
133
    }
134
135
    public function mergeCommonPlugins(array $plugins): array
136
    {
137
        return array_merge(
138
            [StartPlugin::class],
139
            $plugins,
140
            [AddPayloadSignaturePlugin::class, AddPayloadBodyPlugin::class, AddRadarPlugin::class, ResponsePlugin::class, ParserPlugin::class],
141
        );
142
    }
143
144
    protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): ServerRequestInterface
145
    {
146
        if (is_array($contents) && isset($contents['body'], $contents['headers'])) {
147
            return new ServerRequest('POST', 'http://localhost', $contents['headers'], $contents['body']);
148
        }
149
150
        if (is_array($contents)) {
151
            return new ServerRequest('POST', 'http://localhost', [], json_encode($contents));
152
        }
153
154
        if ($contents instanceof ServerRequestInterface) {
155
            return $contents;
156
        }
157
158
        return ServerRequest::fromGlobals();
159
    }
160
}
161