ApiStore   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 4
c 5
b 0
f 2
lcom 0
cbo 0
dl 0
loc 65
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 9 2
A __call() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the light/apistore.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace light\apistore;
13
14
/**
15
 * 调用总线
16
 * ~~~
17
 * $store = new ApiStore();
18
 * $result = $store->phone->get('123123123');
19
 * ~~~.
20
 */
21
final class ApiStore
22
{
23
    /**
24
     * The app key of calling the interface.
25
     *
26
     * @var string
27
     */
28
    public $apikey;
29
30
    public static $supportedApis = [
31
        'ip' => 'light\apistore\apis\Ip',
32
        'phone' => 'light\apistore\apis\Phone',
33
        'translate' => 'light\apistore\apis\Translate',
34
        'idcard' => 'light\apistore\apis\IdCard',
35
        'currency' => 'light\apistore\apis\Currency',
36
        'pullword' => 'light\apistore\apis\PullWord',
37
        'distance' => 'light\apistore\apis\Distance',
38
    ];
39
40
    /**
41
     * Init.
42
     *
43
     * @param string $apikey 调用接口的API key
44
     */
45 18
    public function __construct($apikey)
46
    {
47 18
        $this->apikey = $apikey;
48 18
    }
49
    /**
50
     * Implement the __get magic method.
51
     *
52
     * @param string $name The property name or the api name
53
     *
54
     * @throws InvalidaCallException
55
     * @throws UnknownPropertyException
56
     *
57
     * @return mixed
58
     */
59 18
    public function __get($name)
60
    {
61 18
        if (isset(static::$supportedApis[$name])) {
62 17
            $class = static::$supportedApis[$name];
63
64 17
            return new $class($this->apikey);
65
        }
66 1
        throw new \Exception('Not exists method');
67
    }
68
69
    /**
70
     * Invoke methods.
71
     *
72
     * @param string $name   the name of method try to invoke
73
     * @param array  $params the params passed to the method
74
     *
75
     * @throws InvalidCallException
76
     *
77
     * @return mixed the method result of calling
78
     */
79 1
    public function __call($name, $params)
80
    {
81 1
        $obj = $this->{$name};
82
83 1
        return call_user_func_array([$obj, 'get'], $params);
84
    }
85
}
86