TimCloud::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
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
 *
19
 * @package TimSDK
20
 * @property \TimSDK\Core\IMCloud $im
21
 * @property \TimSDK\Core\ApiAlias $apiAlias
22
 * @method \TimSDK\Foundation\ResponseBag requestAccountImport($body = '', $option = [])
23
 * @method \TimSDK\Foundation\ResponseBag requestMutiAccountImport($body = '', $option = [])
24
 * @method \TimSDK\Foundation\ResponseBag requestRegisterAccountV1($body = '', $option = [])
25
 * @method \TimSDK\Foundation\ResponseBag requestKick($body = '', $option = [])
26
 * @method \TimSDK\Foundation\ResponseBag requestSendMsg($body = '', $option = [])
27
 * @method \TimSDK\Foundation\ResponseBag requestBatchSendMsg($body = '', $option = [])
28
 * @method \TimSDK\Foundation\ResponseBag requestImportMsg($body = '', $option = [])
29
 * @method \TimSDK\Foundation\ResponseBag requestImPush($body = '', $option = [])
30
 * @method \TimSDK\Foundation\ResponseBag requestImGetPushReport($body = '', $option = [])
31
 * @method \TimSDK\Foundation\ResponseBag requestImSetAttrName($body = '', $option = [])
32
 * @method \TimSDK\Foundation\ResponseBag requestImGetAttrName($body = '', $option = [])
33
 * @method \TimSDK\Foundation\ResponseBag requestImSetAttr($body = '', $option = [])
34
 * @method \TimSDK\Foundation\ResponseBag requestImRemoveAttr($body = '', $option = [])
35
 * @method \TimSDK\Foundation\ResponseBag requestImGetAttr($body = '', $option = [])
36
 * @method \TimSDK\Foundation\ResponseBag requestImAddTag($body = '', $option = [])
37
 * @method \TimSDK\Foundation\ResponseBag requestImRemoveTag($body = '', $option = [])
38
 * @method \TimSDK\Foundation\ResponseBag requestImRemoveAllTags($body = '', $option = [])
39
 * @method \TimSDK\Foundation\ResponseBag requestGetAppidGroupList($body = '', $option = [])
40
 * @method \TimSDK\Foundation\ResponseBag requestCreateGroup($body = '', $option = [])
41
 * @method \TimSDK\Foundation\ResponseBag requestGetGroupInfo($body = '', $option = [])
42
 * @method \TimSDK\Foundation\ResponseBag requestGetGroupMemberInfo($body = '', $option = [])
43
 * @method \TimSDK\Foundation\ResponseBag requestModifyGroupBaseInfo($body = '', $option = [])
44
 * @method \TimSDK\Foundation\ResponseBag requestAddGroupMember($body = '', $option = [])
45
 * @method \TimSDK\Foundation\ResponseBag requestDeleteGroupMember($body = '', $option = [])
46
 * @method \TimSDK\Foundation\ResponseBag requestModifyGroupMemberInfo($body = '', $option = [])
47
 * @method \TimSDK\Foundation\ResponseBag requestDestroyGroup($body = '', $option = [])
48
 * @method \TimSDK\Foundation\ResponseBag requestGetJoinedGroupList($body = '', $option = [])
49
 * @method \TimSDK\Foundation\ResponseBag requestGetRoleInGroup($body = '', $option = [])
50
 * @method \TimSDK\Foundation\ResponseBag requestForbidSendMsg($body = '', $option = [])
51
 * @method \TimSDK\Foundation\ResponseBag requestGetGroupShuttedUin($body = '', $option = [])
52
 * @method \TimSDK\Foundation\ResponseBag requestSendGroupMsg($body = '', $option = [])
53
 * @method \TimSDK\Foundation\ResponseBag requestSendGroupSystemNotification($body = '', $option = [])
54
 * @method \TimSDK\Foundation\ResponseBag requestChangeGroupOwner($body = '', $option = [])
55
 * @method \TimSDK\Foundation\ResponseBag requestImportGroup($body = '', $option = [])
56
 * @method \TimSDK\Foundation\ResponseBag requestImportGroupMsg($body = '', $option = [])
57
 * @method \TimSDK\Foundation\ResponseBag requestImportGroupMember($body = '', $option = [])
58
 * @method \TimSDK\Foundation\ResponseBag requestSetUnreadMsgNum($body = '', $option = [])
59
 * @method \TimSDK\Foundation\ResponseBag requestDeleteGroupMsgBySender($body = '', $option = [])
60
 * @method \TimSDK\Foundation\ResponseBag requestGroupMsgGetSimple($body = '', $option = [])
61
 * @method \TimSDK\Foundation\ResponseBag requestPortraitGet($body = '', $option = [])
62
 * @method \TimSDK\Foundation\ResponseBag requestPortraitSet($body = '', $option = [])
63
 * @method \TimSDK\Foundation\ResponseBag requestFriendAdd($body = '', $option = [])
64
 * @method \TimSDK\Foundation\ResponseBag requestFriendImport($body = '', $option = [])
65
 * @method \TimSDK\Foundation\ResponseBag requestFriendDelete($body = '', $option = [])
66
 * @method \TimSDK\Foundation\ResponseBag requestFriendDeleteAll($body = '', $option = [])
67
 * @method \TimSDK\Foundation\ResponseBag requestFriendCheck($body = '', $option = [])
68
 * @method \TimSDK\Foundation\ResponseBag requestFriendGetAll($body = '', $option = [])
69
 * @method \TimSDK\Foundation\ResponseBag requestFriendGetList($body = '', $option = [])
70
 * @method \TimSDK\Foundation\ResponseBag requestBlackListAdd($body = '', $option = [])
71
 * @method \TimSDK\Foundation\ResponseBag requestBlackListDelete($body = '', $option = [])
72
 * @method \TimSDK\Foundation\ResponseBag requestBlackListGet($body = '', $option = [])
73
 * @method \TimSDK\Foundation\ResponseBag requestBlackListCheck($body = '', $option = [])
74
 * @method \TimSDK\Foundation\ResponseBag requestGroupAdd($body = '', $option = [])
75
 * @method \TimSDK\Foundation\ResponseBag requestGroupDelete($body = '', $option = [])
76
 * @method \TimSDK\Foundation\ResponseBag requestDirtyWordsGet($body = '', $option = [])
77
 * @method \TimSDK\Foundation\ResponseBag requestDirtyWordsAdd($body = '', $option = [])
78
 * @method \TimSDK\Foundation\ResponseBag requestDirtyWordsDelete($body = '', $option = [])
79
 * @method \TimSDK\Foundation\ResponseBag requestGetHistory($body = '', $option = [])
80
 * @method \TimSDK\Foundation\ResponseBag requestQueryState($body = '', $option = [])
81
 * @method \TimSDK\Foundation\ResponseBag requestSetNoSpeaking($body = '', $option = [])
82
 * @method \TimSDK\Foundation\ResponseBag requestGetNoSpeaking($body = '', $option = [])
83
 */
84
class TimCloud extends ServiceContainer
85
{
86
    /**
87
     * TimCloud version
88
     *
89
     * @var string
90
     */
91
    const VERSION = '0.1.2';
92
93
    protected $providers = [
94
        \TimSDK\Core\ServiceProviders\ApiAliasServiceProvider::class,
95
        \TimSDK\Core\ServiceProviders\IMCloudServiceProvider::class,
96
    ];
97
98 12
    public function __construct(array $config = [], array $prepends = [])
99
    {
100 12
        if (Arr::has($config, ['private_key', 'public_key'])) {
101 10
            $config['private_key'] = $this->formatKey($config['private_key'], 'private');
102 10
            $config['public_key'] = $this->formatKey($config['public_key'], 'public');
103
        }
104
105 12
        parent::__construct($config, $prepends);
106 12
    }
107
108 2
    public function setAppId($appid)
109
    {
110 2
        $this->setConfig('app_id', $appid);
111
112 2
        return $this;
113
    }
114
115 2
    public function setIdentifier($identifier)
116
    {
117 2
        if (!empty($identifier)) {
118 2
            $this->setConfig('identifier', $identifier);
119
        }
120
121 2
        return $this;
122
    }
123
124 2 View Code Duplication
    public function setPrivateKey($prikey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126 2
        if (is_file($prikey)) {
127
            $prikey = file_get_contents($prikey);
128
        } else {
129 2
            $prikey = $this->formatKey($prikey, 'private');
130
        }
131
132 2
        if (!empty($prikey)) {
133 2
            $this->setConfig('private_key', $prikey);
134
        }
135
136 2
        return $this;
137
    }
138
139 2 View Code Duplication
    public function setPublicKey($pubkey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141 2
        if (is_file($pubkey)) {
142
            $pubkey = file_get_contents($pubkey);
143
        } else {
144 2
            $pubkey = $this->formatKey($pubkey, 'public');
145
        }
146
147 2
        $this->setConfig('public_key', $pubkey);
148
149 2
        return $this;
150
    }
151
152 10
    public function formatKey($key, $keyType)
153
    {
154 10
        $keyType = strtoupper($keyType);
155
156 10
        if (Str::startsWith($key, "-----BEGIN $keyType KEY-----")) {
157 1
            return $key;
158
        }
159
160 10
        return "-----BEGIN $keyType KEY-----" . PHP_EOL .
161 10
            wordwrap(str_replace(["\r", "\n"], '', $key), 64, PHP_EOL, true) .
162 10
            PHP_EOL . "-----END $keyType KEY-----";
163
    }
164
165
    /**
166
     * Send a request
167
     *
168
     * @param string $uri
169
     * @param string $body
170
     * @param array  $options
171
     * @return ResponseBag
172
     */
173 2
    public function request($uri, $body = '', $options = [])
174
    {
175 2
        return $this['im']->handle($this['apiAlias'][$uri], $body, $options);
176
    }
177
178
    /**
179
     * Get the version number of the application.
180
     *
181
     * @return string
182
     */
183 1
    public function version()
184
    {
185 1
        return static::VERSION;
186
    }
187
188
    /**
189
     * Call request method
190
     *
191
     * @param $method
192
     * @param $arguments
193
     * @return mixed
194
     */
195 1
    public function __call($method, $arguments)
196
    {
197 1
        $method = ltrim($method, 'request');
198 1
        $uri = Str::upper(Str::snake($method));
199 1
        array_unshift($arguments, $uri);
200
201 1
        return call_user_func_array([$this, 'request'], $arguments);
202
    }
203
204 2
    protected function setConfig($key, $value)
205
    {
206 2
        if (!empty($value)) {
207 2
            $oldVal = $this['config']->get($key, null);
208 2
            if ($value !== $oldVal) {
209 2
                $this['config']->set($key, $value);
210 2
                $this['im']->needRefresh();
211
            }
212
        }
213 2
    }
214
}
215