1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OneSignal; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
6
|
|
|
|
7
|
|
|
class Devices |
8
|
|
|
{ |
9
|
|
|
const DEVICES_LIMIT = 50; |
10
|
|
|
|
11
|
|
|
const IOS = 0; |
12
|
|
|
const ANDROID = 1; |
13
|
|
|
const AMAZON = 2; |
14
|
|
|
const WINDOWS_PHONE = 3; |
15
|
|
|
|
16
|
|
|
protected $api; |
17
|
|
|
|
18
|
|
|
public function __construct(OneSignal $api) |
19
|
|
|
{ |
20
|
|
|
$this->api = $api; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get information about device with provided ID. |
25
|
|
|
* |
26
|
|
|
* @param string $id Device ID |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function getOne($id) |
31
|
|
|
{ |
32
|
|
|
return $this->api->request('GET', '/players/' . $id); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get information about all registered devices for your application. |
37
|
|
|
* |
38
|
|
|
* Application auth key must be set. |
39
|
|
|
* |
40
|
|
|
* @param int $limit Results offset (results are sorted by ID) |
41
|
|
|
* @param int $offset How many devices to return (max 50) |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function getAll($limit = self::DEVICES_LIMIT, $offset = 0) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return $this->api->request('GET', '/players?' . http_build_query([ |
48
|
|
|
'limit' => max(0, min(self::DEVICES_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))), |
49
|
|
|
'offset' => max(0, min(self::DEVICES_LIMIT, filter_var($offset, FILTER_VALIDATE_INT))), |
50
|
|
|
]), [ |
51
|
|
|
'Authorization' => 'Basic ' . $this->api->getConfig()->getApplicationAuthKey(), |
52
|
|
|
], json_encode([ |
53
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
54
|
|
|
])); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register a device for your application. |
59
|
|
|
* |
60
|
|
|
* @param array $data Device data |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function add(array $data) |
65
|
|
|
{ |
66
|
|
|
$data = $this->resolve($data, function (OptionsResolver $resolver) { |
67
|
|
|
$resolver |
68
|
|
|
->setRequired('device_type') |
69
|
|
|
->setAllowedTypes('device_type', 'int') |
70
|
|
|
->setAllowedValues('device_type', [ |
71
|
|
|
self::IOS, |
72
|
|
|
self::ANDROID, |
73
|
|
|
self::AMAZON, |
74
|
|
|
self::WINDOWS_PHONE, |
75
|
|
|
]); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
return $this->api->request('POST', '/players', [ |
79
|
|
|
'Content-Type' => 'application/json', |
80
|
|
|
], json_encode($data)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Update existing registered device for your application with provided data. |
85
|
|
|
* |
86
|
|
|
* @param string $id Device ID |
87
|
|
|
* @param array $data New device data |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function update($id, array $data) |
92
|
|
|
{ |
93
|
|
|
return $this->api->request('PUT', '/players/' . $id, [ |
94
|
|
|
'Content-Type' => 'application/json', |
95
|
|
|
], json_encode($this->resolve($data))); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Call on new device session in your app. |
100
|
|
|
* |
101
|
|
|
* @param string $id Device ID |
102
|
|
|
* @param array $data Device data |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function onSession($id, array $data) |
107
|
|
|
{ |
108
|
|
|
$data = (new OptionsResolver()) |
109
|
|
|
->setDefined('identifier') |
110
|
|
|
->setAllowedTypes('identifier', 'string') |
111
|
|
|
->setDefined('language') |
112
|
|
|
->setAllowedTypes('language', 'string') |
113
|
|
|
->setDefined('timezone') |
114
|
|
|
->setAllowedTypes('timezone', 'int') |
115
|
|
|
->setDefined('game_version') |
116
|
|
|
->setAllowedTypes('game_version', 'string') |
117
|
|
|
->setDefined('device_model') |
118
|
|
|
->setAllowedTypes('device_model', 'string') |
119
|
|
|
->setDefined('ad_id') |
120
|
|
|
->setAllowedTypes('ad_id', 'string') |
121
|
|
|
->setDefined('sdk') |
122
|
|
|
->setAllowedTypes('sdk', 'string') |
123
|
|
|
->resolve($data); |
124
|
|
|
|
125
|
|
|
return $this->api->request('PUT', '/players/' . $id . '/on_session', [ |
126
|
|
|
'Content-Type' => 'application/json', |
127
|
|
|
], json_encode($data)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Track a new purchase. |
132
|
|
|
* |
133
|
|
|
* @param string $id Device ID |
134
|
|
|
* @param array $data Device data |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function onPurchase($id, array $data) |
139
|
|
|
{ |
140
|
|
|
$data = (new OptionsResolver()) |
141
|
|
|
->setDefined('existing') |
142
|
|
|
->setAllowedTypes('existing', 'bool') |
143
|
|
|
->setRequired('purchases') |
144
|
|
|
->setAllowedTypes('purchases', 'array') |
145
|
|
|
->resolve($data); |
146
|
|
|
|
147
|
|
|
foreach ($data['purchases'] as $key => $purchase) { |
148
|
|
|
$data['purchases'][$key] = (new OptionsResolver()) |
149
|
|
|
->setRequired('sku') |
150
|
|
|
->setAllowedTypes('sku', 'string') |
151
|
|
|
->setRequired('amount') |
152
|
|
|
->setAllowedTypes('amount', 'float') |
153
|
|
|
->setRequired('iso') |
154
|
|
|
->setAllowedTypes('iso', 'string') |
155
|
|
|
->resolve($purchase); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->api->request('PUT', '/players/' . $id . '/on_purchase', [ |
159
|
|
|
'Content-Type' => 'application/json', |
160
|
|
|
], json_encode($data)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Increment the device's total session length. |
165
|
|
|
* |
166
|
|
|
* @param string $id Device ID |
167
|
|
|
* @param array $data Device data |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function onFocus($id, array $data) |
172
|
|
|
{ |
173
|
|
|
$data = (new OptionsResolver()) |
174
|
|
|
->setDefault('state', 'ping') |
175
|
|
|
->setRequired('active_time') |
176
|
|
|
->setAllowedTypes('active_time', 'int') |
177
|
|
|
->resolve($data); |
178
|
|
|
|
179
|
|
|
return $this->api->request('PUT', '/players/' . $id . '/on_focus', [ |
180
|
|
|
'Content-Type' => 'application/json', |
181
|
|
|
], json_encode($data)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Export all information about devices in a CSV format for your application. |
186
|
|
|
* |
187
|
|
|
* Application auth key must be set. |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function csvExport() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
return $this->api->request('POST', '/players/csv_export', [ |
194
|
|
|
'Authorization' => 'Basic ' . $this->api->getConfig()->getApplicationAuthKey(), |
195
|
|
|
], json_encode([ |
196
|
|
|
'app_id' => $this->api->getConfig()->getApplicationId(), |
197
|
|
|
])); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
protected function resolve(array $data, callable $callback = null) |
201
|
|
|
{ |
202
|
|
|
$resolver = new OptionsResolver(); |
203
|
|
|
|
204
|
|
|
if (is_callable($callback)) { |
205
|
|
|
$callback($resolver); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$resolver |
209
|
|
|
->setDefined('identifier') |
210
|
|
|
->setAllowedTypes('identifier', 'string') |
211
|
|
|
->setDefined('language') |
212
|
|
|
->setAllowedTypes('language', 'string') |
213
|
|
|
->setDefined('timezone') |
214
|
|
|
->setAllowedTypes('timezone', 'int') |
215
|
|
|
->setDefined('game_version') |
216
|
|
|
->setAllowedTypes('game_version', 'string') |
217
|
|
|
->setDefined('device_model') |
218
|
|
|
->setAllowedTypes('device_model', 'string') |
219
|
|
|
->setDefined('device_os') |
220
|
|
|
->setAllowedTypes('device_os', 'string') |
221
|
|
|
->setDefined('ad_id') |
222
|
|
|
->setAllowedTypes('ad_id', 'string') |
223
|
|
|
->setDefined('sdk') |
224
|
|
|
->setAllowedTypes('sdk', 'string') |
225
|
|
|
->setDefined('session_count') |
226
|
|
|
->setAllowedTypes('session_count', 'int') |
227
|
|
|
->setDefined('tags') |
228
|
|
|
->setAllowedTypes('tags', 'array') |
229
|
|
|
->setDefined('amount_spent') |
230
|
|
|
->setAllowedTypes('amount_spent', 'float') |
231
|
|
|
->setDefined('created_at') |
232
|
|
|
->setAllowedTypes('created_at', 'int') |
233
|
|
|
->setDefined('playtime') |
234
|
|
|
->setAllowedTypes('playtime', 'int') |
235
|
|
|
->setDefined('badge_count') |
236
|
|
|
->setAllowedTypes('badge_count', 'int') |
237
|
|
|
->setDefined('last_active') |
238
|
|
|
->setAllowedTypes('last_active', 'int') |
239
|
|
|
->setDefault('app_id', $this->api->getConfig()->getApplicationId()); |
240
|
|
|
|
241
|
|
|
return $resolver->resolve($data); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.