Completed
Push — master ( 572eca...d15297 )
by Tomas
01:54
created

Devices::csvExport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace OneSignal;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class Devices
8
{
9
    const DEVICES_LIMIT = 300;
10
11
    const IOS = 0;
12
    const ANDROID = 1;
13
    const AMAZON = 2;
14
    const WINDOWS_PHONE = 3;
15
    const WINDOWS_PHONE_MPNS = 3;
16
    const CHROME_APP = 4;
17
    const CHROME_WEB = 5;
18
    const WINDOWS_PHONE_WNS = 6;
19
    const SAFARI = 7;
20
    const FIREFOX = 8;
21
    const MACOS = 9;
22
23
    protected $api;
24
25
    public function __construct(OneSignal $api)
26
    {
27
        $this->api = $api;
28
    }
29
30
    /**
31
     * Get information about device with provided ID.
32
     *
33
     * @param string $id Device ID
34
     *
35
     * @return array
36
     */
37
    public function getOne($id)
38
    {
39
        $query = [
40
            'app_id' => $this->api->getConfig()->getApplicationId(),
41
        ];
42
43
        return $this->api->request('GET', '/players/'.$id.'?'.http_build_query($query));
44
    }
45
46
    /**
47
     * Get information about all registered devices for your application.
48
     *
49
     * Application auth key must be set.
50
     *
51
     * @param int $limit  How many devices to return. Max is 300. Default is 300
52
     * @param int $offset Result offset. Default is 0. Results are sorted by id
53
     *
54
     * @return array
55
     */
56 View Code Duplication
    public function getAll($limit = self::DEVICES_LIMIT, $offset = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
57
    {
58
        $query = [
59
            'limit' => max(1, min(self::DEVICES_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
60
            'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
61
            'app_id' => $this->api->getConfig()->getApplicationId(),
62
        ];
63
64
        return $this->api->request('GET', '/players?'.http_build_query($query), [
65
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
66
        ]);
67
    }
68
69
    /**
70
     * Register a device for your application.
71
     *
72
     * @param array $data Device data
73
     *
74
     * @return array
75
     */
76
    public function add(array $data)
77
    {
78
        $data = $this->resolve($data, function (OptionsResolver $resolver) {
79
            $resolver
80
                ->setRequired('device_type')
81
                ->setAllowedTypes('device_type', 'int')
82
                ->setAllowedValues('device_type', [
83
                    self::IOS,
84
                    self::ANDROID,
85
                    self::AMAZON,
86
                    self::WINDOWS_PHONE,
87
                    self::WINDOWS_PHONE_MPNS,
88
                    self::CHROME_APP,
89
                    self::CHROME_WEB,
90
                    self::WINDOWS_PHONE_WNS,
91
                    self::SAFARI,
92
                    self::FIREFOX,
93
                    self::MACOS,
94
                ]);
95
        });
96
97
        return $this->api->request('POST', '/players', [], json_encode($data));
98
    }
99
100
    /**
101
     * Update existing registered device for your application with provided data.
102
     *
103
     * @param string $id   Device ID
104
     * @param array  $data New device data
105
     *
106
     * @return array
107
     */
108
    public function update($id, array $data)
109
    {
110
        $data = $this->resolve($data);
111
112
        return $this->api->request('PUT', '/players/'.$id, [], json_encode($data));
113
    }
114
115
    /**
116
     * Call on new device session in your app.
117
     *
118
     * @param string $id   Device ID
119
     * @param array  $data Device data
120
     *
121
     * @return array
122
     */
123
    public function onSession($id, array $data)
124
    {
125
        $data = (new OptionsResolver())
126
            ->setDefined('identifier')
127
            ->setAllowedTypes('identifier', 'string')
128
            ->setDefined('language')
129
            ->setAllowedTypes('language', 'string')
130
            ->setDefined('timezone')
131
            ->setAllowedTypes('timezone', 'int')
132
            ->setDefined('game_version')
133
            ->setAllowedTypes('game_version', 'string')
134
            ->setDefined('device_model')
135
            ->setAllowedTypes('device_model', 'string')
136
            ->setDefined('ad_id')
137
            ->setAllowedTypes('ad_id', 'string')
138
            ->setDefined('sdk')
139
            ->setAllowedTypes('sdk', 'string')
140
            ->resolve($data);
141
142
        return $this->api->request('POST', '/players/'.$id.'/on_session', [], json_encode($data));
143
    }
144
145
    /**
146
     * Track a new purchase.
147
     *
148
     * @param string $id   Device ID
149
     * @param array  $data Device data
150
     *
151
     * @return array
152
     */
153
    public function onPurchase($id, array $data)
154
    {
155
        $data = (new OptionsResolver())
156
            ->setDefined('existing')
157
            ->setAllowedTypes('existing', 'bool')
158
            ->setRequired('purchases')
159
            ->setAllowedTypes('purchases', 'array')
160
            ->resolve($data);
161
162
        foreach ($data['purchases'] as $key => $purchase) {
163
            $data['purchases'][$key] = (new OptionsResolver())
164
                ->setRequired('sku')
165
                ->setAllowedTypes('sku', 'string')
166
                ->setRequired('amount')
167
                ->setAllowedTypes('amount', 'float')
168
                ->setRequired('iso')
169
                ->setAllowedTypes('iso', 'string')
170
                ->resolve($purchase);
171
        }
172
173
        return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
174
    }
175
176
    /**
177
     * Increment the device's total session length.
178
     *
179
     * @param string $id   Device ID
180
     * @param array  $data Device data
181
     *
182
     * @return array
183
     */
184
    public function onFocus($id, array $data)
185
    {
186
        $data = (new OptionsResolver())
187
            ->setDefault('state', 'ping')
188
            ->setRequired('active_time')
189
            ->setAllowedTypes('active_time', 'int')
190
            ->resolve($data);
191
192
        return $this->api->request('POST', '/players/'.$id.'/on_focus', [], json_encode($data));
193
    }
194
195
    /**
196
     * Export all information about devices in a CSV format for your application.
197
     *
198
     * Application auth key must be set.
199
     *
200
     * @param array $extraFields Additional fields that you wish to include.
201
     *                           Currently supports: "location", "rooted"
202
     *
203
     * @return array
204
     */
205
    public function csvExport(array $extraFields = [])
206
    {
207
        $url = '/players/csv_export?app_id='.$this->api->getConfig()->getApplicationId();
208
209
        $headers = [
210
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
211
        ];
212
213
        $body = [
214
            'extra_fields' => $extraFields,
215
        ];
216
217
        return $this->api->request('POST', $url, $headers, json_encode($body));
218
    }
219
220
    protected function resolve(array $data, callable $callback = null)
221
    {
222
        $resolver = new OptionsResolver();
223
224
        if (is_callable($callback)) {
225
            $callback($resolver);
226
        }
227
228
        $resolver
229
            ->setDefined('identifier')
230
            ->setAllowedTypes('identifier', 'string')
231
            ->setDefined('language')
232
            ->setAllowedTypes('language', 'string')
233
            ->setDefined('timezone')
234
            ->setAllowedTypes('timezone', 'int')
235
            ->setDefined('game_version')
236
            ->setAllowedTypes('game_version', 'string')
237
            ->setDefined('device_model')
238
            ->setAllowedTypes('device_model', 'string')
239
            ->setDefined('device_os')
240
            ->setAllowedTypes('device_os', 'string')
241
            ->setDefined('ad_id')
242
            ->setAllowedTypes('ad_id', 'string')
243
            ->setDefined('sdk')
244
            ->setAllowedTypes('sdk', 'string')
245
            ->setDefined('session_count')
246
            ->setAllowedTypes('session_count', 'int')
247
            ->setDefined('tags')
248
            ->setAllowedTypes('tags', 'array')
249
            ->setDefined('amount_spent')
250
            ->setAllowedTypes('amount_spent', 'float')
251
            ->setDefined('created_at')
252
            ->setAllowedTypes('created_at', 'int')
253
            ->setDefined('playtime')
254
            ->setAllowedTypes('playtime', 'int')
255
            ->setDefined('badge_count')
256
            ->setAllowedTypes('badge_count', 'int')
257
            ->setDefined('last_active')
258
            ->setAllowedTypes('last_active', 'int')
259
            ->setDefined('notification_types')
260
            ->setAllowedTypes('notification_types', 'int')
261
            ->setAllowedValues('notification_types', [1, -2])
262
            ->setDefined('test_type')
263
            ->setAllowedTypes('test_type', 'int')
264
            ->setAllowedValues('test_type', [1, 2])
265
            ->setDefault('app_id', $this->api->getConfig()->getApplicationId());
266
267
        return $resolver->resolve($data);
268
    }
269
}
270