EMSTracker::getLanguage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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