EMSTracker   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 11
dl 0
loc 150
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getLanguage() 0 4 1
A setLanguage() 0 8 3
A getAuthenticate() 0 4 1
A setAuthenticate() 0 5 1
A setVersion() 0 4 1
A getVersion() 0 4 1
A track() 0 13 3
A sendRequest() 0 11 2
A formatEndpoint() 0 4 1
A buildShipment() 0 16 3
1
<?php
2
/**
3
 * Slince shipment tracker library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\ShipmentTracking\EMS;
7
8
use Carbon\Carbon;
9
use GuzzleHttp\Client as HttpClient;
10
use GuzzleHttp\Psr7\Request;
11
use GuzzleHttp\Exception\GuzzleException;
12
use Psr\Http\Message\RequestInterface;
13
use Slince\ShipmentTracking\Foundation\Exception\InvalidArgumentException;
14
use Slince\ShipmentTracking\Foundation\Exception\TrackException;
15
use Slince\ShipmentTracking\Foundation\HttpAwareTracker;
16
use Slince\ShipmentTracking\Foundation\Shipment;
17
use Slince\ShipmentTracking\Foundation\ShipmentEvent;
18
use Slince\ShipmentTracking\Utility;
19
20
class EMSTracker extends HttpAwareTracker
21
{
22
    /**
23
     * @var string
24
     */
25
    const TRACKING_ENDPOINT = 'http://shipping.ems.com.cn/partner/api/public/p/track/query/{language}/{trackingNumber}';
26
27
    /**
28
     * @var string
29
     */
30
    protected $language;
31
32
    /**
33
     * @var string
34
     */
35
    protected static $version = 'international_eub_us_1.1';
36
37
    /**
38
     * @var string
39
     */
40
    protected $authenticate;
41
42
    public function __construct($authenticate, $language, HttpClient $httpClient = null)
43
    {
44
        $this->authenticate = $authenticate;
45
        $this->setLanguage($language);
46
        $httpClient && $this->setHttpClient($httpClient);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getLanguage()
53
    {
54
        return $this->language;
55
    }
56
57
    /**
58
     * @param string $language
59
     * @return EMSTracker
60
     */
61
    public function setLanguage($language)
62
    {
63
        if ($language != 'en' && $language != 'cn') {
64
            throw new InvalidArgumentException(sprintf('Invalid language, expect "cn" or "en", giving "%s"', $language));
65
        }
66
        $this->language = $language;
67
        return $this;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getAuthenticate()
74
    {
75
        return $this->authenticate;
76
    }
77
78
    /**
79
     * @param string $authenticate
80
     * @return EMSTracker
81
     */
82
    public function setAuthenticate($authenticate)
83
    {
84
        $this->authenticate = $authenticate;
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $version
90
     */
91
    public static function setVersion($version)
92
    {
93
        static::$version = $version;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public static function getVersion()
100
    {
101
        return static::$version;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function track($trackingNumber)
108
    {
109
        $request = new Request('GET', static::formatEndpoint($this->language, $trackingNumber));
110
        try {
111
            $array = Utility::parseXml($this->sendRequest($request));
112
        } catch (InvalidArgumentException $exception) {
113
            throw new TrackException($exception->getMessage());
114
        }
115
        if (empty($array['trace'])) {
116
            throw new TrackException(sprintf('Bad response,code: "%s", message:"%s"', $array['code'], $array['description']));
117
        }
118
        return static::buildShipment($array);
119
    }
120
121
    /**
122
     * @param RequestInterface $request
123
     * @param array $options
124
     * @return string
125
     * @codeCoverageIgnore
126
     */
127
    protected function sendRequest(RequestInterface $request, array $options = [])
128
    {
129
        try {
130
            $request = $request->withHeader('version', static::$version)
131
                ->withHeader('authenticate', $this->authenticate);
132
            $response = $this->getHttpClient()->send($request, $options);
133
            return (string)$response->getBody();
134
        } catch (GuzzleException $exception) {
135
            throw new TrackException($exception->getMessage());
136
        }
137
    }
138
139
    /**
140
     * @param string $language
141
     * @param string $trackingNumber
142
     * @return string
143
     */
144
    protected static function formatEndpoint($language, $trackingNumber)
145
    {
146
        return str_replace(['{language}', '{trackingNumber}'], [$language, $trackingNumber], static::TRACKING_ENDPOINT);
147
    }
148
149
    /**
150
     * @param array $json
151
     * @return Shipment
152
     */
153
    protected static function buildShipment($json)
154
    {
155
        $trace = is_numeric(key($json['trace'])) ? $json['trace'] : [$json['trace']];
156
        $events = array_map(function($item) {
157
            return ShipmentEvent::fromArray([
158
                'location' => $item['acceptAddress'],
159
                'description' => $item['remark'],
160
                'date' => Carbon::parse($item['acceptTime']),
161
            ]);
162
        }, $trace);
163
        $shipment = new Shipment($events);
164
        if ($firstEvent = reset($events)) {
165
            $shipment->setDeliveredAt($firstEvent->getDate());
166
        }
167
        return $shipment;
168
    }
169
}