Tracking::list()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 28
nc 4
nop 1
dl 0
loc 42
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
namespace DansMaCulotte\LaPoste;
4
5
use DansMaCulotte\LaPoste\Exceptions\Exception;
6
use DansMaCulotte\LaPoste\Resources\Status;
7
use GuzzleHttp\Exception\GuzzleException;
8
use GuzzleHttp\Exception\RequestException;
9
10
/**
11
 * Implementation of "Suivi" (Tracking) Web Service
12
 * https://developer.laposte.fr/products/suivi/latest
13
 */
14
class Tracking extends LaPoste
15
{
16
    /** @var string */
17
    const SERVICE_URI = '/suivi/v1';
18
19
    /**
20
     * Construct Method
21
     *
22
     * @param string $apiKey La Poste Developer API Key
23
     */
24
    public function __construct($apiKey)
25
    {
26
        parent::__construct($apiKey);
27
    }
28
29
    /**
30
     * Get the status of a shipping from his unique tracking code.
31
     *
32
     * @param string $code A shipping code
33
     *
34
     * @return Status
35
     * @throws Exception
36
     * @throws GuzzleException
37
     */
38
    public function track(string $code)
39
    {
40
        try {
41
            $response = $this->httpClient->request(
42
                'GET',
43
                self::SERVICE_URI,
44
                [
45
                    'query' => [
46
                        'code' => $code
47
                    ]
48
                ]
49
            );
50
51
            $body = json_decode((string) $response->getBody(), true);
52
53
            return new Status(
54
                $body['code'],
55
                $body['date'],
56
                $body['status'],
57
                $body['message'],
58
                $body['link'],
59
                $body['type']
60
            );
61
        } catch (RequestException $e) {
62
            $body = json_decode($e->getResponse()->getBody(), true);
63
            if (isset($body['message'])) {
64
                throw Exception::trackingError($body['message']);
65
            } else {
66
                throw Exception::trackingError('No error message were provided');
67
            }
68
        }
69
    }
70
71
    /**
72
     * Get the status of a list of shipping from their unique tracking code.
73
     *
74
     * @param array $codes An array of shipping codes
75
     *
76
     * @return array
77
     * @throws \GuzzleHttp\Exception\GuzzleException
78
     */
79
    public function list(array $codes)
80
    {
81
        $response = $this->httpClient->request(
82
            'GET',
83
            self::SERVICE_URI . '/list',
84
            [
85
                'query' => [
86
                    'codes' => implode(',', $codes)
87
                ]
88
            ]
89
        );
90
91
        $body = json_decode((string) $response->getBody(), true);
92
93
        $results = [];
94
        foreach ($body as $item) {
95
            if (isset($item['data'])) {
96
                $status = $item['data'];
97
                array_push(
98
                    $results,
99
                    new Status(
100
                        $status['code'],
101
                        $status['message'],
102
                        $status['date'],
103
                        $status['status'],
104
                        $status['link'],
105
                        $status['type']
106
                    )
107
                );
108
            } else if (isset($item['error'])) {
109
                $error = $item['error'];
110
                array_push(
111
                    $results,
112
                    new Status(
113
                        $error['code'],
114
                        $error['message']
115
                    )
116
                );
117
            }
118
        }
119
120
        return $results;
121
    }
122
}
123