KuaiDi100Tracker   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 7
dl 0
loc 138
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setAppKey() 0 4 1
A getAppKey() 0 4 1
A getCarrier() 0 4 1
A setCarrier() 0 5 1
A buildQueryParameters() 0 11 1
A track() 0 11 2
A getHttpClient() 0 7 2
A sendRequest() 0 9 2
A buildShipment() 0 16 2
1
<?php
2
/**
3
 * Slince shipment tracker library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\ShipmentTracking\KuaiDi100;
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\Shipment;
15
use Slince\ShipmentTracking\ShipmentEvent;
16
17
class KuaiDi100Tracker extends HttpAwareTracker
18
{
19
    /**
20
     * @var string
21
     */
22
    const TRACKING_ENDPOINT = 'http://api.kuaidi100.com/api';
23
24
    /**
25
     * @var string
26
     */
27
    protected $appKey;
28
29
    /**
30
     * @var string
31
     */
32
    protected $carrier;
33
34
    public function __construct($appKey, $carrier = null, HttpClient $httpClient = null)
35
    {
36
        $this->appKey = $appKey;
37
        $this->carrier = $carrier;
38
        $httpClient && $this->setHttpClient($httpClient);
39
    }
40
41
    /**
42
     * @param string $appKey
43
     */
44
    public function setAppKey($appKey)
45
    {
46
        $this->appKey = $appKey;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getAppKey()
53
    {
54
        return $this->appKey;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getCarrier()
61
    {
62
        return $this->carrier;
63
    }
64
65
    /**
66
     * @param string $carrier
67
     * @return KuaiDi100Tracker
68
     */
69
    public function setCarrier($carrier)
70
    {
71
        $this->carrier = $carrier;
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $trackingNumber
77
     * @return array
78
     */
79
    protected function buildQueryParameters($trackingNumber)
80
    {
81
        return [
82
            'id' => $this->appKey,
83
            'com' => $this->carrier,
84
            'nu' => $trackingNumber,
85
            'show' => 0,
86
            'multi' => 1,
87
            'order' => 'asc',
88
        ];
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function track($trackingNumber)
95
    {
96
        $request = new Request('GET', static::TRACKING_ENDPOINT);
97
        $json = $this->sendRequest($request, [
98
            'query' => $this->buildQueryParameters($trackingNumber)
99
        ]);
100
        if ($json['status'] != 1) {
101
            throw new TrackException(sprintf('Bad response with status "%d"', $json['status']));
102
        }
103
        return static::buildShipment($json);
104
    }
105
106
    /**
107
     * @return HttpClient
108
     * @codeCoverageIgnore
109
     */
110
    protected function getHttpClient()
111
    {
112
        if (!is_null($this->httpClient)) {
113
            return $this->httpClient;
114
        }
115
        return $this->httpClient = new HttpClient();
116
    }
117
118
    /**
119
     * @param RequestInterface $request
120
     * @param array $options
121
     * @return array
122
     * @codeCoverageIgnore
123
     */
124
    protected function sendRequest(RequestInterface $request, array $options = [])
125
    {
126
        try {
127
            $response = $this->getHttpClient()->send($request, $options);
128
            return \GuzzleHttp\json_decode((string)$response->getBody(), true);
129
        } catch (GuzzleException $exception) {
130
            throw new TrackException($exception->getMessage());
131
        }
132
    }
133
134
    /**
135
     * @param array $json
136
     * @return Shipment
137
     */
138
    protected static function buildShipment($json)
139
    {
140
        $events = array_map(function($item) {
141
            return ShipmentEvent::fromArray([
142
                'location' => $item['location'],
143
                'description' => $item['context'],
144
                'date' => $item['time'],
145
            ]);
146
        }, $json['data']);
147
        $shipment = new Shipment($events);
148
        $shipment->setIsDelivered($json['state'] == 3);
149
        if ($firstEvent = reset($events)) {
150
            $shipment->setDeliveredAt($firstEvent->getDate());
151
        }
152
        return $shipment;
153
    }
154
}