Completed
Push — master ( 41961e...cadd2d )
by Lars
01:57
created

Pakkelabels::make_api_call()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 22
nc 6
nop 3
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   http://opensource.org/licenses/bsd-license.php New BSD License
12
 * @version   GIT: <git_id>
13
 * @link      http://github.com/discimport/pakkelabels-dk
14
 */
15
namespace Pakkelabels;
16
17
use Pakkelabels\Exception\Pakkelabels_Exception;
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/v1';
62
63
    /**
64
     * API user
65
     *
66
     * @var string
67
     */
68
    protected $api_user;
69
70
    /**
71
     * API key
72
     *
73
     * @var string
74
     */
75
    protected $api_key;
76
77
    /**
78
     * Token
79
     *
80
     * @var string
81
     */
82
    protected $token;
83
84
    /**
85
     * Constructor
86
     *
87
     * @param string $api_user
88
     * @param string $api_key
89
     *
90
     * @return mixed
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
91
     * @throws \Pakkelabels_Exception
92
     */
93
    public function __construct($api_user, $api_key)
94
    {
95
        $this->api_user = $api_user;
96
        $this->api_key = $api_key;
97
        $this->login();
98
    }
99
100
    /**
101
     * Login
102
     *
103
     * @return void
104
     * @throws \Pakkelabels_Exception
105
     */
106
    protected function login()
107
    {
108
        $result = $this->make_api_call('users/login', true, array('api_user' => $this->api_user, 'api_key' => $this->api_key));
109
        $this->token = $result['token'];
110
    }
111
112
    /**
113
     * Get balance
114
     *
115
     * @return void
116
     * @throws \Pakkelabels_Exception
117
     */
118
    public function balance()
119
    {
120
        $result = $this->make_api_call('users/balance');
121
        return $result['balance'];
122
    }
123
124
    /**
125
     * Get PDF
126
     *
127
     * @return base64 encoded string
128
     * @throws \Pakkelabels_Exception
129
     */
130
    public function pdf($id)
131
    {
132
        $result = $this->make_api_call('shipments/pdf', false, array('id' => $id));
133
        return $result['base64'];
134
    }
135
136
    /**
137
     * Search shipments
138
     *
139
     * @param array $params
140
     *
141
     * @return mixed
142
     * @throws \Pakkelabels_Exception
143
     */
144
    public function shipments($params = array())
145
    {
146
        $result = $this->make_api_call('shipments/shipments', false, $params);
147
        return $result;
148
    }
149
150
    /**
151
     * Get imported shipments
152
     *
153
     * @param array $params
154
     *
155
     * @return mixed
156
     * @throws \Pakkelabels_Exception
157
     */
158
    public function imported_shipments($params = array())
159
    {
160
        $result = $this->make_api_call('shipments/imported_shipments', false, $params);
161
        return $result;
162
    }
163
164
    /**
165
     * Create imported shipment
166
     *
167
     * @param array $params
168
     *
169
     * @return mixed
170
     * @throws \Pakkelabels_Exception
171
     */
172
    public function create_imported_shipment($params)
173
    {
174
        $result = $this->make_api_call('shipments/imported_shipment', true, $params);
175
        return $result;
176
    }
177
178
    /**
179
     * Create shipment
180
     *
181
     * @param array $params
182
     *
183
     * @return mixed
184
     * @throws \Pakkelabels_Exception
185
     */
186
    public function create_shipment($params)
187
    {
188
        $result = $this->make_api_call('shipments/shipment', true, $params);
189
        return $result;
190
    }
191
192
    /**
193
     * Get freight rates
194
     *
195
     * @return mixed
196
     * @throws \Pakkelabels_Exception
197
     */
198
    public function freight_rates()
199
    {
200
        $result = $this->make_api_call('shipments/freight_rates');
201
        return $result;
202
    }
203
204
    /**
205
     * Get payment requests
206
     *
207
     * @return mixed
208
     * @throws \Pakkelabels_Exception
209
     */
210
    public function payment_requests()
211
    {
212
        $result = $this->make_api_call('users/payment_requests');
213
        return $result;
214
    }
215
216
    /**
217
     * Get GLS Droppoints
218
     *
219
     * @param array $params
220
     *
221
     * @return mixed
222
     * @throws \Pakkelabels_Exception
223
     */
224
    public function gls_droppoints($params)
225
    {
226
        $result = $this->make_api_call('shipments/gls_droppoints', false, $params);
227
        return $result;
228
    }
229
230
    /**
231
     * Get token
232
     *
233
     * @return string
234
     */
235
    public function getToken()
236
    {
237
        return $this->token;
238
    }
239
240
    /**
241
     * Make API Call
242
     *
243
     * @param string  $method
244
     * @param boolean $doPost
245
     * @param array   $params
246
     *
247
     * @return mixed
248
     * @throws \Pakkelabels_Exception
249
     */
250
    protected function make_api_call($method, $doPost = false, $params = array())
251
    {
252
        $ch = curl_init();
253
        $params['token'] = $this->token;
254
255
        $query = http_build_query($params);
256
        if ($doPost) {
257
            curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method);
258
            curl_setopt($ch, CURLOPT_POST, 1);
259
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
260
        } else {
261
            curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . $method . '?' . $query);
262
        }
263
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
264
265
        $output = curl_exec($ch);
266
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
267
        curl_close($ch);
268
269
        $output = json_decode($output, true);
270
        if ($http_code != 200) {
271
            if (is_array($output['message'])) {
272
                print_r($output['message']);
273
                die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method make_api_call() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
274
            } else {
275
                throw new Pakkelabels_Exception($output['message']);
276
            }
277
        }
278
        return $output;
279
    }
280
}
281