Completed
Pull Request — master (#529)
by
unknown
02:47
created

Device::unbind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 10
ccs 0
cts 9
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
 */
19
namespace EasyWeChat\Device;
20
21
use EasyWeChat\Core\AccessToken;
22
use EasyWeChat\Core\AbstractAPI;
23
24
/**
25
 * Class Device.
26
 */
27
class Device extends AbstractAPI
28
{
29
30
    protected $deviceType;
31
    protected $productId;
32
    protected $config;
33
34
    const API_TRANS_MSG = 'https://api.weixin.qq.com/device/transmsg';
35
    const API_CREATE = 'https://api.weixin.qq.com/device/create_qrcode';
36
    const API_DEV_STAT = 'https://api.weixin.qq.com/device/get_stat';
37
    const API_DEV_AUTH = 'https://api.weixin.qq.com/device/authorize_device';
38
    const API_DEV_GET_QRCODE = 'https://api.weixin.qq.com/device/getqrcode';
39
    const API_DEV_VERIFY_QRCODE = 'https://api.weixin.qq.com/device/verify_qrcode';
40
    const API_DEV_BIND = 'https://api.weixin.qq.com/device/bind';
41
    const API_DEV_UNBIND = 'https://api.weixin.qq.com/device/unbind';
42
    const API_DEV_COMPEL_BIND = 'https://api.weixin.qq.com/device/compel_bind';
43
    const API_DEV_COMPEL_UNBIND = 'https://api.weixin.qq.com/device/compel_unbind';
44
    const API_DEV_GET_OPENID = 'https://api.weixin.qq.com/device/get_openid';
45
    const API_USER_DEV_BIND = 'https://api.weixin.qq.com/device/get_bind_device';
46
47
    public function __construct(AccessToken $accessToken, $config)
48
    {
49
        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...
50
        $this->config = $config;
51
        $this->deviceType = $this->config['device_type'];
52
        $this->productId = $this->config['product_id'];
53
    }
54
55
    public function setProductId($productId)
56
    {
57
        $this->productId = $productId;
58
        return $this;
59
    }
60
61
    /**
62
     * Send message to device
63
     *
64
     * @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...
65
     *
66
     * @return \EasyWeChat\Support\Collection
67
     */
68
    public function sendToDevice($deviceId, $openId, $content)
69
    {
70
        $params = [
71
            'device_type' => $this->deviceType,
72
            'device_id' => $deviceId,
73
            'open_id' => $openId,
74
            'content' => base64_decode($content),
75
        ];
76
77
        return $this->parseJSON('json', [self::API_TRANS_MSG, $params]);
78
    }
79
80
    public function getDeviceQrcode(array $deviceIds)
81
    {
82
        $params = [
83
            'device_num' => count($deviceIds),
84
            'device_id_list' => $deviceIds
85
        ];
86
87
        return $this->parseJSON('json', [self::API_CREATE, $params]);
88
    }
89
90
    public function authorizeDevice(array $deviceInfos, $opType = 0)
91
    {
92
        $params = [
93
            'device_num' => count($deviceInfos),
94
            'device_list' => $this->getDeviceList($deviceInfos),
95
            'op_type' => $opType,
96
            'product_id' => $this->productId
97
        ];
98
99
        return $this->parseJSON('json', [self::API_DEV_AUTH, $params]);
100
    }
101
102
    protected function getDeviceList($deviceInfos)
103
    {
104
        $res = [];
105
        foreach($deviceInfos as $dInfo)
106
        {
107
            $data = [
108
                'id' => $dInfo['deviceId'],
109
                'mac' => $dInfo['mac'],
110
                'connect_protocol' => $this->config['connect_protocol'],
111
                'auth_key' => $this->config['auth_key'],
112
                'close_strategy' => $this->config['close_strategy'],
113
                'conn_strategy' => $this->config['conn_strategy'],
114
                'crypt_method' => $this->config['crypt_method'],
115
                'auth_ver' => $this->config['auth_ver'],
116
                'manu_mac_pos' => $this->config['manu_mac_pos'],
117
                'ser_mac_pos' => $this->config['ser_mac_pos'],
118
            ];
119
120
            !empty($this->config['ble_simple_protocol']) ? $data['ble_simple_protocol'] = $this->config['ble_simple_protocol'] : '';
121
122
            $res[] = $data;
123
        }
124
125
        return $res;
126
    }
127
128
    public function createDeviceId()
129
    {
130
        $params = [
131
            'product_id' => $this->productId,
132
        ];
133
134
        return $this->parseJSON('get', [self::API_DEV_GET_QRCODE, $params]);
135
    }
136
137
    public function bind($openId, $deviceId, $ticket)
138
    {
139
        $params = [
140
            'ticket' => $ticket,
141
            'device_id' => $deviceId,
142
            'openid' => $openId
143
        ];
144
145
        return $this->parseJSON('json', [self::API_DEV_BIND, $params]);
146
    }
147
148
    public function unbind($openId, $deviceId, $ticket)
149
    {
150
        $params = [
151
            'ticket' => $ticket,
152
            'device_id' => $deviceId,
153
            'openid' => $openId
154
        ];
155
156
        return $this->parseJSON('json', [self::API_DEV_UNBIND, $params]);
157
    }
158
159 View Code Duplication
    public function compelBind($openId, $deviceId)
160
    {
161
        $params = [
162
            'device_id' => $deviceId,
163
            'openid' => $openId
164
        ];
165
166
        return $this->parseJSON('json', [self::API_DEV_COMPEL_BIND, $params]);
167
    }
168
169 View Code Duplication
    public function compelUnbind($openId, $deviceId)
170
    {
171
        $params = [
172
            'device_id' => $deviceId,
173
            'openid' => $openId
174
        ];
175
176
        return $this->parseJSON('json', [self::API_DEV_COMPEL_UNBIND, $params]);
177
    }
178
179 View Code Duplication
    public function getDeviceStatus($deviceId)
180
    {
181
        $params = [
182
            'device_id' => $deviceId
183
        ];
184
185
        return $this->parseJSON('get', [self::API_DEV_STAT, $params]);
186
    }
187
188 View Code Duplication
    public function verifyQrcode($ticket)
189
    {
190
        $params = [
191
            'ticket' => $ticket
192
        ];
193
194
        return $this->parseJSON('post', [self::API_DEV_VERIFY_QRCODE, $params]);
195
    }
196
197 View Code Duplication
    public function getOpenid($deviceId)
198
    {
199
        $params = [
200
            'device_type' => $this->deviceType,
201
            'device_id' => $deviceId
202
        ];
203
204
        return $this->parseJSON('get', [self::API_DEV_GET_OPENID, $params]);
205
    }
206
207 View Code Duplication
    public function getDeviceidByOpenid($openid)
208
    {
209
        $params = [
210
            'openid' => $openid
211
        ];
212
213
        return $this->parseJSON('get', [self::API_USER_DEV_BIND, $params]);
214
    }
215
}
216