Completed
Pull Request — master (#546)
by
unknown
05:32
created

Device::apply()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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