1 | <?php |
||
16 | class USPSTracker extends HttpAwareTracker |
||
17 | { |
||
18 | const TRACKING_ENDPOINT = 'http://production.shippingapis.com/ShippingAPI.dll'; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $userId; |
||
24 | |||
25 | /** |
||
26 | * You can get your userID from the following url |
||
27 | * {@link https://www.usps.com/business/web-tools-apis/welcome.htm} |
||
28 | */ |
||
29 | public function __construct($userId, HttpClient $httpClient = null) |
||
34 | |||
35 | /** |
||
36 | * @return string |
||
37 | */ |
||
38 | public function getUserId() |
||
42 | |||
43 | /** |
||
44 | * @param string $userId |
||
45 | * @return USPSTracker |
||
46 | */ |
||
47 | public function setUserId($userId) |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function track($trackingNumber) |
||
57 | { |
||
58 | try { |
||
59 | $response = $this->request([ |
||
60 | 'query' => [ |
||
61 | 'API' => 'TrackV2', |
||
62 | 'XML' => static::buildXml($this->userId, $trackingNumber) |
||
63 | ] |
||
64 | ]); |
||
65 | $array = Utility::parseXml($response->getBody()); |
||
66 | } catch (\Exception $exception) { |
||
67 | throw new TrackException($exception->getMessage()); |
||
68 | } |
||
69 | if (!isset($array['TrackInfo'])) { |
||
70 | throw new TrackException($array['Description']); |
||
71 | } |
||
72 | if (isset($array['TrackInfo']['Error'])) { |
||
73 | throw new TrackException($array['TrackInfo']['Error']['Description']); |
||
74 | } |
||
75 | if (!isset($array['TrackInfo']['TrackSummary']) && !isset($array['TrackInfo']['TrackDetail'])) { |
||
76 | throw new TrackException('Cannot find any events'); |
||
77 | } |
||
78 | $shipment = static::buildShipment($array); |
||
79 | return $shipment; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param array $options |
||
84 | * @return ResponseInterface |
||
85 | * @codeCoverageIgnore |
||
86 | */ |
||
87 | protected function request($options) |
||
91 | |||
92 | /** |
||
93 | * @param string $userId |
||
94 | * @param string $trackID |
||
95 | * @return string |
||
96 | */ |
||
97 | protected static function buildXml($userId, $trackID) |
||
108 | |||
109 | /** |
||
110 | * @param array $array |
||
111 | * @return Shipment |
||
112 | */ |
||
113 | protected static function buildShipment($array) |
||
154 | } |