Completed
Pull Request — master (#24)
by Tomas
02:12
created

Devices::add()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 24
rs 8.9713
cc 1
eloc 19
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 = 50;
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 50)
48
     *
49
     * @return array
50
     */
51 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...
52
    {
53
        return $this->api->request('GET', '/players?' . http_build_query([
54
            'limit' => max(0, 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
        ]), [
57
            'Authorization' => 'Basic ' . $this->api->getConfig()->getApplicationAuthKey(),
58
        ], json_encode([
59
            'app_id' => $this->api->getConfig()->getApplicationId(),
60
        ]));
61
    }
62
63
    /**
64
     * Register a device for your application.
65
     *
66
     * @param array $data Device data
67
     *
68
     * @return array
69
     */
70
    public function add(array $data)
71
    {
72
        $data = $this->resolve($data, function (OptionsResolver $resolver) {
73
            $resolver
74
                ->setRequired('device_type')
75
                ->setAllowedTypes('device_type', 'int')
76
                ->setAllowedValues('device_type', [
77
                    self::IOS,
78
                    self::ANDROID,
79
                    self::AMAZON,
80
                    self::WINDOWS_PHONE,
81
                    self::WINDOWS_PHONE_MPNS,
82
                    self::CHROME_APP,
83
                    self::CHROME_WEB,
84
                    self::WINDOWS_PHONE_WNS,
85
                    self::SAFARI,
86
                    self::FIREFOX,
87
                ]);
88
        });
89
90
        return $this->api->request('POST', '/players', [
91
            'Content-Type' => 'application/json',
92
        ], json_encode($data));
93
    }
94
95
    /**
96
     * Update existing registered device for your application with provided data.
97
     *
98
     * @param string $id   Device ID
99
     * @param array  $data New device data
100
     *
101
     * @return array
102
     */
103
    public function update($id, array $data)
104
    {
105
        return $this->api->request('PUT', '/players/' . $id, [
106
            'Content-Type' => 'application/json',
107
        ], json_encode($this->resolve($data)));
108
    }
109
110
    /**
111
     * Call on new device session in your app.
112
     *
113
     * @param string $id   Device ID
114
     * @param array  $data Device data
115
     *
116
     * @return array
117
     */
118
    public function onSession($id, array $data)
119
    {
120
        $data = (new OptionsResolver())
121
            ->setDefined('identifier')
122
            ->setAllowedTypes('identifier', 'string')
123
            ->setDefined('language')
124
            ->setAllowedTypes('language', 'string')
125
            ->setDefined('timezone')
126
            ->setAllowedTypes('timezone', 'int')
127
            ->setDefined('game_version')
128
            ->setAllowedTypes('game_version', 'string')
129
            ->setDefined('device_model')
130
            ->setAllowedTypes('device_model', 'string')
131
            ->setDefined('ad_id')
132
            ->setAllowedTypes('ad_id', 'string')
133
            ->setDefined('sdk')
134
            ->setAllowedTypes('sdk', 'string')
135
            ->resolve($data);
136
137
        return $this->api->request('PUT', '/players/' . $id . '/on_session', [
138
            'Content-Type' => 'application/json',
139
        ], json_encode($data));
140
    }
141
142
    /**
143
     * Track a new purchase.
144
     *
145
     * @param string $id   Device ID
146
     * @param array  $data Device data
147
     *
148
     * @return array
149
     */
150
    public function onPurchase($id, array $data)
151
    {
152
        $data = (new OptionsResolver())
153
            ->setDefined('existing')
154
            ->setAllowedTypes('existing', 'bool')
155
            ->setRequired('purchases')
156
            ->setAllowedTypes('purchases', 'array')
157
            ->resolve($data);
158
159
        foreach ($data['purchases'] as $key => $purchase) {
160
            $data['purchases'][$key] = (new OptionsResolver())
161
                ->setRequired('sku')
162
                ->setAllowedTypes('sku', 'string')
163
                ->setRequired('amount')
164
                ->setAllowedTypes('amount', 'float')
165
                ->setRequired('iso')
166
                ->setAllowedTypes('iso', 'string')
167
                ->resolve($purchase);
168
        }
169
170
        return $this->api->request('PUT', '/players/' . $id . '/on_purchase', [
171
            'Content-Type' => 'application/json',
172
        ], 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', [
192
            'Content-Type' => 'application/json',
193
        ], json_encode($data));
194
    }
195
196
    /**
197
     * Export all information about devices in a CSV format for your application.
198
     *
199
     * Application auth key must be set.
200
     *
201
     * @return array
202
     */
203 View Code Duplication
    public function csvExport()
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...
204
    {
205
        return $this->api->request('POST', '/players/csv_export', [
206
            'Authorization' => 'Basic ' . $this->api->getConfig()->getApplicationAuthKey(),
207
        ], json_encode([
208
            'app_id' => $this->api->getConfig()->getApplicationId(),
209
        ]));
210
    }
211
212
    protected function resolve(array $data, callable $callback = null)
213
    {
214
        $resolver = new OptionsResolver();
215
216
        if (is_callable($callback)) {
217
            $callback($resolver);
218
        }
219
220
        $resolver
221
            ->setDefined('identifier')
222
            ->setAllowedTypes('identifier', 'string')
223
            ->setDefined('language')
224
            ->setAllowedTypes('language', 'string')
225
            ->setDefined('timezone')
226
            ->setAllowedTypes('timezone', 'int')
227
            ->setDefined('game_version')
228
            ->setAllowedTypes('game_version', 'string')
229
            ->setDefined('device_model')
230
            ->setAllowedTypes('device_model', 'string')
231
            ->setDefined('device_os')
232
            ->setAllowedTypes('device_os', 'string')
233
            ->setDefined('ad_id')
234
            ->setAllowedTypes('ad_id', 'string')
235
            ->setDefined('sdk')
236
            ->setAllowedTypes('sdk', 'string')
237
            ->setDefined('session_count')
238
            ->setAllowedTypes('session_count', 'int')
239
            ->setDefined('tags')
240
            ->setAllowedTypes('tags', 'array')
241
            ->setDefined('amount_spent')
242
            ->setAllowedTypes('amount_spent', 'float')
243
            ->setDefined('created_at')
244
            ->setAllowedTypes('created_at', 'int')
245
            ->setDefined('playtime')
246
            ->setAllowedTypes('playtime', 'int')
247
            ->setDefined('badge_count')
248
            ->setAllowedTypes('badge_count', 'int')
249
            ->setDefined('last_active')
250
            ->setAllowedTypes('last_active', 'int')
251
            ->setDefault('app_id', $this->api->getConfig()->getApplicationId());
252
253
        return $resolver->resolve($data);
254
    }
255
}
256