1 | <?php |
||
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() |
||
107 | |||
108 | /** |
||
109 | * @param RequestInterface $request |
||
110 | * @param array $options |
||
111 | * @return array |
||
112 | * @codeCoverageIgnore |
||
113 | */ |
||
114 | protected function sendRequest(RequestInterface $request, array $options = []) |
||
123 | |||
124 | /** |
||
125 | * @param array $json |
||
126 | * @return Shipment |
||
127 | */ |
||
128 | protected static function buildShipment($json) |
||
170 | } |