Jne   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
wmc 7
c 8
b 1
f 1
lcom 1
cbo 9
dl 0
loc 116
ccs 23
cts 23
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A httpClient() 0 8 2
A searchOrigin() 0 4 1
A searchDestination() 0 4 1
A searchLocation() 0 8 1
A deliveryOptions() 0 6 1
A deliveryOptionsParams() 0 10 1
1
<?php
2
3
namespace Jne;
4
5
use Jne\Contracts\CourierSystemInterface;
6
use Jne\Collections\LocationCollection;
7
use Jne\Mappers\ArrayMappers\LocationMapper;
8
use Jne\Collections\DeliveryOptionCollection;
9
use Jne\Mappers\HtmlMappers\DeliveryOptionHtmlMapper;
10
use Jne\Contracts\Foundation\PackageInterface;
11
12
class Jne implements CourierSystemInterface
13
{
14
    /**
15
     * JNE base URI.
16
     */
17
    const BASE_URI = 'http://www.jne.co.id/';
18
19
    /**
20
     * Search origin URI.
21
     */
22
    const SEARCH_ORIGIN_URI = 'server/server_city_from.php';
23
24
    /**
25
     * Search destination URI.
26
     */
27
    const SEARCH_DESTINATION_URI = 'server/server_city.php';
28
29
    /**
30
     * Deliver URI.
31
     */
32
    const DELIVER_URI = 'getDetailFare.php';
33
34
    /**
35
     * Http client instance.
36
     *
37
     * @var \Jne\Contracts\HttpClientInterface
38
     */
39
    protected $httpClient;
40
41
    /**
42
     * Get http client instance.
43
     *
44
     * @return \Jne\Contracts\HttpClientInterface
45
     */
46 4
    public function httpClient()
47
    {
48 4
        if (is_null($this->httpClient)) {
49 4
            $this->httpClient = new HttpClient(self::BASE_URI);
50 4
        }
51
52 4
        return $this->httpClient;
53
    }
54
55
    /**
56
     * Search for available origin location.
57
     *
58
     * @param string $query
59
     *
60
     * @return \Jne\Contracts\Collections\LocationCollectionInterface
61
     */
62 1
    public function searchOrigin($query)
63
    {
64 1
        return $this->searchLocation(self::SEARCH_ORIGIN_URI, $query);
65
    }
66
67
    /**
68
     * Search for available destination location.
69
     *
70
     * @param string $query
71
     *
72
     * @return \Jne\Contracts\Collections\LocationCollectionInterface
73
     */
74 1
    public function searchDestination($query)
75
    {
76 1
        return $this->searchLocation(self::SEARCH_DESTINATION_URI, $query);
77
    }
78
79
    /**
80
     * Search for available location.
81
     *
82
     * @param string $uri
83
     * @param string $query
84
     *
85
     * @return \Jne\Contracts\Collections\LocationCollectionInterface
86
     */
87 2
    protected function searchLocation($uri, $query)
88
    {
89 2
        $uri .= '?'.http_build_query(['term' => $query]);
90
91 2
        $locations = $this->httpClient()->getAndParseJson($uri);
92
93 2
        return LocationCollection::fromArray($locations, new LocationMapper());
94
    }
95
96
    /**
97
     * Get delivery options.
98
     *
99
     * @param \Jne\Contracts\Foundation\PackageInterface $package
100
     *
101
     * @return \Jne\Contracts\Collections\DeliveryOptionCollectionInterface
102
     */
103 1
    public function deliveryOptions(PackageInterface $package)
104
    {
105 1
        $deliveryOptions = $this->httpClient()->postAndParseHtml(self::DELIVER_URI, $this->deliveryOptionsParams($package));
106
107 1
        return DeliveryOptionCollection::fromHtml($deliveryOptions, new DeliveryOptionHtmlMapper());
108
    }
109
110
    /**
111
     * Get delivery options parameters.
112
     *
113
     * @param \Jne\Contracts\Foundation\PackageInterface $package
114
     *
115
     * @return array
116
     */
117 1
    protected function deliveryOptionsParams(PackageInterface $package)
118
    {
119
        return [
120 1
            'origin' => $package->origin()->code(),
121 1
            'originlabel' => $package->origin()->name(),
122 1
            'dest' => $package->destination()->code(),
123 1
            'destlabel' => $package->destination()->name(),
124 1
            'weight' => $package->weight()->kilograms(),
125 1
        ];
126
    }
127
}
128