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 cachingCity() |
54
|
|
|
{ |
55
|
|
|
self::cacheCity(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function costDetails() |
59
|
|
|
{ |
60
|
|
|
self::$return = self::get_cost_details(self::$arr); |
61
|
|
|
|
62
|
|
|
return new static(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function courier() |
66
|
|
|
{ |
67
|
|
|
self::$return = self::get_courier(self::$arr); |
68
|
|
|
|
69
|
|
|
return new static(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function province() |
73
|
|
|
{ |
74
|
|
|
$ret = self::provinceData(); |
75
|
|
|
self::$return = $ret; |
76
|
|
|
|
77
|
|
|
return new static(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function city() |
81
|
|
|
{ |
82
|
|
|
$ret = self::cityData(); |
83
|
|
|
self::$return = $ret; |
84
|
|
|
|
85
|
|
|
return new static(); |
86
|
|
|
} |
87
|
|
|
private static function setupConfig(){ |
88
|
|
|
self::$cacheType = strtolower(config('irfa.rajaongkir.cache_type')); |
|
|
|
|
89
|
|
|
self::$province = config('irfa.rajaongkir.province_table'); |
90
|
|
|
self::$city = config('irfa.rajaongkir.city_table'); |
91
|
|
|
} |
92
|
|
|
private static function provinceData(){ |
93
|
|
|
if (function_exists('config') and function_exists('app')) { |
|
|
|
|
94
|
|
|
self::setupConfig(); |
95
|
|
|
$cache_type = self::$cacheType; |
96
|
|
|
if ($cache_type == 'database') { |
97
|
|
|
if (ROCache::checkProv()) { |
98
|
|
|
if (count(ROCache::getProv(self::$arr)) > 0) { |
99
|
|
|
$ret = ROCache::getProv(self::$arr); |
100
|
|
|
} else { |
101
|
|
|
$ret = self::get_province(self::$arr); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} elseif ($cache_type == 'file') { |
105
|
|
|
$ret = ROCache::cacheFile(self::$province, self::$arr); |
106
|
|
|
if ($ret == null) { |
107
|
|
|
self::exceptionCache(); |
108
|
|
|
} |
109
|
|
|
} else { |
110
|
|
|
$ret = self::get_province(self::$arr); |
111
|
|
|
} |
112
|
|
|
} else { |
113
|
|
|
$ret = self::get_province(self::$arr); |
114
|
|
|
} |
115
|
|
|
return $ret; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
private static function cityData(){ |
118
|
|
|
if (function_exists('config') and function_exists('app')) { |
|
|
|
|
119
|
|
|
self::setupConfig(); |
120
|
|
|
$cache_type = self::$cacheType; |
121
|
|
|
if ($cache_type == 'database') { |
122
|
|
|
if (ROCache::checkCity()) { |
123
|
|
|
if (count(ROCache::getCity(self::$arr)) > 0) { |
124
|
|
|
$ret = ROCache::getCity(self::$arr); |
125
|
|
|
} else { |
126
|
|
|
$ret = self::get_city(self::$arr); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} elseif ($cache_type == 'file') { |
130
|
|
|
$ret = ROCache::cacheFile(self::$city, self::$arr); |
131
|
|
|
if ($ret == null) { |
132
|
|
|
self::exceptionCache(); |
133
|
|
|
} |
134
|
|
|
} else { |
135
|
|
|
$ret = self::get_city(self::$arr); |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$ret = self::get_city(self::$arr); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $ret; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
private static function exceptionCache(){ |
144
|
|
|
throw new Exception('Cache file is empty. Try php artisan raja-ongkir:cache'); |
145
|
|
|
|
146
|
|
|
return false; |
|
|
|
|
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.