Completed
Pull Request — master (#105)
by
unknown
01:31
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
     * @param string $id   Device ID
119
     *
120
     * @return array
121
     */
122 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...
123
    {
124
        $query = [
125
            'app_id' => $this->api->getConfig()->getApplicationId(),
126
        ];
127
128
        return $this->api->request('DELETE', '/players/'.$id.'?'.http_build_query($query), [
129
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
130
        ]);
131
    }
132
    
133
    /**
134
     * Call on new device session in your app.
135
     *
136
     * @param string $id   Device ID
137
     * @param array  $data Device data
138
     *
139
     * @return array
140
     */
141
    public function onSession($id, array $data)
142
    {
143
        $data = $this->resolverFactory->createDeviceSessionResolver()->resolve($data);
144
145
        return $this->api->request('POST', '/players/'.$id.'/on_session', [], json_encode($data));
146
    }
147
148
    /**
149
     * Track a new purchase.
150
     *
151
     * @param string $id   Device ID
152
     * @param array  $data Device data
153
     *
154
     * @return array
155
     */
156
    public function onPurchase($id, array $data)
157
    {
158
        $data = $this->resolverFactory->createDevicePurchaseResolver()->resolve($data);
159
160
        return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
161
    }
162
163
    /**
164
     * Increment the device's total session length.
165
     *
166
     * @param string $id   Device ID
167
     * @param array  $data Device data
168
     *
169
     * @return array
170
     */
171
    public function onFocus($id, array $data)
172
    {
173
        $data = $this->resolverFactory->createDeviceFocusResolver()->resolve($data);
174
175
        return $this->api->request('POST', '/players/'.$id.'/on_focus', [], json_encode($data));
176
    }
177
178
    /**
179
     * Export all information about devices in a CSV format for your application.
180
     *
181
     * Application auth key must be set.
182
     *
183
     * @param array $extraFields Additional fields that you wish to include.
184
     *                           Currently supports: "location", "country", "rooted"
185
     *
186
     * @return array
187
     */
188
    public function csvExport(array $extraFields = [])
189
    {
190
        $url = '/players/csv_export?app_id='.$this->api->getConfig()->getApplicationId();
191
192
        $headers = [
193
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
194
        ];
195
196
        $body = [
197
            'extra_fields' => $extraFields,
198
        ];
199
200
        return $this->api->request('POST', $url, $headers, json_encode($body));
201
    }
202
}
203