Completed
Push — master ( b8ee44...86e435 )
by Jim
02:27
created

TimCloud::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/14/2018
6
 * Time: 9:01 PM
7
 */
8
9
namespace TimSDK;
10
11
use TimSDK\Support\Arr;
12
use TimSDK\Support\Str;
13
use TimSDK\Foundation\ResponseBag;
14
use TimSDK\Container\ServiceContainer;
15
16
/**
17
 * Class TimCloud
18
 * @package TimSDK
19
 * @property \TimSDK\Core\IMCloud $im
20
 * @property \TimSDK\Core\ApiAlias $apiAlias
21
 */
22
class TimCloud extends ServiceContainer
23
{
24
    /**
25
     * TimCloud version
26
     *
27
     * @var string
28
     */
29
    const VERSION = '0.1.2';
30
31
    protected $providers = [
32
        \TimSDK\Core\ServiceProviders\ApiAliasServiceProvider::class,
33
        \TimSDK\Core\ServiceProviders\IMCloudServiceProvider::class,
34
    ];
35
36
    public function __construct(array $config = [], array $prepends = [])
37
    {
38
        if (Arr::has($config, ['private_key', 'public_key'])) {
39
            $config['private_key'] = $this->formatKey($config['private_key'], 'private');
40
            $config['public_key'] = $this->formatKey($config['public_key'], 'public');
41
        }
42
43
        parent::__construct($config, $prepends);
44
    }
45
46
    public function setAppId($appid)
47
    {
48
        $this->setConfig('app_id', $appid);
49
50
        return $this;
51
    }
52
53
    public function setIdentifier($identifier)
54
    {
55
        if (!empty($identifier)) {
56
            $this->setConfig('identifier', $identifier);
57
        }
58
59
        return $this;
60
    }
61
62
    public function setPrivateKey($prikey)
63
    {
64
        if (is_file($prikey)) {
65
            $prikey = file_get_contents($prikey);
66
        } else {
67
            $prikey = $this->formatKey($prikey, 'private');
68
        }
69
70
        if (!empty($prikey)) {
71
            $this->setConfig('private_key', $prikey);
72
        }
73
74
        return $this;
75
    }
76
77
    public function setPublicKey($pubkey)
78
    {
79
        if (is_file($pubkey)) {
80
            $pubkey = file_get_contents($pubkey);
81
        } else {
82
            $pubkey = $this->formatKey($pubkey, 'public');
83
        }
84
85
        $this->setConfig('public_key', $pubkey);
86
87
        return $this;
88
    }
89
90
    public function formatKey($key, $keyType)
91
    {
92
        $keyType = strtoupper($keyType);
93
94
        if (Str::startsWith($key, "-----BEGIN $keyType KEY-----")) {
95
            return $key;
96
        }
97
98
        return "-----BEGIN $keyType KEY-----" . PHP_EOL .
99
            wordwrap(str_replace(["\r", "\n"], '', $key), 64, PHP_EOL, true) .
100
            PHP_EOL . "-----END $keyType KEY-----";
101
    }
102
103
    /**
104
     * Send a request
105
     *
106
     * @param string $uri
107
     * @param string $body
108
     * @param array  $options
109
     * @return ResponseBag
110
     */
111
    public function request($uri, $body = '', $options = [])
112
    {
113
        return $this['im']->handle($this['apiAlias'][$uri], $body, $options);
114
    }
115
116
    /**
117
     * Get the version number of the application.
118
     *
119
     * @return string
120
     */
121
    public function version()
122
    {
123
        return static::VERSION;
124
    }
125
126
    /**
127
     * Call request method
128
     *
129
     * @param $method
130
     * @param $arguments
131
     * @return mixed
132
     */
133
    public function __call($method, $arguments)
134
    {
135
        $method = ltrim($method, 'request');
136
        $uri = Str::upper(Str::snake($method));
137
        array_unshift($arguments, $uri);
138
139
        return call_user_func_array([$this, 'request'], $arguments);
140
    }
141
142
    protected function setConfig($key, $value)
143
    {
144
        if (!empty($value)) {
145
            $oldVal = $this['config']->get($key, null);
146
            if ($value !== $oldVal) {
147
                $this['config']->set($key, $value);
148
                $this['im']->needRefresh();
149
            }
150
        }
151
    }
152
}
153