Pakkelabels   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 41.54%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 17
c 9
b 0
f 0
lcom 1
cbo 1
dl 0
loc 262
ccs 27
cts 65
cp 0.4154
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getPickupPoints() 0 4 1
A pdkDroppoints() 0 4 1
A importedShipments() 0 5 1
A createImportedShipment() 0 5 1
A createShipment() 0 5 1
A freightRates() 0 5 1
A paymentRequests() 0 5 1
A glsDroppoints() 0 5 1
B makeApiCall() 0 26 3
A login() 0 9 1
A getToken() 0 4 1
A __construct() 0 6 1
A balance() 0 5 1
A pdf() 0 5 1
A shipments() 0 5 1
1
<?php
2
/**
3
 * Pakkelabels
4
 *
5
 * PHP version 5
6
 *
7
 * @category  Pakkelabels
8
 * @package   Pakkelabels
9
 * @author    Lars Olesen <[email protected]>
10
 * @author    Hasse Ramlev Hansen <[email protected]>
11
 * @copyright 2015 Lars Olesen
12
 * @license   MIT Open Source License https://opensource.org/licenses/MIT
13
 * @version   GIT: <git_id>
14
 * @link      http://github.com/discimport/pakkelabels-dk
15
 */
16
namespace Pakkelabels;
17
18
use Pakkelabels\Exception\PakkelabelsException;
19
20
/**
21
 * Class Pakkelabels
22
 *
23
 * Usage:
24
 * ----------------
25
 * The first thing required is to login
26
 * $label = new Pakkelabels('api_user', 'api_key');
27
 *
28
 * This will login and fetch the required token.
29
 * The token is then automatically added to any subsequent calls.
30
 *
31
 * To see the generated token you can use:
32
 * echo $label->getToken();
33
 *
34
 * Examples:
35
 * ----------------
36
 * // Get all Post Danmark labels shipped to Denmark
37
 * $labels = $label->shipments(array('shipping_agent' => 'pdk', 'receiver_country' => 'DK'));
38
 *
39
 * // Display the PDF for a specific label
40
 * $base64 = $label->pdf(31629);
41
 * $pdf = base64_decode($base64);
42
 * header('Content-type: application/pdf');
43
 * header('Content-Disposition: inline; filename="label.pdf"');
44
 * echo $pdf;
45
 *
46
 * @category  Pakkelabels
47
 * @package   Pakkelabels
48
 * @author    Lars Olesen <[email protected]>
49
 * @copyright 2015 Lars Olesen
50
 * @license   http://opensource.org/licenses/bsd-license.php New BSD License
51
 * @link      http://github.com/discimport/pakkelabels-dk
52
 */
53
54
class Pakkelabels
55
{
56
57
    /**
58
     * API Endpoint URL
59
     *
60
     * @var string
61
     */
62
    const API_ENDPOINT = 'https://app.pakkelabels.dk/api/public/v2';
63
64
    /**
65
     * API Endpoint URL
66
     *
67
     * @var string
68
     */
69
    const VERSION = '1.1';
70
71
    /**
72
     * API user
73
     *
74
     * @var string
75
     */
76
    protected $api_user;
77
78
    /**
79
     * API key
80
     *
81
     * @var string
82
     */
83
    protected $api_key;
84
85
    /**
86
     * Token
87
     *
88
     * @var string
89
     */
90
    protected $token;
91
92
    /**
93
     * Constructor
94
     *
95
     * @param string $api_user
96
     * @param string $api_key
97
     *
98
     * @throws \PakkelabelsException
99
     */
100 1
    public function __construct($api_user, $api_key)
101
    {
102 1
        $this->api_user = $api_user;
103 1
        $this->api_key = $api_key;
104 1
        $this->login();
105
    }
106
107
    /**
108
     * Login
109
     *
110
     * @return void
111
     * @throws \PakkelabelsException
112
     */
113 1
    protected function login()
114
    {
115 1
        $result = $this->makeApiCall(
116 1
            'users/login',
117 1
            true,
118 1
            array('api_user' => $this->api_user, 'api_key' => $this->api_key)
119 1
        );
120
        $this->token = $result['token'];
121
    }
122
123
    /**
124
     * Get balance
125
     *
126
     * @return void
127
     * @throws \PakkelabelsException
128
     */
129
    public function balance()
130
    {
131
        $result = $this->makeApiCall('users/balance');
132
        return $result['balance'];
133
    }
134
135
    /**
136
     * Get PDF
137
     *
138
     * @return base64 encoded string
139
     * @throws \PakkelabelsException
140
     */
141
    public function pdf($id)
142
    {
143
        $result = $this->makeApiCall('shipments/pdf', false, array('id' => $id));
144
        return $result['base64'];
145
    }
146
147
    /**
148
     * Get pickup points.
149
     *
150
     * @param array $params
151
     *
152
     * @return mixed
153
     * @throws \PakkelabelsException
154
     */
155
    public function getPickupPoints($params = array())
156
    {
157
        return $this->makeApiCall('pickup_points', false, $params);
158
    }
159
160
    /**
161
     * Search shipments
162
     *
163
     * @param array $params
164
     *
165
     * @return mixed
166
     * @throws \PakkelabelsException
167
     */
168
    public function shipments($params = array())
169
    {
170
        $result = $this->makeApiCall('shipments/shipments', false, $params);
171
        return $result;
172
    }
173
174
    /**
175
     * Get imported shipments
176
     *
177
     * @param array $params
178
     *
179
     * @return mixed
180
     * @throws \PakkelabelsException
181
     */
182
    public function importedShipments($params = array())
183
    {
184
        $result = $this->makeApiCall('shipments/imported_shipments', false, $params);
185
        return $result;
186
    }
187
188
    /**
189
     * Create imported shipment
190
     *
191
     * @param array $params
192
     *
193
     * @return mixed
194
     * @throws \PakkelabelsException
195
     */
196
    public function createImportedShipment($params)
197
    {
198
        $result = $this->makeApiCall('shipments/imported_shipment', true, $params);
199
        return $result;
200
    }
201
202
    /**
203
     * Create shipment
204
     *
205
     * @param array $params
206
     *
207
     * @return mixed
208
     * @throws \PakkelabelsException
209
     */
210
    public function createShipment($params)
211
    {
212
        $result = $this->makeApiCall('shipments/shipment', true, $params);
213
        return $result;
214
    }
215
216
    /**
217
     * Get freight rates
218
     *
219
     * @param array $params
220
     *
221
     * @return mixed
222
     * @throws \PakkelabelsException
223
     */
224
    public function freightRates($params = array())
225
    {
226
        $result = $this->makeApiCall('shipments/freight_rates', false, $params);
227
        return $result;
228
    }
229
230
    /**
231
     * Get payment requests
232
     *
233
     * @return mixed
234
     * @throws \PakkelabelsException
235
     */
236
    public function paymentRequests()
237
    {
238
        $result = $this->makeApiCall('users/payment_requests');
239
        return $result;
240
    }
241
242
    /**
243
     * Get GLS Droppoints
244
     *
245
     * @param array $params
246
     *
247
     * @return mixed
248
     * @throws \PakkelabelsException
249
     */
250
    public function glsDroppoints($params)
251
    {
252
        $result = $this->makeApiCall('shipments/gls_droppoints', false, $params);
253
        return $result;
254
    }
255
256
    /**
257
     * Get PostDK Droppoints
258
     *
259
     * @param array $params
260
     *
261
     * @return mixed
262
     * @throws \PakkelabelsException
263
     */
264
    public function pdkDroppoints($params)
265
    {
266
        return $this->makeApiCall('shipments/pdk_droppoints', false, $params);
267
    }
268
269
    /**
270
     * Get token
271
     *
272
     * @return string
273
     */
274
    public function getToken()
275
    {
276
        return $this->token;
277
    }
278
279
    /**
280
     * Make API Call
281
     *
282
     * @param string  $method
283
     * @param boolean $doPost
284
     * @param array   $params
285
     *
286
     * @return mixed
287
     * @throws \PakkelabelsException
288
     */
289 1
    protected function makeApiCall($method, $doPost = false, $params = array())
290
    {
291 1
        $ch = curl_init();
292 1
        $params['token'] = $this->token;
293 1
        $params['user_agent'] = 'pdk_php_library v' . self::VERSION;
294
295 1
        $query = http_build_query($params);
296 1
        if ($doPost === true) {
297 1
            curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method);
298 1
            curl_setopt($ch, CURLOPT_POST, 1);
299 1
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
300 1
        } else {
301
            curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method . '?' . $query);
302
        }
303 1
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
304
305 1
        $output = curl_exec($ch);
306 1
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
307 1
        curl_close($ch);
308
309 1
        $output = json_decode($output, true);
310 1
        if ($http_code != 200) {
311 1
            throw new PakkelabelsException($output['message']);
312
        }
313
        return $output;
314
    }
315
}
316