Devices::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace vasadibt\onesignal\service;
4
5
use vasadibt\onesignal\resolver\ResolverFactory;
6
7
/**
8
 * Class Devices
9
 * @package vasadibt\onesignal\service
10
 */
11
class Devices extends Request
12
{
13
    const DEVICES_LIMIT = 300;
14
15
    const IOS = 0;
16
    const ANDROID = 1;
17
    const AMAZON = 2;
18
    const WINDOWS_PHONE = 3;
19
    const WINDOWS_PHONE_MPNS = 3;
20
    const CHROME_APP = 4;
21
    const CHROME_WEB = 5;
22
    const WINDOWS_PHONE_WNS = 6;
23
    const SAFARI = 7;
24
    const FIREFOX = 8;
25
    const MACOS = 9;
26
    const ALEXA = 10;
27
    const EMAIL = 11;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public $methodName = self::PLAYERS;
33
34
    /**
35
     * Get information about device with provided ID.
36
     *
37
     * @param string $id Device ID
38
     *
39
     * @return array
40
     * @throws \GuzzleHttp\Exception\GuzzleException
41
     * @throws \yii\web\BadRequestHttpException
42
     */
43
    public function getOne($id)
44
    {
45
        return $this->get($id);
46
    }
47
48
    /**
49
     * Get information about all registered devices for your application.
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
     * @throws \GuzzleHttp\Exception\GuzzleException
56
     * @throws \yii\web\BadRequestHttpException
57
     */
58
    public function getAll($limit = self::DEVICES_LIMIT, $offset = 0)
59
    {
60
        return $this->get(null, [
61
            'limit' => max(1, min(self::DEVICES_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
62
            'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
63
        ]);
64
    }
65
66
    /**
67
     * Register a device for your application.
68
     *
69
     * @param array $data Device data
70
     *
71
     * @return array
72
     * @throws \GuzzleHttp\Exception\GuzzleException
73
     * @throws \yii\web\BadRequestHttpException
74
     */
75
    public function add(array $data)
76
    {
77
        return $this->post(null, ResolverFactory::createNewDeviceResolver($this->api)->resolve($data));
78
    }
79
80
    /**
81
     * Update existing registered device for your application with provided data.
82
     *
83
     * @param string $id Device ID
84
     * @param array $data New device data
85
     *
86
     * @return array
87
     * @throws \GuzzleHttp\Exception\GuzzleException
88
     * @throws \yii\web\BadRequestHttpException
89
     */
90
    public function update($id, array $data)
91
    {
92
        return $this->put($id, ResolverFactory::createExistingDeviceResolver($this->api)->resolve($data));
93
    }
94
95
    /**
96
     * Call on new device session in your app.
97
     *
98
     * @param string $id Device ID
99
     * @param array $data Device data
100
     *
101
     * @return array
102
     * @throws \GuzzleHttp\Exception\GuzzleException
103
     * @throws \yii\web\BadRequestHttpException
104
     */
105
    public function onSession($id, array $data)
106
    {
107
        return $this->post($id . '/on_session', ResolverFactory::createDeviceSessionResolver()->resolve($data));
108
    }
109
110
    /**
111
     * Track a new purchase.
112
     *
113
     * @param string $id Device ID
114
     * @param array $data Device data
115
     *
116
     * @return array
117
     * @throws \GuzzleHttp\Exception\GuzzleException
118
     * @throws \yii\web\BadRequestHttpException
119
     */
120
    public function onPurchase($id, array $data)
121
    {
122
        return $this->post($id . '/on_purchase', ResolverFactory::createDevicePurchaseResolver()->resolve($data));
123
    }
124
125
    /**
126
     * Increment the device's total session length.
127
     *
128
     * @param string $id Device ID
129
     * @param array $data Device data
130
     *
131
     * @return array
132
     * @throws \GuzzleHttp\Exception\GuzzleException
133
     * @throws \yii\web\BadRequestHttpException
134
     */
135
    public function onFocus($id, array $data)
136
    {
137
        return $this->post($id . '/on_focus', ResolverFactory::createDeviceFocusResolver()->resolve($data));
138
    }
139
140
    /**
141
     * Export all information about devices in a CSV format for your application.
142
     *
143
     * @param array $extraFields Additional fields that you wish to include.
144
     *                           Currently supports: "location", "country", "rooted"
145
     *
146
     * @return array
147
     * @throws \GuzzleHttp\Exception\GuzzleException
148
     * @throws \yii\web\BadRequestHttpException
149
     */
150
    public function csvExport(array $extraFields = [])
151
    {
152
        return $this->post('csv_export', ['extra_fields' => $extraFields]);
153
    }
154
}
155