Completed
Push — master ( 8787ba...721c87 )
by wannanbigpig
05:01 queued 11s
created

ServiceContainer::apiCommonConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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