1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
Author: Irfa Ardiansyah <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Irfa\RajaOngkir\Ongkir; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use Irfa\RajaOngkir\Caching\ROCache; |
11
|
|
|
use Irfa\RajaOngkir\Ongkir\Func\Api; |
12
|
|
|
|
13
|
|
|
class Ongkir extends Api |
14
|
|
|
{ |
15
|
|
|
private static $arr; |
16
|
|
|
private static $return; |
17
|
|
|
private static $province; |
18
|
|
|
private static $city; |
19
|
|
|
private static $cacheType; |
20
|
|
|
|
21
|
|
|
public static function find($arr) |
22
|
|
|
{ |
23
|
|
|
if (is_array($arr)) { |
24
|
|
|
self::$arr = $arr; |
25
|
|
|
|
26
|
|
|
return new static(); |
27
|
|
|
} else { |
28
|
|
|
throw new Exception('Parameter must be an array.'); |
29
|
|
|
|
30
|
|
|
return false; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function get() |
35
|
|
|
{ |
36
|
|
|
self::$arr = null; //Clear parameter |
37
|
|
|
if (empty(self::$return)) { |
38
|
|
|
throw new Exception('Data is not defined.'); |
39
|
|
|
|
40
|
|
|
return false; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
$ret = self::$return; |
43
|
|
|
self::$return = null; |
44
|
|
|
|
45
|
|
|
return $ret; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function cachingProvince() |
49
|
|
|
{ |
50
|
|
|
self::cacheProvince(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function cachingSubDistrict() |
54
|
|
|
{ |
55
|
|
|
$get = self::cityData(); |
56
|
|
|
$count = count($get); |
57
|
|
|
$i=0; |
58
|
|
|
echo PHP_EOL."\033[42mThis may take longer, please wait.\033[0m"; |
59
|
|
|
foreach($get as $city){ |
60
|
|
|
$i++; |
61
|
|
|
echo PHP_EOL."Remaining City\033[96m ".$i."/".$count."\033[0m"; |
62
|
|
|
echo PHP_EOL."Get Subdistrict\033[96m ".$city->city_name."...\033[0m".PHP_EOL; |
63
|
|
|
self::cacheSubDistrict(['city' => $city->city_id]); |
64
|
|
|
echo PHP_EOL; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function cachingCity() |
69
|
|
|
{ |
70
|
|
|
self::cacheCity(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function costDetails() |
74
|
|
|
{ |
75
|
|
|
self::$return = self::get_cost_details(self::$arr); |
76
|
|
|
|
77
|
|
|
return new static(); |
78
|
|
|
} |
79
|
|
|
public static function testConnection() |
80
|
|
|
{ |
81
|
|
|
self::$return = self::test_connection(self::$arr); |
82
|
|
|
|
83
|
|
|
return new static(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function courier() |
87
|
|
|
{ |
88
|
|
|
self::$return = self::get_courier(self::$arr); |
89
|
|
|
|
90
|
|
|
return new static(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function province() |
94
|
|
|
{ |
95
|
|
|
$ret = self::provinceData(); |
96
|
|
|
self::$return = $ret; |
97
|
|
|
|
98
|
|
|
return new static(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function subDistrict() |
102
|
|
|
{ |
103
|
|
|
$ret = self::subDistrictData(); |
104
|
|
|
self::$return = $ret; |
105
|
|
|
|
106
|
|
|
return new static(); |
107
|
|
|
} |
108
|
|
|
public static function internationalOrigin() |
109
|
|
|
{ |
110
|
|
|
$ret = self::internationalOriginData(); |
111
|
|
|
self::$return = $ret; |
112
|
|
|
|
113
|
|
|
return new static(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function city() |
117
|
|
|
{ |
118
|
|
|
$ret = self::cityData(); |
119
|
|
|
self::$return = $ret; |
120
|
|
|
|
121
|
|
|
return new static(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private static function setupConfig() |
125
|
|
|
{ |
126
|
|
|
self::$cacheType = strtolower(config('irfa.rajaongkir.cache_type')); |
|
|
|
|
127
|
|
|
self::$province = config('irfa.rajaongkir.province_table'); |
128
|
|
|
self::$city = config('irfa.rajaongkir.city_table'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private static function provinceData() |
132
|
|
|
{ |
133
|
|
|
if (function_exists('config') && function_exists('app')) { |
134
|
|
|
self::setupConfig(); |
135
|
|
|
$cache_type = self::$cacheType; |
136
|
|
|
if ($cache_type == 'database') { |
137
|
|
|
if (ROCache::checkProv()) { |
138
|
|
|
if (count(ROCache::getProv(self::$arr)) > 0) { |
139
|
|
|
$ret = ROCache::getProv(self::$arr); |
140
|
|
|
} else { |
141
|
|
|
$ret = self::get_province(self::$arr); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} elseif ($cache_type == 'file') { |
145
|
|
|
$ret = ROCache::cacheFile(self::$province, self::$arr); |
146
|
|
|
if ($ret == null) { |
147
|
|
|
self::exceptionCache(); |
148
|
|
|
} |
149
|
|
|
} else { |
150
|
|
|
$ret = self::get_province(self::$arr); |
151
|
|
|
} |
152
|
|
|
} else { |
153
|
|
|
$ret = self::get_province(self::$arr); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $ret; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
private static function subDistrictData() |
159
|
|
|
{ |
160
|
|
|
if (function_exists('config') && function_exists('app')) { |
161
|
|
|
self::setupConfig(); |
162
|
|
|
$cache_type = self::$cacheType; |
163
|
|
|
if ($cache_type == 'database') { |
164
|
|
|
if (ROCache::checkProv()) { |
165
|
|
|
if (count(ROCache::getSubdistrict(self::$arr)) > 0) { |
166
|
|
|
$ret = ROCache::getSubdistrict(self::$arr); |
167
|
|
|
} else { |
168
|
|
|
$ret = self::getSubdistrict(self::$arr); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} elseif ($cache_type == 'file') { |
172
|
|
|
$ret = ROCache::cacheFile(self::$province, self::$arr); |
173
|
|
|
if ($ret == null) { |
174
|
|
|
self::exceptionCache(); |
175
|
|
|
} |
176
|
|
|
} else { |
177
|
|
|
$ret = self::getSubdistrict(self::$arr); |
178
|
|
|
} |
179
|
|
|
} else { |
180
|
|
|
$ret = self::getSubdistrict(self::$arr); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $ret; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
private static function internationalOriginData() |
186
|
|
|
{ |
187
|
|
|
|
188
|
|
|
$ret = self::get_international_origin(self::$arr); |
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
return $ret; |
192
|
|
|
} |
193
|
|
|
private static function cityData() |
194
|
|
|
{ |
195
|
|
|
if (function_exists('config') && function_exists('app')) { |
196
|
|
|
self::setupConfig(); |
197
|
|
|
$cache_type = self::$cacheType; |
198
|
|
|
if ($cache_type == 'database') { |
199
|
|
|
if (ROCache::checkCity()) { |
200
|
|
|
if (count(ROCache::getCity(self::$arr)) > 0) { |
201
|
|
|
$ret = ROCache::getCity(self::$arr); |
202
|
|
|
} else { |
203
|
|
|
$ret = self::get_city(self::$arr); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} elseif ($cache_type == 'file') { |
207
|
|
|
$ret = ROCache::cacheFile(self::$city, self::$arr); |
208
|
|
|
if ($ret == null) { |
209
|
|
|
self::exceptionCache(); |
210
|
|
|
} |
211
|
|
|
} else { |
212
|
|
|
$ret = self::get_city(self::$arr); |
213
|
|
|
} |
214
|
|
|
} else { |
215
|
|
|
$ret = self::get_city(self::$arr); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $ret; |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
private static function exceptionCache() |
222
|
|
|
{ |
223
|
|
|
throw new Exception('Cache file is empty. Try php artisan raja-ongkir:cache'); |
224
|
|
|
|
225
|
|
|
return false; |
|
|
|
|
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.