Passed
Push — master ( 27eb3f...fa0f8d )
by du
01:52
created

IpClient::setProviderConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace DucCnzj\Ip;
4
5
use DucCnzj\Ip\Imp\IpImp;
6
use DucCnzj\Ip\Imp\DataMapImp;
7
use DucCnzj\Ip\Imp\CacheStoreImp;
8
use DucCnzj\Ip\Imp\RequestHandlerImp;
9
use DucCnzj\Ip\Exceptions\InvalidIpAddress;
10
use DucCnzj\Ip\Exceptions\ServerErrorException;
11
use DucCnzj\Ip\Exceptions\IpProviderClassNotExistException;
12
13
/**
14
 * @method string getCity()
15
 * @method string getCountry()
16
 * @method string getAddress()
17
 * @method string getRegion()
18
 *
19
 * Class IpClient
20
 *
21
 * @package DucCnzj\Ip
22
 */
23
class IpClient
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $ip;
29
30
    /**
31
     * @var array
32
     */
33
    protected $providerConfig = [];
34
35
    /**
36
     * @var array|null ['baidu', 'taobao']
37
     */
38
    protected $providers = [];
39
40
    /**
41
     * @var array
42
     */
43
    protected $instances = [];
44
45
    /**
46
     * @var DataMapImp
47
     */
48
    protected $dataMapper;
49
50
    /**
51
     * @var RequestHandlerImp|null
52
     */
53
    protected $requestHandler;
54
55
    /**
56
     * @param int $times
57
     *
58
     * @return $this
59
     *
60
     * @author duc <[email protected]>
61
     */
62 5
    public function try(int $times)
63
    {
64 5
        $this->requestHandler = $this->getRequestHandler()->setTryTimes($times);
65
66 5
        return $this;
67
    }
68
69
    /**
70
     * @param string $ip
71
     *
72
     * @throws InvalidIpAddress
73
     *
74
     * @author duc <[email protected]>
75
     */
76 15
    public function checkIp(string $ip)
77
    {
78 15
        $b = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip);
79
80 15
        if (! $b) {
81 1
            throw new InvalidIpAddress;
82
        }
83 14
    }
84
85
    /**
86
     * @return array
87
     *
88
     * @author duc <[email protected]>
89
     */
90 12
    public function resolveProviders()
91
    {
92 12
        $providerInstance = [];
93
94 12
        foreach ($this->getProviders() as $provider) {
95 12
            if (! isset($this->instances[$provider])) {
96 5
                $this->instances[$provider] = $this->createProvider($provider);
97
            }
98
99 11
            $providerInstance[$provider] = $this->instances[$provider];
100
        }
101
102 11
        return $providerInstance;
103
    }
104
105
    /**
106
     * @param string $provider
107
     *
108
     * @return IpImp
109
     * @throws IpProviderClassNotExistException
110
     *
111
     * @author duc <[email protected]>
112
     */
113 5
    public function createProvider(string $provider)
114
    {
115 5
        $config = $this->getProviderConfig($provider);
116
117 5
        $shortName = ucfirst(strtolower($provider)) . 'Ip';
118
119 5
        $class = __NAMESPACE__ . "\Strategies\\{$shortName}";
120
121 5
        if (! class_exists($class)) {
122 1
            throw new IpProviderClassNotExistException("{$class} 不存在");
123
        }
124
125 4
        return new $class($config);
126
    }
127
128
    /**
129
     * @param string $msg
130
     *
131
     * @return array
132
     *
133
     * @author duc <[email protected]>
134
     */
135 5
    public function responseWithError(string $msg)
136
    {
137
        return [
138 5
            'success' => 0,
139 5
            'message' => $msg,
140
        ];
141
    }
142
143
    /**
144
     * @return DataMapper|DataMapImp|NullDataMapper
145
     *
146
     * @author duc <[email protected]>
147
     */
148 7
    public function getDataMapper()
149
    {
150 7
        $response = $this->getOriginalInfo();
151
152 7
        if (! $response['success']) {
153 4
            return (new NullDataMapper())->setInfo(['ip' => $this->getIp()]);
154
        }
155
156 3
        if (! $this->dataMapper) {
157 3
            $this->dataMapper = new DataMapper();
158
159 3
            return $this->dataMapper->setInfo($response);
160
        }
161
162 1
        return $this->dataMapper->setInfo($response);
163
    }
164
165
    /**
166
     * @return RequestHandlerImp|RequestHandler|null
167
     *
168
     * @author duc <[email protected]>
169
     */
170 11
    public function getRequestHandler()
171
    {
172 11
        if (! $this->requestHandler) {
173 8
            return $this->requestHandler = new RequestHandler();
174
        }
175
176 7
        return $this->requestHandler;
177
    }
178
179
    /**
180
     * @return array|mixed
181
     *
182
     * @author duc <[email protected]>
183
     */
184 9
    public function getOriginalInfo()
185
    {
186
        try {
187 9
            $result = $this->getRequestHandler()
188 9
                ->send($this->resolveProviders(), $this->getIp());
189 5
        } catch (ServerErrorException $e) {
190 5
            return $this->responseWithError($e->getMessage());
191
        }
192
193 5
        return $result;
194
    }
195
196
    /**
197
     * @return array
198
     *
199
     * @author duc <[email protected]>
200
     */
201 3
    public function getErrors(): array
202
    {
203 3
        return $this->getRequestHandler()->getErrors();
204
    }
205
206
    /**
207
     * @return array
208
     *
209
     * @author duc <[email protected]>
210
     */
211 15
    public function getProviders()
212
    {
213 15
        if (is_null($this->providers)) {
214 1
            return [];
215
        }
216
217 15
        if (count($this->providers) === 0) {
218 5
            return $this->providers = $this->getDefaultProviders();
219
        }
220
221 11
        return $this->providers;
222
    }
223
224
    /**
225
     * @return string
226
     * @throws \Exception
227
     *
228
     * @author duc <[email protected]>
229
     */
230 15
    public function getIp()
231
    {
232 15
        if (! $this->ip) {
233 1
            throw new \Exception('请先设置 ip');
234
        }
235
236 14
        return $this->ip;
237
    }
238
239
    /**
240
     * @return array
241
     *
242
     * @author duc <[email protected]>
243
     */
244 5
    public function getDefaultProviders()
245
    {
246
        return [
247 5
            'baidu',
248
            'ali',
249
            'tencent',
250
            'taobao',
251
        ];
252
    }
253
254
    /**
255
     * @param string $provider
256
     *
257
     * @return array|string
258
     */
259 7
    public function getProviderConfig(string $provider)
260
    {
261 7
        if (! isset($this->providerConfig[$provider])) {
262 7
            return [];
263
        }
264
265 2
        return $this->providerConfig[$provider];
266
    }
267
268
    /**
269
     * @return CacheStore|CacheStoreImp
270
     *
271
     * @author duc <[email protected]>
272
     */
273 8
    public function getCacheStore()
274
    {
275 8
        return $this->getRequestHandler()->getCacheStore();
276
    }
277
278
    /**
279
     * @param string[] ...$names
280
     *
281
     * @return array
282
     *
283
     * @author duc <[email protected]>
284
     */
285 1
    public function getConfigs(string ...$names)
286
    {
287 1
        if (empty($names)) {
288 1
            return $this->providerConfig;
289
        }
290
291 1
        $result = [];
292 1
        foreach ($names as $provider) {
293 1
            $result[$provider] = $this->getProviderConfig($provider);
294
        }
295
296 1
        return $result;
297
    }
298
299
    /**
300
     * @param string $ip
301
     *
302
     * @return $this
303
     *
304
     * @author duc <[email protected]>
305
     */
306 14
    public function setIp(string $ip)
307
    {
308 14
        $this->checkIp($ip);
309
310 14
        $this->ip = $ip;
311
312 14
        return $this;
313
    }
314
315
    /**
316
     * @param string[] ...$provider
317
     *
318
     * @return $this
319
     *
320
     * @author duc <[email protected]>
321
     */
322 11
    public function useProvider(string ...$provider)
323
    {
324 11
        $providers = array_merge($this->providers ?? [], array_filter($provider));
325 11
        $this->providers = array_unique($providers);
326
327 11
        return $this;
328
    }
329
330
    /**
331
     * @param string $provider
332
     * @param array|string $config
333
     *
334
     * @return $this
335
     *
336
     * @author duc <[email protected]>
337
     */
338 3
    public function setProviderConfig(string $provider, $config)
339
    {
340 3
        $this->providerConfig[$provider] = $config;
341
342 3
        return $this;
343
    }
344
345
    /**
346
     * @param array $configs
347
     *
348
     * @return $this
349
     *
350
     * @author duc <[email protected]>
351
     */
352 1
    public function setConfigs(array $configs)
353
    {
354 1
        foreach ($configs as $provider => $config) {
355 1
            $this->setProviderConfig($provider, $config);
356
        }
357
358 1
        return $this;
359
    }
360
361
    /**
362
     * @param DataMapImp $dataMapper
363
     *
364
     * @return $this
365
     *
366
     * @author duc <[email protected]>
367
     */
368 1
    public function setDataMapper(DataMapImp $dataMapper)
369
    {
370 1
        $this->dataMapper = $dataMapper;
371
372 1
        return $this;
373
    }
374
375
    /**
376
     * @param RequestHandlerImp $requestHandler
377
     *
378
     * @return $this
379
     *
380
     * @author duc <[email protected]>
381
     */
382 3
    public function setRequestHandler(RequestHandlerImp $requestHandler)
383
    {
384 3
        $this->requestHandler = $requestHandler;
385
386 3
        return $this;
387
    }
388
389
    /**
390
     * @param CacheStoreImp $cacheStore
391
     *
392
     * @return $this
393
     *
394
     * @author duc <[email protected]>
395
     */
396 2
    public function setCacheStore(CacheStoreImp $cacheStore)
397
    {
398 2
        $this->requestHandler = $this->getRequestHandler()->setCacheStore($cacheStore);
399
400 2
        return $this;
401
    }
402
403
    /**
404
     * @param string $provider
405
     * @param IpImp  $instance
406
     *
407
     * @return $this
408
     *
409
     * @author duc <[email protected]>
410
     */
411 9
    public function bound(string $provider, IpImp $instance)
412
    {
413 9
        $this->instances[$provider] = $instance;
414
415 9
        return $this;
416
    }
417
418
    /**
419
     * @param string[] ...$provider
420
     *
421
     * @return IpClient
422
     *
423
     * @author duc <[email protected]>
424
     */
425 5
    public function use(string ...$provider)
426
    {
427 5
        return $this->useProvider(...$provider);
428
    }
429
430
    /**
431
     * @return $this
432
     *
433
     * @author duc <[email protected]>
434
     */
435 2
    public function clearUse()
436
    {
437 2
        $this->providers = null;
438
439 2
        return $this;
440
    }
441
442
    /**
443
     * @param string $name
444
     *
445
     * @return mixed|null
446
     *
447
     * @author duc <[email protected]>
448
     */
449 1
    public function getInstanceByName(string $name)
450
    {
451 1
        return isset($this->instances[$name]) ? $this->instances[$name] : null;
452
    }
453
454
    /**
455
     * @param string $name
456
     * @param $arguments
457
     *
458
     * @return array
459
     *
460
     * @author duc <[email protected]>
461
     */
462 1
    public function __call(string $name, $arguments)
463
    {
464 1
        return $this->getDataMapper()->{$name}(...$arguments);
465
    }
466
467
    /**
468
     * @param string $name
469
     *
470
     * @return mixed|string
471
     *
472
     * @author duc <[email protected]>
473
     */
474
    public function __get(string $name)
475
    {
476
        return $this->getDataMapper()->{$name};
477
    }
478
}
479