Completed
Push — master ( 3b05e6...5d6453 )
by Tomas
01:55
created

Devices::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 17
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
22
    protected $api;
23
24
    public function __construct(OneSignal $api)
25
    {
26
        $this->api = $api;
27
    }
28
29
    /**
30
     * Get information about device with provided ID.
31
     *
32
     * @param string $id Device ID
33
     *
34
     * @return array
35
     */
36
    public function getOne($id)
37
    {
38
        return $this->api->request('GET', '/players/'.$id);
39
    }
40
41
    /**
42
     * Get information about all registered devices for your application.
43
     *
44
     * Application auth key must be set.
45
     *
46
     * @param int $limit  Results offset (results are sorted by ID)
47
     * @param int $offset How many devices to return (max 300)
48
     *
49
     * @return array
50
     */
51
    public function getAll($limit = self::DEVICES_LIMIT, $offset = 0)
52
    {
53
        $query = [
54
            'limit' => max(1, min(self::DEVICES_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
55
            'offset' => max(0, min(self::DEVICES_LIMIT, filter_var($offset, FILTER_VALIDATE_INT))),
56
            'app_id' => $this->api->getConfig()->getApplicationId(),
57
        ];
58
59
        return $this->api->request('GET', '/players?'.http_build_query($query), [
60
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
61
        ]);
62
    }
63
64
    /**
65
     * Register a device for your application.
66
     *
67
     * @param array $data Device data
68
     *
69
     * @return array
70
     */
71
    public function add(array $data)
72
    {
73
        $data = $this->resolve($data, function (OptionsResolver $resolver) {
74
            $resolver
75
                ->setRequired('device_type')
76
                ->setAllowedTypes('device_type', 'int')
77
                ->setAllowedValues('device_type', [
78
                    self::IOS,
79
                    self::ANDROID,
80
                    self::AMAZON,
81
                    self::WINDOWS_PHONE,
82
                    self::WINDOWS_PHONE_MPNS,
83
                    self::CHROME_APP,
84
                    self::CHROME_WEB,
85
                    self::WINDOWS_PHONE_WNS,
86
                    self::SAFARI,
87
                    self::FIREFOX,
88
                ]);
89
        });
90
91
        return $this->api->request('POST', '/players', [], json_encode($data));
92
    }
93
94
    /**
95
     * Update existing registered device for your application with provided data.
96
     *
97
     * @param string $id   Device ID
98
     * @param array  $data New device data
99
     *
100
     * @return array
101
     */
102
    public function update($id, array $data)
103
    {
104
        $data = $this->resolve($data, function (OptionsResolver $resolver) {
105
            $resolver
106
                ->setDefined('notification_types')
107
                ->setAllowedTypes('notification_types', 'int')
108
                ->setAllowedValues('notification_types', [1, -2]);
109
        });
110
111
        return $this->api->request('PUT', '/players/'.$id, [], json_encode($data));
112
    }
113
114
    /**
115
     * Call on new device session in your app.
116
     *
117
     * @param string $id   Device ID
118
     * @param array  $data Device data
119
     *
120
     * @return array
121
     */
122
    public function onSession($id, array $data)
123
    {
124
        $data = (new OptionsResolver())
125
            ->setDefined('identifier')
126
            ->setAllowedTypes('identifier', 'string')
127
            ->setDefined('language')
128
            ->setAllowedTypes('language', 'string')
129
            ->setDefined('timezone')
130
            ->setAllowedTypes('timezone', 'int')
131
            ->setDefined('game_version')
132
            ->setAllowedTypes('game_version', 'string')
133
            ->setDefined('device_model')
134
            ->setAllowedTypes('device_model', 'string')
135
            ->setDefined('ad_id')
136
            ->setAllowedTypes('ad_id', 'string')
137
            ->setDefined('sdk')
138
            ->setAllowedTypes('sdk', 'string')
139
            ->resolve($data);
140
141
        return $this->api->request('PUT', '/players/'.$id.'/on_session', [], json_encode($data));
142
    }
143
144
    /**
145
     * Track a new purchase.
146
     *
147
     * @param string $id   Device ID
148
     * @param array  $data Device data
149
     *
150
     * @return array
151
     */
152
    public function onPurchase($id, array $data)
153
    {
154
        $data = (new OptionsResolver())
155
            ->setDefined('existing')
156
            ->setAllowedTypes('existing', 'bool')
157
            ->setRequired('purchases')
158
            ->setAllowedTypes('purchases', 'array')
159
            ->resolve($data);
160
161
        foreach ($data['purchases'] as $key => $purchase) {
162
            $data['purchases'][$key] = (new OptionsResolver())
163
                ->setRequired('sku')
164
                ->setAllowedTypes('sku', 'string')
165
                ->setRequired('amount')
166
                ->setAllowedTypes('amount', 'float')
167
                ->setRequired('iso')
168
                ->setAllowedTypes('iso', 'string')
169
                ->resolve($purchase);
170
        }
171
172
        return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
173
    }
174
175
    /**
176
     * Increment the device's total session length.
177
     *
178
     * @param string $id   Device ID
179
     * @param array  $data Device data
180
     *
181
     * @return array
182
     */
183
    public function onFocus($id, array $data)
184
    {
185
        $data = (new OptionsResolver())
186
            ->setDefault('state', 'ping')
187
            ->setRequired('active_time')
188
            ->setAllowedTypes('active_time', 'int')
189
            ->resolve($data);
190
191
        return $this->api->request('PUT', '/players/'.$id.'/on_focus', [], json_encode($data));
192
    }
193
194
    /**
195
     * Export all information about devices in a CSV format for your application.
196
     *
197
     * Application auth key must be set.
198
     *
199
     * @param array $extraFields Additional fields that you wish to include.
200
     *                           Currently supports: "location", "rooted"
201
     *
202
     * @return array
203
     */
204
    public function csvExport(array $extraFields = [])
205
    {
206
        $url = '/players/csv-export?app_id='.$this->api->getConfig()->getApplicationId();
207
208
        $headers = [
209
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
210
        ];
211
212
        $body = [
213
            'extra_fields' => $extraFields,
214
        ];
215
216
        return $this->api->request('POST', $url, $headers, $body);
0 ignored issues
show
Documentation introduced by
$body is of type array<string,array,{"extra_fields":"array"}>, but the function expects a string|object<Psr\Http\M...e\StreamInterface>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
217
    }
218
219
    protected function resolve(array $data, callable $callback = null)
220
    {
221
        $resolver = new OptionsResolver();
222
223
        if (is_callable($callback)) {
224
            $callback($resolver);
225
        }
226
227
        $resolver
228
            ->setDefined('identifier')
229
            ->setAllowedTypes('identifier', 'string')
230
            ->setDefined('language')
231
            ->setAllowedTypes('language', 'string')
232
            ->setDefined('timezone')
233
            ->setAllowedTypes('timezone', 'int')
234
            ->setDefined('game_version')
235
            ->setAllowedTypes('game_version', 'string')
236
            ->setDefined('device_model')
237
            ->setAllowedTypes('device_model', 'string')
238
            ->setDefined('device_os')
239
            ->setAllowedTypes('device_os', 'string')
240
            ->setDefined('ad_id')
241
            ->setAllowedTypes('ad_id', 'string')
242
            ->setDefined('sdk')
243
            ->setAllowedTypes('sdk', 'string')
244
            ->setDefined('session_count')
245
            ->setAllowedTypes('session_count', 'int')
246
            ->setDefined('tags')
247
            ->setAllowedTypes('tags', 'array')
248
            ->setDefined('amount_spent')
249
            ->setAllowedTypes('amount_spent', 'float')
250
            ->setDefined('created_at')
251
            ->setAllowedTypes('created_at', 'int')
252
            ->setDefined('playtime')
253
            ->setAllowedTypes('playtime', 'int')
254
            ->setDefined('badge_count')
255
            ->setAllowedTypes('badge_count', 'int')
256
            ->setDefined('last_active')
257
            ->setAllowedTypes('last_active', 'int')
258
            ->setDefined('test_type')
259
            ->setAllowedTypes('test_type', 'int')
260
            ->setAllowedValues('test_type', [1, 2])
261
            ->setDefault('app_id', $this->api->getConfig()->getApplicationId());
262
263
        return $resolver->resolve($data);
264
    }
265
}
266