Completed
Push — master ( 286399...1d9aa3 )
by Tomas
13s
created

Devices::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace OneSignal;
4
5
use OneSignal\Resolver\ResolverFactory;
6
7
class Devices
8
{
9
    const DEVICES_LIMIT = 300;
10
11
    const IOS = 0;
12
13
    const ANDROID = 1;
14
15
    const AMAZON = 2;
16
17
    const WINDOWS_PHONE = 3;
18
19
    const WINDOWS_PHONE_MPNS = 3;
20
21
    const CHROME_APP = 4;
22
23
    const CHROME_WEB = 5;
24
25
    const WINDOWS_PHONE_WNS = 6;
26
27
    const SAFARI = 7;
28
29
    const FIREFOX = 8;
30
31
    const MACOS = 9;
32
33
    const ALEXA = 10;
34
35
    const EMAIL = 11;
36
37
    protected $api;
38
39
    private $resolverFactory;
40
41
    public function __construct(OneSignal $api, ResolverFactory $resolverFactory)
42
    {
43
        $this->api = $api;
44
        $this->resolverFactory = $resolverFactory;
45
    }
46
47
    /**
48
     * Get information about device with provided ID.
49
     *
50
     * @param string $id Device ID
51
     *
52
     * @return array
53
     */
54
    public function getOne($id)
55
    {
56
        $query = [
57
            'app_id' => $this->api->getConfig()->getApplicationId(),
58
        ];
59
60
        return $this->api->request('GET', '/players/'.$id.'?'.http_build_query($query));
61
    }
62
63
    /**
64
     * Get information about all registered devices for your application.
65
     *
66
     * Application auth key must be set.
67
     *
68
     * @param int $limit  How many devices to return. Max is 300. Default is 300
69
     * @param int $offset Result offset. Default is 0. Results are sorted by id
70
     *
71
     * @return array
72
     */
73 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...
74
    {
75
        $query = [
76
            'limit' => max(1, min(self::DEVICES_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
77
            'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
78
            'app_id' => $this->api->getConfig()->getApplicationId(),
79
        ];
80
81
        return $this->api->request('GET', '/players?'.http_build_query($query), [
82
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
83
        ]);
84
    }
85
86
    /**
87
     * Register a device for your application.
88
     *
89
     * @param array $data Device data
90
     *
91
     * @return array
92
     */
93
    public function add(array $data)
94
    {
95
        $data = $this->resolverFactory->createNewDeviceResolver()->resolve($data);
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->resolverFactory->createExistingDeviceResolver()->resolve($data);
111
112
        return $this->api->request('PUT', '/players/'.$id, [], json_encode($data));
113
    }
114
115
    /**
116
     * Delete existing registered device from your application.
117
     *
118
     * OneSignal supports DELETE on the players API endpoint which is not documented in their official documentation
119
     * Reference: https://documentation.onesignal.com/docs/handling-personal-data#section-deleting-users-or-other-data-from-onesignal
120
     *
121
     * Application auth key must be set.
122
     *
123
     * @param string $id Device ID
124
     *
125
     * @return array
126
     */
127 View Code Duplication
    public function delete($id)
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...
128
    {
129
        $query = [
130
            'app_id' => $this->api->getConfig()->getApplicationId(),
131
        ];
132
133
        return $this->api->request('DELETE', '/players/'.$id.'?'.http_build_query($query), [
134
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
135
        ]);
136
    }
137
138
    /**
139
     * Call on new device session in your app.
140
     *
141
     * @param string $id   Device ID
142
     * @param array  $data Device data
143
     *
144
     * @return array
145
     */
146
    public function onSession($id, array $data)
147
    {
148
        $data = $this->resolverFactory->createDeviceSessionResolver()->resolve($data);
149
150
        return $this->api->request('POST', '/players/'.$id.'/on_session', [], json_encode($data));
151
    }
152
153
    /**
154
     * Track a new purchase.
155
     *
156
     * @param string $id   Device ID
157
     * @param array  $data Device data
158
     *
159
     * @return array
160
     */
161
    public function onPurchase($id, array $data)
162
    {
163
        $data = $this->resolverFactory->createDevicePurchaseResolver()->resolve($data);
164
165
        return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
166
    }
167
168
    /**
169
     * Increment the device's total session length.
170
     *
171
     * @param string $id   Device ID
172
     * @param array  $data Device data
173
     *
174
     * @return array
175
     */
176
    public function onFocus($id, array $data)
177
    {
178
        $data = $this->resolverFactory->createDeviceFocusResolver()->resolve($data);
179
180
        return $this->api->request('POST', '/players/'.$id.'/on_focus', [], json_encode($data));
181
    }
182
183
    /**
184
     * Export all information about devices in a CSV format for your application.
185
     *
186
     * Application auth key must be set.
187
     *
188
     * @param array $extraFields Additional fields that you wish to include.
189
     *                           Currently supports: "location", "country", "rooted"
190
     *
191
     * @return array
192
     */
193
    public function csvExport(array $extraFields = [])
194
    {
195
        $url = '/players/csv_export?app_id='.$this->api->getConfig()->getApplicationId();
196
197
        $headers = [
198
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
199
        ];
200
201
        $body = [
202
            'extra_fields' => $extraFields,
203
        ];
204
205
        return $this->api->request('POST', $url, $headers, json_encode($body));
206
    }
207
}
208