Devices::onFocus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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