Completed
Push — master ( e66946...bb4136 )
by Lars
02:39
created

Pakkelabels::makeApiCall()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0105

Importance

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