Completed
Push — master ( 3d76d0...009ad5 )
by Tomas
02:26
created

Devices   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 268
Duplicated Lines 9.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 12
c 8
b 1
f 2
lcom 1
cbo 3
dl 25
loc 268
rs 10

10 Methods

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