Completed
Push — master ( f286a8...3bf7f4 )
by Carlos
02:50
created

Device::createDeviceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[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
/**
13
 * Device.php.
14
 *
15
 * @author    soone <[email protected]>
16
 * @copyright 2016 soone <[email protected]>
17
 */
18
namespace EasyWeChat\Device;
19
20
use EasyWeChat\Core\AbstractAPI;
21
use EasyWeChat\Core\AccessToken;
22
23
/**
24
 * Class Device.
25
 */
26
class Device extends AbstractAPI
27
{
28
    protected $deviceType;
29
    protected $productId;
30
    protected $config;
31
32
    const API_TRANS_MSG = 'https://api.weixin.qq.com/device/transmsg';
33
    const API_CREATE = 'https://api.weixin.qq.com/device/create_qrcode';
34
    const API_DEV_STAT = 'https://api.weixin.qq.com/device/get_stat';
35
    const API_DEV_AUTH = 'https://api.weixin.qq.com/device/authorize_device';
36
    const API_DEV_GET_QRCODE = 'https://api.weixin.qq.com/device/getqrcode';
37
    const API_DEV_VERIFY_QRCODE = 'https://api.weixin.qq.com/device/verify_qrcode';
38
    const API_DEV_BIND = 'https://api.weixin.qq.com/device/bind';
39
    const API_DEV_UNBIND = 'https://api.weixin.qq.com/device/unbind';
40
    const API_DEV_COMPEL_BIND = 'https://api.weixin.qq.com/device/compel_bind';
41
    const API_DEV_COMPEL_UNBIND = 'https://api.weixin.qq.com/device/compel_unbind';
42
    const API_DEV_GET_OPENID = 'https://api.weixin.qq.com/device/get_openid';
43
    const API_USER_DEV_BIND = 'https://api.weixin.qq.com/device/get_bind_device';
44
45
    public function __construct(AccessToken $accessToken, $config)
46
    {
47
        parent::setAccessToken($accessToken);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setAccessToken() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->setAccessToken().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
48
        $this->config = $config;
49
        $this->deviceType = $this->config['device_type'];
50
        $this->productId = $this->config['product_id'];
51
    }
52
53
    public function setProductId($productId)
54
    {
55
        $this->productId = $productId;
56
        return $this;
57
    }
58
59
    /**
60
     *
61
     * Send message to device.
62
     * @param int $sceneValue
0 ignored issues
show
Bug introduced by
There is no parameter named $sceneValue. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
     *
64
     * @return \EasyWeChat\Support\Collection
65
     */
66
    public function sendToDevice($deviceId, $openId, $content)
67
    {
68
        $params = [
69
            'device_type' => $this->deviceType,
70
            'device_id' => $deviceId,
71
            'open_id' => $openId,
72
            'content' => base64_decode($content, true),
73
        ];
74
75
        return $this->parseJSON('json', [self::API_TRANS_MSG, $params]);
76
    }
77
78
    public function getDeviceQrcode(array $deviceIds)
79
    {
80
        $params = [
81
            'device_num' => count($deviceIds),
82
            'device_id_list' => $deviceIds,
83
        ];
84
85
        return $this->parseJSON('json', [self::API_CREATE, $params]);
86
    }
87
88
    public function authorizeDevice(array $deviceInfos, $opType = 0)
89
    {
90
        $params = [
91
            'device_num' => count($deviceInfos),
92
            'device_list' => $this->getDeviceList($deviceInfos),
93
            'op_type' => $opType,
94
            'product_id' => $this->productId,
95
        ];
96
97
        return $this->parseJSON('json', [self::API_DEV_AUTH, $params]);
98
    }
99
100
    protected function getDeviceList($deviceInfos)
101
    {
102
        $res = [];
103
        foreach($deviceInfos as $dInfo) {
104
            $data = [
105
                'id' => $dInfo['deviceId'],
106
                'mac' => $dInfo['mac'],
107
                'connect_protocol' => $this->config['connect_protocol'],
108
                'auth_key' => $this->config['auth_key'],
109
                'close_strategy' => $this->config['close_strategy'],
110
                'conn_strategy' => $this->config['conn_strategy'],
111
                'crypt_method' => $this->config['crypt_method'],
112
                'auth_ver' => $this->config['auth_ver'],
113
                'manu_mac_pos' => $this->config['manu_mac_pos'],
114
                'ser_mac_pos' => $this->config['ser_mac_pos'],
115
            ];
116
117
            !empty($this->config['ble_simple_protocol']) ? $data['ble_simple_protocol'] = $this->config['ble_simple_protocol'] : '';
118
119
            $res[] = $data;
120
        }
121
122
        return $res;
123
    }
124
125
    public function createDeviceId()
126
    {
127
        $params = [
128
            'product_id' => $this->productId,
129
        ];
130
131
        return $this->parseJSON('get', [self::API_DEV_GET_QRCODE, $params]);
132
    }
133
134
    public function bind($openId, $deviceId, $ticket)
135
    {
136
        $params = [
137
            'ticket' => $ticket,
138
            'device_id' => $deviceId,
139
            'openid' => $openId,
140
        ];
141
142
        return $this->parseJSON('json', [self::API_DEV_BIND, $params]);
143
    }
144
145
    public function unbind($openId, $deviceId, $ticket)
146
    {
147
        $params = [
148
            'ticket' => $ticket,
149
            'device_id' => $deviceId,
150
            'openid' => $openId,
151
        ];
152
153
        return $this->parseJSON('json', [self::API_DEV_UNBIND, $params]);
154
    }
155
156 View Code Duplication
    public function compelBind($openId, $deviceId)
157
    {
158
        $params = [
159
            'device_id' => $deviceId,
160
            'openid' => $openId,
161
        ];
162
163
        return $this->parseJSON('json', [self::API_DEV_COMPEL_BIND, $params]);
164
    }
165
166 View Code Duplication
    public function compelUnbind($openId, $deviceId)
167
    {
168
        $params = [
169
            'device_id' => $deviceId,
170
            'openid' => $openId,
171
        ];
172
173
        return $this->parseJSON('json', [self::API_DEV_COMPEL_UNBIND, $params]);
174
    }
175
176 View Code Duplication
    public function getDeviceStatus($deviceId)
177
    {
178
        $params = [
179
            'device_id' => $deviceId,
180
        ];
181
182
        return $this->parseJSON('get', [self::API_DEV_STAT, $params]);
183
    }
184
185 View Code Duplication
    public function verifyQrcode($ticket)
186
    {
187
        $params = [
188
            'ticket' => $ticket,
189
        ];
190
191
        return $this->parseJSON('post', [self::API_DEV_VERIFY_QRCODE, $params]);
192
    }
193
194 View Code Duplication
    public function getOpenid($deviceId)
195
    {
196
        $params = [
197
            'device_type' => $this->deviceType,
198
            'device_id' => $deviceId,
199
        ];
200
201
        return $this->parseJSON('get', [self::API_DEV_GET_OPENID, $params]);
202
    }
203
204 View Code Duplication
    public function getDeviceidByOpenid($openid)
205
    {
206
        $params = [
207
            'openid' => $openid,
208
        ];
209
210
        return $this->parseJSON('get', [self::API_USER_DEV_BIND, $params]);
211
    }
212
}
213