Completed
Push — master ( 058da3...8d02ae )
by Carlos
05:22 queued 02:18
created

Device::apply()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 4
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 3
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    allen05ren <[email protected]>
16
 * @copyright 2016 overtrue <[email protected]>
17
 *
18
 * @see       https://github.com/overtrue
19
 * @see       http://overtrue.me
20
 */
21
22
namespace EasyWeChat\ShakeAround;
23
24
use EasyWeChat\Core\AbstractAPI;
25
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
26
27
/**
28
 * Class Device.
29
 */
30
class Device extends AbstractAPI
31
{
32
    const API_DEVICE_APPLYID = 'https://api.weixin.qq.com/shakearound/device/applyid';
33
    const API_DEVICE_APPLYSTATUS = 'https://api.weixin.qq.com/shakearound/device/applystatus';
34
    const API_DEVICE_UPDATE = 'https://api.weixin.qq.com/shakearound/device/update';
35
    const API_DEVICE_BINDLOCATION = 'https://api.weixin.qq.com/shakearound/device/bindlocation';
36
    const API_DEVICE_SEARCH = 'https://api.weixin.qq.com/shakearound/device/search';
37
38
    /**
39
     * Apply device ids.
40
     *
41
     * @param int    $quantity
42
     * @param string $reason
43
     * @param string $comment
44
     * @param int    $poiId
45
     *
46
     * @return \EasyWeChat\Support\Collection
47
     */
48 1
    public function apply($quantity, $reason, $comment = '', $poiId = null)
49
    {
50
        $params = [
51 1
            'quantity' => intval($quantity),
52 1
            'apply_reason' => $reason,
53 1
        ];
54
55 1
        if (!empty($comment)) {
56 1
            $params['comment'] = $comment;
57 1
        }
58
59 1
        if (!is_null($poiId)) {
60 1
            $params['poi_id'] = intval($poiId);
61 1
        }
62
63 1
        return $this->parseJSON('json', [self::API_DEVICE_APPLYID, $params]);
64
    }
65
66
    /**
67
     * Get audit status.
68
     *
69
     * @param int $applyId
70
     *
71
     * @return \EasyWeChat\Support\Collection
72
     */
73 1
    public function getStatus($applyId)
74
    {
75
        $params = [
76 1
            'apply_id' => intval($applyId),
77 1
        ];
78
79 1
        return $this->parseJSON('json', [self::API_DEVICE_APPLYSTATUS, $params]);
80
    }
81
82
    /**
83
     * Update a device comment.
84
     *
85
     * @param array  $deviceIdentifier
86
     * @param string $comment
87
     *
88
     * @return \EasyWeChat\Support\Collection
89
     */
90 1
    public function update(array $deviceIdentifier, $comment)
91
    {
92
        $params = [
93 1
            'device_identifier' => $deviceIdentifier,
94 1
            'comment' => $comment,
95 1
        ];
96
97 1
        return $this->parseJSON('json', [self::API_DEVICE_UPDATE, $params]);
98
    }
99
100
    /**
101
     * Bind location for device.
102
     *
103
     * @param array  $deviceIdentifier
104
     * @param int    $poiId
105
     * @param int    $type
106
     * @param string $poiAppid
107
     *
108
     * @return \EasyWeChat\Support\Collection
109
     *
110
     * @throws InvalidArgumentException
111
     */
112 1
    public function bindLocation(array $deviceIdentifier, $poiId, $type = 1, $poiAppid = null)
113
    {
114
        $params = [
115 1
            'device_identifier' => $deviceIdentifier,
116 1
            'poi_id' => intval($poiId),
117 1
        ];
118
119 1
        if ($type === 2) {
120 1
            if (is_null($poiAppid)) {
121 1
                throw new InvalidArgumentException('If value of argument #3 is 2, argument #4 is required.');
122
            }
123 1
            $params['type'] = 2;
124 1
            $params['poi_appid'] = $poiAppid;
125 1
        }
126
127 1
        return $this->parseJSON('json', [self::API_DEVICE_BINDLOCATION, $params]);
128
    }
129
130
    /**
131
     * Fetch batch of devices by deviceIds.
132
     *
133
     * @param array $deviceIdentifiers
134
     *
135
     * @return \EasyWeChat\Support\Collection
136
     */
137 1
    public function fetchByIds(array $deviceIdentifiers)
138
    {
139
        $params = [
140 1
            'type' => 1,
141 1
            'device_identifiers' => $deviceIdentifiers,
142 1
        ];
143
144 1
        return $this->fetch($params);
145
    }
146
147
    /**
148
     * Pagination to fetch batch of devices.
149
     *
150
     * @param int $lastSeen
151
     * @param int $count
152
     *
153
     * @return \EasyWeChat\Support\Collection
154
     */
155 1 View Code Duplication
    public function pagination($lastSeen, $count)
156
    {
157
        $params = [
158 1
            'type' => 2,
159 1
            'last_seen' => intval($lastSeen),
160 1
            'count' => intval($count),
161 1
        ];
162
163 1
        return $this->fetch($params);
164
    }
165
166
    /**
167
     * Fetch batch of devices by applyId.
168
     *
169
     * @param int $applyId
170
     * @param int $lastSeen
171
     * @param int $count
172
     *
173
     * @return \EasyWeChat\Support\Collection
174
     */
175 1 View Code Duplication
    public function fetchByApplyId($applyId, $lastSeen, $count)
176
    {
177
        $params = [
178 1
            'type' => 3,
179 1
            'apply_id' => intval($applyId),
180 1
            'last_seen' => intval($lastSeen),
181 1
            'count' => intval($count),
182 1
        ];
183
184 1
        return $this->fetch($params);
185
    }
186
187
    /**
188
     * Fetch batch of devices.
189
     *
190
     * @param array $params
191
     *
192
     * @return \EasyWeChat\Support\Collection
193
     */
194 3
    private function fetch($params)
195
    {
196 3
        return $this->parseJSON('json', [self::API_DEVICE_SEARCH, $params]);
197
    }
198
}
199