ParcelTracking   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 31
dl 0
loc 70
rs 10
c 2
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatusByID() 0 33 3
A __construct() 0 3 1
1
<?php
2
3
namespace DansMaCulotte\Colissimo;
4
5
use DansMaCulotte\Colissimo\Exceptions\Exception;
6
use DansMaCulotte\Colissimo\Resources\ParcelStatus;
7
use SimpleXMLElement;
8
9
/**
10
 * Implementation of Parcel Tracking Web Service
11
 * https://www.colissimo.entreprise.laposte.fr/system/files/imagescontent/docs/spec_ws_suivi.pdf
12
 */
13
class ParcelTracking extends Colissimo
14
{
15
    /** @var string */
16
    const SERVICE_URL = 'https://www.coliposte.fr/tracking-chargeur-cxf/TrackingServiceWS/';
17
18
    /** @var array */
19
    const ERRORS = [
20
        101 => 'Invalid parcel ID',
21
        103 => 'Parcel ID older than 30 days',
22
        104 => 'Parcel ID outside of client range IDs',
23
        105 => 'Unknown parcel ID',
24
        201 => 'Invalid account number or password',
25
        202 => 'Request unauthorized for this account',
26
        1000 => 'Internal server error',
27
    ];
28
29
    /**
30
     * Construct Method
31
     *
32
     * @param array $credentials Contains login and password for authentication
33
     * @param array $options Guzzle client options
34
     */
35
    public function __construct(array $credentials, array $options = [])
36
    {
37
        parent::__construct($credentials, self::SERVICE_URL);
38
    }
39
    
40
    /**
41
     * Retrieve Parcel status by it's ID
42
     *
43
     * @param string $id Colissimo parcel number
44
     * @param array $options Additional parameters
45
     *
46
     * @return ParcelStatus
47
     * @throws Exception
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     */
50
    public function getStatusByID(string $id, array $options = [])
51
    {
52
        $options = array_merge(
53
            [
54
                'skybillNumber' => $id,
55
            ],
56
            $options
57
        );
58
59
        $response = $this->httpRequest(
60
            'track',
61
            $options
62
        );
63
64
        $xml = new SimpleXMLElement((string) $response->getBody());
65
66
        $return = $xml->xpath('//return');
67
        if (count($return) && $return[0]->errorCode != 0) {
68
            $this->parseErrorCodeAndThrow((int) $return[0]->errorCode, self::ERRORS);
69
        }
70
71
        $parcelStatus = new ParcelStatus(
72
            $return[0]->skybillNumber,
73
            $return[0]->eventCode,
74
            $return[0]->eventDate,
75
            $return[0]->eventLibelle,
76
            $return[0]->eventSite,
77
            $return[0]->recipientCity,
78
            $return[0]->recipientZipCode,
79
            $return[0]->recipientCountryCode
80
        );
81
82
        return $parcelStatus;
83
    }
84
}
85