Completed
Push — master ( d15297...c819e3 )
by Tomas
02:01
created

Devices   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 268
Duplicated Lines 4.48 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 12
loc 268
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOne() 0 8 1
A getAll() 12 12 1
A add() 0 23 1
A update() 0 6 1
B onSession() 0 26 1
A onPurchase() 0 22 2
A onFocus() 0 10 1
A csvExport() 0 14 1
A resolve() 0 49 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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_os')
135
            ->setAllowedTypes('device_os', 'string')
136
            // @todo: remove "device_model" later (this option is probably deprecated as it is removed from documentation)
137
            ->setDefined('device_model')
138
            ->setAllowedTypes('device_model', 'string')
139
            ->setDefined('ad_id')
140
            ->setAllowedTypes('ad_id', 'string')
141
            ->setDefined('sdk')
142
            ->setAllowedTypes('sdk', 'string')
143
            ->setDefined('tags')
144
            ->setAllowedTypes('tags', 'array')
145
            ->resolve($data);
146
147
        return $this->api->request('POST', '/players/'.$id.'/on_session', [], json_encode($data));
148
    }
149
150
    /**
151
     * Track a new purchase.
152
     *
153
     * @param string $id   Device ID
154
     * @param array  $data Device data
155
     *
156
     * @return array
157
     */
158
    public function onPurchase($id, array $data)
159
    {
160
        $data = (new OptionsResolver())
161
            ->setDefined('existing')
162
            ->setAllowedTypes('existing', 'bool')
163
            ->setRequired('purchases')
164
            ->setAllowedTypes('purchases', 'array')
165
            ->resolve($data);
166
167
        foreach ($data['purchases'] as $key => $purchase) {
168
            $data['purchases'][$key] = (new OptionsResolver())
169
                ->setRequired('sku')
170
                ->setAllowedTypes('sku', 'string')
171
                ->setRequired('amount')
172
                ->setAllowedTypes('amount', 'float')
173
                ->setRequired('iso')
174
                ->setAllowedTypes('iso', 'string')
175
                ->resolve($purchase);
176
        }
177
178
        return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
179
    }
180
181
    /**
182
     * Increment the device's total session length.
183
     *
184
     * @param string $id   Device ID
185
     * @param array  $data Device data
186
     *
187
     * @return array
188
     */
189
    public function onFocus($id, array $data)
190
    {
191
        $data = (new OptionsResolver())
192
            ->setDefault('state', 'ping')
193
            ->setRequired('active_time')
194
            ->setAllowedTypes('active_time', 'int')
195
            ->resolve($data);
196
197
        return $this->api->request('POST', '/players/'.$id.'/on_focus', [], json_encode($data));
198
    }
199
200
    /**
201
     * Export all information about devices in a CSV format for your application.
202
     *
203
     * Application auth key must be set.
204
     *
205
     * @param array $extraFields Additional fields that you wish to include.
206
     *                           Currently supports: "location", "country", "rooted"
207
     *
208
     * @return array
209
     */
210
    public function csvExport(array $extraFields = [])
211
    {
212
        $url = '/players/csv_export?app_id='.$this->api->getConfig()->getApplicationId();
213
214
        $headers = [
215
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
216
        ];
217
218
        $body = [
219
            'extra_fields' => $extraFields,
220
        ];
221
222
        return $this->api->request('POST', $url, $headers, json_encode($body));
223
    }
224
225
    protected function resolve(array $data, callable $callback = null)
226
    {
227
        $resolver = new OptionsResolver();
228
229
        if (is_callable($callback)) {
230
            $callback($resolver);
231
        }
232
233
        $resolver
234
            ->setDefined('identifier')
235
            ->setAllowedTypes('identifier', 'string')
236
            ->setDefined('language')
237
            ->setAllowedTypes('language', 'string')
238
            ->setDefined('timezone')
239
            ->setAllowedTypes('timezone', 'int')
240
            ->setDefined('game_version')
241
            ->setAllowedTypes('game_version', 'string')
242
            ->setDefined('device_model')
243
            ->setAllowedTypes('device_model', 'string')
244
            ->setDefined('device_os')
245
            ->setAllowedTypes('device_os', 'string')
246
            ->setDefined('ad_id')
247
            ->setAllowedTypes('ad_id', 'string')
248
            ->setDefined('sdk')
249
            ->setAllowedTypes('sdk', 'string')
250
            ->setDefined('session_count')
251
            ->setAllowedTypes('session_count', 'int')
252
            ->setDefined('tags')
253
            ->setAllowedTypes('tags', 'array')
254
            ->setDefined('amount_spent')
255
            ->setAllowedTypes('amount_spent', 'float')
256
            ->setDefined('created_at')
257
            ->setAllowedTypes('created_at', 'int')
258
            ->setDefined('playtime')
259
            ->setAllowedTypes('playtime', 'int')
260
            ->setDefined('badge_count')
261
            ->setAllowedTypes('badge_count', 'int')
262
            ->setDefined('last_active')
263
            ->setAllowedTypes('last_active', 'int')
264
            ->setDefined('notification_types')
265
            ->setAllowedTypes('notification_types', 'int')
266
            ->setAllowedValues('notification_types', [1, -2])
267
            ->setDefined('test_type')
268
            ->setAllowedTypes('test_type', 'int')
269
            ->setAllowedValues('test_type', [1, 2])
270
            ->setDefault('app_id', $this->api->getConfig()->getApplicationId());
271
272
        return $resolver->resolve($data);
273
    }
274
}
275