Completed
Pull Request — master (#74)
by
unknown
04:19
created

Devices::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
    const ANDROID = 1;
13
    const AMAZON = 2;
14
    const WINDOWS_PHONE = 3;
15
    const WINDOWS_PHONE_MPNS = 3;
16
    const CHROME_APP = 4;
17
    const CHROME_WEB = 5;
18
    const WINDOWS_PHONE_WNS = 6;
19
    const SAFARI = 7;
20
    const FIREFOX = 8;
21
    const MACOS = 9;
22
23
    protected $api;
24
25
    /**
26
     * @var ResolverFactory
27
     */
28
    private $resolverFactory;
29
30
    public function __construct(OneSignal $api, ResolverFactory $resolverFactory)
31
    {
32
        $this->api = $api;
33
        $this->resolverFactory = $resolverFactory;
34
    }
35
36
    /**
37
     * Get information about device with provided ID.
38
     *
39
     * @param string $id Device ID
40
     *
41
     * @return array
42
     */
43
    public function getOne($id)
44
    {
45
        $query = [
46
            'app_id' => $this->api->getConfig()->getApplicationId(),
47
        ];
48
49
        return $this->api->request('GET', '/players/'.$id.'?'.http_build_query($query));
50
    }
51
52
    /**
53
     * Get information about all registered devices for your application.
54
     *
55
     * Application auth key must be set.
56
     *
57
     * @param int $limit  How many devices to return. Max is 300. Default is 300
58
     * @param int $offset Result offset. Default is 0. Results are sorted by id
59
     *
60
     * @return array
61
     */
62 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...
63
    {
64
        $query = [
65
            'limit' => max(1, min(self::DEVICES_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
66
            'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
67
            'app_id' => $this->api->getConfig()->getApplicationId(),
68
        ];
69
70
        return $this->api->request('GET', '/players?'.http_build_query($query), [
71
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
72
        ]);
73
    }
74
75
    /**
76
     * Register a device for your application.
77
     *
78
     * @param array $data Device data
79
     *
80
     * @return array
81
     */
82
    public function add(array $data)
83
    {
84
        $data = $this->resolverFactory->createNewDeviceResolver()->resolve($data);
85
86
        return $this->api->request('POST', '/players', [], json_encode($data));
87
    }
88
89
    /**
90
     * Update existing registered device for your application with provided data.
91
     *
92
     * @param string $id   Device ID
93
     * @param array  $data New device data
94
     *
95
     * @return array
96
     */
97
    public function update($id, array $data)
98
    {
99
        $data = $this->resolverFactory->createExistingDeviceResolver()->resolve($data);
100
101
        return $this->api->request('PUT', '/players/'.$id, [], json_encode($data));
102
    }
103
104
    /**
105
     * Call on new device session in your app.
106
     *
107
     * @param string $id   Device ID
108
     * @param array  $data Device data
109
     *
110
     * @return array
111
     */
112
    public function onSession($id, array $data)
113
    {
114
        $data = $this->resolverFactory->createDeviceSessionResolver()->resolve($data);
115
116
        return $this->api->request('POST', '/players/'.$id.'/on_session', [], json_encode($data));
117
    }
118
119
    /**
120
     * Track a new purchase.
121
     *
122
     * @param string $id   Device ID
123
     * @param array  $data Device data
124
     *
125
     * @return array
126
     */
127
    public function onPurchase($id, array $data)
128
    {
129
        $data = $this->resolverFactory->createDevicePurchaseResolver()->resolve($data);
130
131
        return $this->api->request('POST', '/players/'.$id.'/on_purchase', [], json_encode($data));
132
    }
133
134
    /**
135
     * Increment the device's total session length.
136
     *
137
     * @param string $id   Device ID
138
     * @param array  $data Device data
139
     *
140
     * @return array
141
     */
142
    public function onFocus($id, array $data)
143
    {
144
        $data = $this->resolverFactory->createDeviceFocusResolver()->resolve($data);
145
146
        return $this->api->request('POST', '/players/'.$id.'/on_focus', [], json_encode($data));
147
    }
148
149
    /**
150
     * Export all information about devices in a CSV format for your application.
151
     *
152
     * Application auth key must be set.
153
     *
154
     * @param array $extraFields Additional fields that you wish to include.
155
     *                           Currently supports: "location", "country", "rooted"
156
     *
157
     * @return array
158
     */
159
    public function csvExport(array $extraFields = [])
160
    {
161
        $url = '/players/csv_export?app_id='.$this->api->getConfig()->getApplicationId();
162
163
        $headers = [
164
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
165
        ];
166
167
        $body = [
168
            'extra_fields' => $extraFields,
169
        ];
170
171
        return $this->api->request('POST', $url, $headers, json_encode($body));
172
    }
173
}
174