KuaiDi100Tracker   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 126
rs 10
c 0
b 0
f 0

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