Test Failed
Push — master ( 90f1e5...8cd561 )
by Jim
02:25
created

IMCloud::getLatestQueryStringArray()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.439
cc 5
eloc 17
nc 12
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/14/2018
6
 * Time: 8:08 PM
7
 */
8
9
namespace TimSDK\Core;
10
11
use TimSDK\Support\Str;
12
use TimSDK\Support\Log;
13
use TimSDK\Support\Arr;
14
use TimSDK\Support\Collection;
15
use TimSDK\Core\Exceptions\HttpException;
16
use TimSDK\Core\Exceptions\MissingArgumentsException;
17
18
class IMCloud extends BaseIMCloud
19
{
20
    /**
21
     * @var Collection
22
     */
23
    protected $query;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $needRefresh = false;
29
30
    /**
31
     * Set on refresh swith
32
     */
33
    public function needRefresh()
34
    {
35
        $this->needRefresh = true;
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    public function isNeedRefresh()
42
    {
43
        return (bool) $this->needRefresh;
44
    }
45
46
    /**
47
     * Init
48
     *
49
     * @throws MissingArgumentsException
50
     * @throws \TimSDK\Core\Exceptions\UserSigException
51
     */
52
    public function initialize()
53
    {
54
        $this->getRefreshedQueryStringCollection(true);
55
    }
56
57
    /**
58
     * Request api
59
     *
60
     * @param       $uri
61
     * @param array $data
62
     * @param array $options
63
     * @return \TimSDK\Foundation\ResponseBag
64
     * @throws \TimSDK\Core\Exceptions\JsonParseException
65
     * @throws \TimSDK\Core\Exceptions\UserSigException
66
     * @throws \TimSDK\Core\Exceptions\HttpException
67
     * @throws \TimSDK\Core\Exceptions\MissingArgumentsException
68
     */
69
    public function handle($uri, $data = [], $options = [])
70
    {
71
        if (empty($data)) {
72
            $data = '{}';
73
        }
74
75
        $response = $this->httpPostJson($uri, $data, array_merge($options, [
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type string; however, parameter $data of TimSDK\Core\BaseIMCloud::httpPostJson() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $response = $this->httpPostJson($uri, /** @scrutinizer ignore-type */ $data, array_merge($options, [
Loading history...
76
            'query' => $this->getRefreshedQueryStringArray()
77
        ]));
78
79
        $this->checkAndThrow($response->getContents());
80
81
        return $response;
82
    }
83
84
    /**
85
     * Generate sig
86
     *
87
     * @param $identifier
88
     * @return string
89
     * @throws \TimSDK\Core\Exceptions\UserSigException
90
     */
91
    public function generateSig($identifier)
92
    {
93
        return $this->app['TLSSig']->genSig($identifier);
94
    }
95
96
    /**
97
     * Refresh query string
98
     *
99
     * @param bool $force
100
     * @return Collection
101
     * @throws Exceptions\UserSigException
102
     * @throws MissingArgumentsException
103
     */
104
    public function getRefreshedQueryStringCollection($force = false)
105
    {
106
        if ($this->needRefresh || $force) {
107
            $this->needRefresh = false;
108
            $this->query = $this->getQueryStringCollection();
109
            $this->query->setAll($this->getLatestQueryStringArray());
110
        }
111
112
        return $this->query;
113
    }
114
115
    /**
116
     * Get the refreshed query string
117
     *
118
     * @return array
119
     * @throws Exceptions\UserSigException
120
     * @throws MissingArgumentsException
121
     */
122
    public function getRefreshedQueryStringArray()
123
    {
124
        return $this->getRefreshedQueryStringCollection()->toArray();
125
    }
126
127
    /**
128
     * Query Getter
129
     *
130
     * @return Collection
131
     */
132
    public function getQueryStringCollection()
133
    {
134
        if (!$this->query instanceof Collection) {
0 ignored issues
show
introduced by
$this->query is always a sub-type of TimSDK\Support\Collection. If $this->query can have other possible types, add them to src/Core/IMCloud.php:21.
Loading history...
135
            $this->query = new Collection();
136
        }
137
138
        return $this->query;
139
    }
140
141
    /**
142
     * Get the latest query string array
143
     *
144
     * @return array
145
     * @throws Exceptions\UserSigException
146
     * @throws MissingArgumentsException
147
     */
148
    public function getLatestQueryStringArray()
149
    {
150
        $data = Arr::only($this->app['config']->all(), [
151
            'sdkappid',
152
            'identifier',
153
            'prikey',
154
            'pubkey',
155
            'random',
156
            'contenttype',
157
        ]);
158
159
        if (!isset($data['random'])) {
160
            $data['random'] = time();
161
        }
162
163
        if (!isset($data['contenttype'])) {
164
            $data['contenttype'] = 'json';
165
        }
166
167
        foreach (['sdkappid', 'identifier', 'prikey', 'pubkey'] as $item) {
168
            if (!isset($data[$item])) {
169
                Log::debug('IMCloud Query: ', $data);
170
                throw new MissingArgumentsException('Missing ' . $item);
171
            }
172
        }
173
174
        $data['usersig'] = $this->generateSig($data['identifier']);
175
176
        return $data;
177
    }
178
179
    /**
180
     * Get a full url
181
     *
182
     * @param $uri
183
     * @return string
184
     */
185
    protected function getFullApiUrl($uri)
186
    {
187
        return Str::startsWith($uri, ['http', 'https']) ? $uri : API::BASE_URL . $uri;
188
    }
189
190
    /**
191
     * Check the array data errors, and Throw exception when the contents contains error.
192
     *
193
     * @param array|Collection $contents
194
     *
195
     * @throws HttpException
196
     */
197
    protected function checkAndThrow($contents)
198
    {
199
        if ($contents instanceof Collection) {
200
            $contents = $contents->toArray();
201
        }
202
203
        if (isset($contents['ErrorCode']) && 0 !== $contents['ErrorCode']) {
204
            if (empty($contents['ErrorInfo'])) {
205
                $contents['ErrorInfo'] = 'Unknown';
206
            }
207
            throw new HttpException($contents['ErrorInfo'], $contents['ErrorCode']);
208
        }
209
    }
210
}
211