Test Failed
Branch master (e5b474)
by wannanbigpig
02:49
created

ServiceContainer::setReturnUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the wannanbigpig/alipay-sdk-php.
4
 *
5
 * (c) wannanbigpig <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace EasyAlipay\Kernel;
12
13
use Closure;
14
use EasyAlipay\Kernel\Contracts\App;
15
use EasyAlipay\Kernel\Notify\Handle;
16
use EasyAlipay\Kernel\Providers\AppServiceProvider;
17
use EasyAlipay\Kernel\Providers\ConfigServiceProvider;
18
use EasyAlipay\Kernel\Providers\HttpClientServiceProvider;
19
use EasyAlipay\Kernel\Providers\LogServiceProvider;
20
use EasyAlipay\Kernel\Providers\RequestServiceProvider;
21
use Pimple\Container;
22
use WannanBigPig\Supports\Exceptions\RuntimeException;
23
24
/**
25
 * Class ServiceContainer
26
 *
27
 * @property \WannanBigPig\Supports\Config             $config
28
 * @property \Symfony\Component\HttpFoundation\Request $request
29
 * @property \GuzzleHttp\Client                        $http_client
30
 * @property \Monolog\Logger                           $logger
31
 *
32
 * @author   liuml  <[email protected]>
33
 * @DateTime 2019-07-18  16:13
34
 */
35
class ServiceContainer extends Container implements App
36
{
37
    /**
38
     * @var string
39
     */
40
    const NORMAL_ENV = 'normal';
41
42
    /**
43
     * @var string
44
     */
45
    const DEV_ENV = 'dev';
46
47
    /**
48
     * @var array
49
     */
50
    protected $gateway = [
51
        self::NORMAL_ENV => 'https://openapi.alipay.com/gateway.do',
52
        self::DEV_ENV => 'https://openapi.alipaydev.com/gateway.do',
53
    ];
54
55
    /**
56
     * @var array
57
     */
58
    protected $providers = [];
59
60
    /**
61
     * @var array
62
     */
63
    protected $userConfig = [];
64
65
    /**
66
     * @var array
67
     */
68
    protected $defaultConfig = [];
69
70
    /**
71
     * Application constructor.
72
     *
73
     * @param array $config
74
     */
75 78
    public function __construct(array $config = [])
76
    {
77 78
        parent::__construct(['app_client_providers' => $this->providers]);
78 78
        $this->init($config);
79 78
        $this->registerProviders($this->getProviders());
80 78
    }
81
82
    /**
83
     * __get.
84
     *
85
     * @param $name
86
     *
87
     * @return mixed
88
     *
89
     * @throws \WannanBigPig\Supports\Exceptions\RuntimeException
90
     */
91 40
    public function __get($name)
92
    {
93 40
        if (isset($this[$name])) {
94 38
            return $this[$name];
95
        }
96 2
        throw new RuntimeException(sprintf('Identifier "%s" is not defined', $name));
97
    }
98
99
    /**
100
     * init application config.
101
     *
102
     * @param array $config
103
     */
104 78
    public function init(array $config)
105
    {
106 78
        $this->userConfig = $config;
107 78
    }
108
109
    /**
110
     * getConfig.
111
     *
112
     * @return array|mixed
113
     */
114 76
    public function getConfig()
115
    {
116
        $base = [
117
            'http' => [
118 76
                'timeout' => 6.0,
119 76
                'base_uri' => $this->getGateway(),
120 76
                'connect_timeout' => 6.0,
121 76
                'log_template' => "\n>>>>>>>>request\n--------\n{request}\n--------\n>>>>>>>>response\n--------\n{response}\n--------\n>>>>>>>>error\n--------\n{error}\n--------\n",
122
            ],
123
        ];
124
125 76
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
126
    }
127
128
    /**
129
     * Get the common request parameters of Alipay interface.
130
     *
131
     * @param $endpoint
132
     *
133
     * @return array
134
     */
135 16
    public function apiCommonConfig(string $endpoint): array
136
    {
137 16
        $this->config->set('api_method', $endpoint);
138
139 16
        return array_merge([
140 16
            'app_id' => $this->config['sys_params.app_id'],
141 16
            'method' => $endpoint,
142 16
            'format' => 'JSON',
143 16
            'charset' => $this->config->get('sys_params.charset', 'utf-8'),
144 16
            'sign_type' => $this->config->get('sys_params.sign_type', 'RSA2'),
145 16
            'sign' => '',
146 16
            'timestamp' => date('Y-m-d H:i:s'),
147 16
            'version' => '1.0',
148 16
            'app_auth_token' => $this->config->get('sys_params.app_auth_token', ''),
149 16
        ], $this->config->get($endpoint.'config', []));
150
    }
151
152
    /**
153
     * setEndpointConfig.
154
     *
155
     * @param string $endpoint
156
     * @param array  $config
157
     *
158
     * @return $this
159
     */
160 18
    public function setEndpointConfig(string $endpoint, array $config)
161
    {
162 18
        $this->config->set($endpoint.'config', $config);
163
164 18
        return $this;
165
    }
166
167
    /**
168
     * Acquisition of development environment.
169
     *
170
     * @return bool
171
     */
172 76
    public function getEnv(): bool
173
    {
174 76
        return (bool)(isset($this->userConfig['sandbox']) ? $this->userConfig['sandbox'] : false);
175
    }
176
177
    /**
178
     * Get Alipay gateway address.
179
     *
180
     * @return mixed
181
     */
182 76
    public function getGateway()
183
    {
184 76
        if ($this->getEnv()) {
185 2
            return $this->gateway[self::DEV_ENV];
186
        } else {
187 76
            return $this->gateway[self::NORMAL_ENV];
188
        }
189
    }
190
191
    /**
192
     * setAppAuthToken.
193
     *
194
     * @param $appAuthToken
195
     *
196
     * @return $this
197
     */
198 2
    public function setAppAuthToken($appAuthToken)
199
    {
200 2
        $this->config->set('sys_params.app_auth_token', $appAuthToken);
201
202 2
        return $this;
203
    }
204
205
    /**
206
     * setNotifyUrl.
207
     *
208
     * @param string $notifyUrl
209
     *
210 78
     * @return $this
211
     */
212 78
    public function setNotifyUrl(string $notifyUrl)
213 78
    {
214
        $this->config->set('notify_url', $notifyUrl);
215
216
        return $this;
217
    }
218
219
    /**
220
     * setReturnUrl.
221
     *
222
     * @param string $returnUrl
223
     *
224
     * @return $this
225
     */
226
    public function setReturnUrl(string $returnUrl)
227
    {
228 10
        $this->config->set('return_url', $returnUrl);
229
230 10
        return $this;
231
    }
232
233
    /**
234
     * Return all providers.
235
     *
236 78
     * @return array
237
     */
238 78
    public function getProviders()
239 78
    {
240
        return array_merge([
241 78
            ConfigServiceProvider::class,
242
            HttpClientServiceProvider::class,
243
            LogServiceProvider::class,
244
            RequestServiceProvider::class,
245
            AppServiceProvider::class,
246
        ]);
247
    }
248
249
    /**
250
     * handleNotify.
251
     *
252
     * @param \Closure $closure
253
     *
254
     * @return \Symfony\Component\HttpFoundation\Response
255
     */
256
    public function handleNotify(Closure $closure)
257
    {
258
        return (new Handle($this))->run($closure);
259
    }
260
261
    /**
262
     * @param array $providers
263
     */
264
    public function registerProviders(array $providers)
265
    {
266
        foreach ($providers as $provider) {
267
            parent::register(new $provider());
268
        }
269
    }
270
}
271