Completed
Push — master ( b6f3d7...bfda35 )
by Tomas
04:37
created

Devices::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

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