Completed
Push — master ( 57fe6f...b27f29 )
by wannanbigpig
03:21 queued 10s
created

ServiceContainer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 89.8%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 51
c 1
b 1
f 0
dl 0
loc 197
ccs 44
cts 49
cp 0.898
rs 10
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getProviders() 0 8 1
A apiCommonConfig() 0 15 1
A __get() 0 6 2
A registerProviders() 0 4 2
A __construct() 0 5 1
A getGateway() 0 7 2
A setAppAuthToken() 0 5 1
A getEnv() 0 3 2
A getConfig() 0 12 1
A init() 0 3 1
A setEndpointConfig() 0 5 1
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 EasyAlipay\Kernel\Contracts\App;
14
use EasyAlipay\Kernel\Exceptions\InvalidConfigException;
15
use EasyAlipay\Kernel\Providers\AppServiceProvider;
16
use EasyAlipay\Kernel\Providers\ConfigServiceProvider;
17
use EasyAlipay\Kernel\Providers\HttpClientServiceProvider;
18
use EasyAlipay\Kernel\Providers\LogServiceProvider;
19
use EasyAlipay\Kernel\Providers\RequestServiceProvider;
20
use Pimple\Container;
21
use WannanBigPig\Supports\Exceptions\RuntimeException;
22
23
/**
24
 * Class ServiceContainer
25
 *
26
 * @property \WannanBigPig\Supports\Config $config
27
 *
28
 * @author   liuml  <[email protected]>
29
 * @DateTime 2019-07-18  16:13
30
 */
31
class ServiceContainer extends Container implements App
32
{
33
    /**
34
     * @var string
35
     */
36
    const NORMAL_ENV = 'normal';
37
38
    /**
39
     * @var string
40
     */
41
    const DEV_ENV = 'dev';
42
43
    /**
44
     * @var array
45
     */
46
    protected $gateway = [
47
        self::NORMAL_ENV => 'https://openapi.alipay.com/gateway.do',
48
        self::DEV_ENV => 'https://openapi.alipaydev.com/gateway.do',
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    protected $providers = [];
55
56
    /**
57
     * @var array
58
     */
59
    protected $userConfig = [];
60
61
    /**
62
     * @var array
63
     */
64
    protected $defaultConfig = [];
65
66
    /**
67
     * Application constructor.
68
     *
69
     * @param array $config
70
     */
71 42
    public function __construct(array $config)
72
    {
73 42
        parent::__construct(['app_client_providers' => $this->providers]);
74 42
        $this->init($config);
75 42
        $this->registerProviders($this->getProviders());
76 42
    }
77
78
    /**
79
     * __get.
80
     *
81
     * @param $name
82
     *
83
     * @return mixed
84
     *
85
     * @throws \WannanBigPig\Supports\Exceptions\RuntimeException
86
     */
87 8
    public function __get($name)
88
    {
89 8
        if (isset($this[$name])) {
90 8
            return $this[$name];
91
        }
92
        throw new RuntimeException(sprintf('Identifier "%s" is not defined', $name));
93
    }
94
95
    /**
96
     * init application config.
97
     *
98
     * @param array $config
99
     */
100 42
    public function init(array $config)
101
    {
102 42
        $this->userConfig = $config;
103 42
    }
104
105
    /**
106
     * getConfig.
107
     *
108
     * @return array|mixed
109
     *
110
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidConfigException
111
     */
112 32
    public function getConfig()
113
    {
114
        $base = [
115
            'http' => [
116 32
                'timeout' => 6.0,
117 32
                'base_uri' => $this->getGateway(),
118 32
                'connect_timeout' => 6.0,
119 32
                'log_template' => "\n>>>>>>>>request\n--------\n{request}\n--------\n>>>>>>>>response\n--------\n{response}\n--------\n>>>>>>>>error\n--------\n{error}\n--------\n",
120
            ],
121
        ];
122
123 32
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
124
    }
125
126
    /**
127
     * Get the common request parameters of Alipay interface.
128
     *
129
     * @param $endpoint
130
     *
131
     * @return array
132
     */
133 8
    public function apiCommonConfig(string $endpoint): array
134
    {
135 8
        $this->config->set('api_method', $endpoint);
136
137 8
        return array_merge([
138 8
            'app_id' => $this->config['sys_params.app_id'],
139 8
            'method' => $endpoint,
140 8
            'format' => 'JSON',
141 8
            'charset' => $this->config->get('sys_params.charset', 'utf-8'),
142 8
            'sign_type' => $this->config->get('sys_params.sign_type', 'RSA2'),
143 8
            'sign' => '',
144 8
            'timestamp' => date('Y-m-d H:i:s'),
145 8
            'version' => '1.0',
146 8
            'app_auth_token' => $this->config->get('sys_params.app_auth_token', ''),
147 8
        ], $this->config->get($endpoint.'config', []));
148
    }
149
150
    /**
151
     * setEndpointConfig.
152
     *
153
     * @param string $endpoint
154
     * @param array  $config
155
     *
156
     * @return $this
157
     */
158 8
    public function setEndpointConfig(string $endpoint, array $config)
159
    {
160 8
        $this->config->set($endpoint.'config', $config);
161
162 8
        return $this;
163
    }
164
165
    /**
166
     * Acquisition of development environment.
167
     *
168
     * @return mixed|string
169
     */
170 32
    public function getEnv()
171
    {
172 32
        return isset($this->userConfig['env']) ? $this->userConfig['env'] : self::NORMAL_ENV;
173
    }
174
175
    /**
176
     * Get Alipay gateway address.
177
     *
178
     * @return mixed
179
     *
180
     * @throws \EasyAlipay\Kernel\Exceptions\InvalidConfigException
181
     */
182 32
    public function getGateway()
183
    {
184 32
        if (isset($this->gateway[$this->getEnv()])) {
185 32
            return $this->gateway[$this->getEnv()];
186
        }
187
188
        throw new InvalidConfigException('Invalid environment configuration');
189
    }
190
191
    /**
192
     * setAppAuthToken.
193
     *
194
     * @param $appAuthToken
195
     *
196
     * @return $this
197
     */
198
    public function setAppAuthToken($appAuthToken)
199
    {
200
        $this->config->set('sys_params.app_auth_token', $appAuthToken);
201
202
        return $this;
203
    }
204
205
    /**
206
     * Return all providers.
207
     *
208
     * @return array
209
     */
210 42
    public function getProviders()
211
    {
212 42
        return array_merge([
213 42
            ConfigServiceProvider::class,
214
            HttpClientServiceProvider::class,
215
            LogServiceProvider::class,
216
            RequestServiceProvider::class,
217
            AppServiceProvider::class,
218
        ]);
219
    }
220
221
    /**
222
     * @param array $providers
223
     */
224 42
    public function registerProviders(array $providers)
225
    {
226 42
        foreach ($providers as $provider) {
227 42
            parent::register(new $provider());
228
        }
229 42
    }
230
}
231