1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slince shipment tracker library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Slince\ShipmentTracking\YanWenExpress; |
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\TrackException; |
13
|
|
|
use Slince\ShipmentTracking\HttpAwareTracker; |
14
|
|
|
use Slince\ShipmentTracking\ShipmentEvent; |
15
|
|
|
|
16
|
|
|
class YanWenTracker extends HttpAwareTracker |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const TRACKING_ENDPOINT = 'http://api.track.yw56.com.cn/v2/api/Tracking'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $culture; |
32
|
|
|
|
33
|
|
|
public function __construct($key = 'none', $culture = 'en', HttpClient $httpClient = null) |
34
|
|
|
{ |
35
|
|
|
$this->key = $key; |
36
|
|
|
$this->culture = $culture; |
37
|
|
|
$httpClient && $this->setHttpClient($httpClient); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getKey() |
44
|
|
|
{ |
45
|
|
|
return $this->key; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $key |
50
|
|
|
* @return YanWenTracker |
51
|
|
|
*/ |
52
|
|
|
public function setKey($key) |
53
|
|
|
{ |
54
|
|
|
$this->key = $key; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getCulture() |
62
|
|
|
{ |
63
|
|
|
return $this->culture; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $culture |
68
|
|
|
* @return YanWenTracker |
69
|
|
|
*/ |
70
|
|
|
public function setCulture($culture) |
71
|
|
|
{ |
72
|
|
|
$this->culture = $culture; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function track($trackingNumber) |
80
|
|
|
{ |
81
|
|
|
$parameters = [ |
82
|
|
|
'key' => $this->key, |
83
|
|
|
'culture' => $this->culture, |
84
|
|
|
'tracking_number' => $trackingNumber |
85
|
|
|
]; |
86
|
|
|
$request = new Request('GET', static::TRACKING_ENDPOINT); |
87
|
|
|
$json = $this->sendRequest($request, [ |
88
|
|
|
'query' => $parameters |
89
|
|
|
]); |
90
|
|
|
if ($json['code'] != 0) { |
91
|
|
|
throw new TrackException(sprintf('Bad response with code "%d"', $json['code'])); |
92
|
|
|
} |
93
|
|
|
return static::buildShipment($json); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return HttpClient |
98
|
|
|
* @codeCoverageIgnore |
99
|
|
|
*/ |
100
|
|
|
protected function getHttpClient() |
101
|
|
|
{ |
102
|
|
|
if (!is_null($this->httpClient)) { |
103
|
|
|
return $this->httpClient; |
104
|
|
|
} |
105
|
|
|
return $this->httpClient = new HttpClient(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param RequestInterface $request |
110
|
|
|
* @param array $options |
111
|
|
|
* @return array |
112
|
|
|
* @codeCoverageIgnore |
113
|
|
|
*/ |
114
|
|
|
protected function sendRequest(RequestInterface $request, array $options = []) |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
|
|
$response = $this->getHttpClient()->send($request, $options); |
118
|
|
|
return \GuzzleHttp\json_decode((string)$response->getBody(), true); |
119
|
|
|
} catch (GuzzleException $exception) { |
120
|
|
|
throw new TrackException($exception->getMessage()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $json |
126
|
|
|
* @return Shipment |
127
|
|
|
*/ |
128
|
|
|
protected static function buildShipment($json) |
129
|
|
|
{ |
130
|
|
|
if (is_null($json['origin_items']) && is_null($json['destin_items'])) { |
131
|
|
|
throw new TrackException(sprintf('Bad response')); |
132
|
|
|
} |
133
|
|
|
$shippingItems = array_merge($json['origin_items'], $json['destin_items']); |
134
|
|
|
$events = array_map(function($item) { |
135
|
|
|
return ShipmentEvent::fromArray([ |
136
|
|
|
'location' => $item['location'], |
137
|
|
|
'description' => $item['message'], |
138
|
|
|
'date' => $item['timestamp'], |
139
|
|
|
'status' => null |
140
|
|
|
]); |
141
|
|
|
}, $shippingItems); |
142
|
|
|
$shipment = new Shipment($events); |
143
|
|
|
|
144
|
|
|
//build origin events |
145
|
|
|
$shipment->setOriginEvents(array_map(function($item) { |
146
|
|
|
return ShipmentEvent::fromArray([ |
147
|
|
|
'location' => $item['location'], |
148
|
|
|
'description' => $item['message'], |
149
|
|
|
'date' => $item['timestamp'], |
150
|
|
|
'status' => null |
151
|
|
|
]); |
152
|
|
|
}, (array)$json['origin_items'])); |
153
|
|
|
|
154
|
|
|
//build destination events |
155
|
|
|
$shipment->setDestinationEvents(array_map(function($item) { |
156
|
|
|
return ShipmentEvent::fromArray([ |
157
|
|
|
'location' => $item['location'], |
158
|
|
|
'description' => $item['message'], |
159
|
|
|
'date' => $item['timestamp'], |
160
|
|
|
'status' => null |
161
|
|
|
]); |
162
|
|
|
}, (array)$json['destin_items'])); |
163
|
|
|
|
164
|
|
|
$shipment->setIsDelivered($json['state'] == 40) |
165
|
|
|
->setOrigin($json['origin_country']) |
166
|
|
|
->setDestination($json['destin_country']) |
167
|
|
|
->setDeliveredAt($json['receive_date']); |
168
|
|
|
return $shipment; |
169
|
|
|
} |
170
|
|
|
} |