Passed
Push — master ( c12223...ee094a )
by IRFA
01:39
created
console/commands/ROCity.php 1 patch
Indentation   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -7,37 +7,37 @@
 block discarded – undo
7 7
 
8 8
 class ROCity extends Command
9 9
 {
10
-    /**
11
-     * The name and signature of the console command.
12
-     *
13
-     * @var string
14
-     */
15
-    protected $signature = 'raja-ongkir:city-cache';
10
+	/**
11
+	 * The name and signature of the console command.
12
+	 *
13
+	 * @var string
14
+	 */
15
+	protected $signature = 'raja-ongkir:city-cache';
16 16
 
17
-    /**
18
-     * The console command description.
19
-     *
20
-     * @var string
21
-     */
22
-    protected $description = 'Create city list cache for faster loading city list';
17
+	/**
18
+	 * The console command description.
19
+	 *
20
+	 * @var string
21
+	 */
22
+	protected $description = 'Create city list cache for faster loading city list';
23 23
 
24
-    /**
25
-     * Create a new command instance.
26
-     *
27
-     * @return void
28
-     */
29
-    public function __construct()
30
-    {
31
-        parent::__construct();
32
-    }
24
+	/**
25
+	 * Create a new command instance.
26
+	 *
27
+	 * @return void
28
+	 */
29
+	public function __construct()
30
+	{
31
+		parent::__construct();
32
+	}
33 33
 
34
-    /**
35
-     * Execute the console command.
36
-     *
37
-     * @return mixed
38
-     */
39
-    public function handle()
40
-    {
41
-        RajaOngkir::cachingCity();
42
-    }
34
+	/**
35
+	 * Execute the console command.
36
+	 *
37
+	 * @return mixed
38
+	 */
39
+	public function handle()
40
+	{
41
+		RajaOngkir::cachingCity();
42
+	}
43 43
 }
Please login to merge, or discard this patch.
console/commands/RORefresh.php 1 patch
Indentation   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -8,51 +8,51 @@
 block discarded – undo
8 8
 
9 9
 class RORefresh extends Command
10 10
 {
11
-    /**
12
-     * The name and signature of the console command.
13
-     *
14
-     * @var string
15
-     */
16
-    protected $signature = 'raja-ongkir:refresh';
11
+	/**
12
+	 * The name and signature of the console command.
13
+	 *
14
+	 * @var string
15
+	 */
16
+	protected $signature = 'raja-ongkir:refresh';
17 17
 
18
-    /**
19
-     * The console command description.
20
-     *
21
-     * @var string
22
-     */
23
-    protected $description = 'Refresh Cache';
18
+	/**
19
+	 * The console command description.
20
+	 *
21
+	 * @var string
22
+	 */
23
+	protected $description = 'Refresh Cache';
24 24
 
25
-    /**
26
-     * Create a new command instance.
27
-     *
28
-     * @return void
29
-     */
30
-    public function __construct()
31
-    {
32
-        parent::__construct();
33
-    }
25
+	/**
26
+	 * Create a new command instance.
27
+	 *
28
+	 * @return void
29
+	 */
30
+	public function __construct()
31
+	{
32
+		parent::__construct();
33
+	}
34 34
 
35
-    /**
36
-     * Execute the console command.
37
-     *
38
-     * @return mixed
39
-     */
40
-    public function handle()
41
-    {
42
-        echo '---------------------'.PHP_EOL;
43
-        echo'Refresh Cache'.PHP_EOL;
44
-        echo'---------------------'.PHP_EOL;
45
-        ROCache::clearCache();
46
-        sleep(1); //Cooling Down
47
-        echo PHP_EOL.'---------------------'.PHP_EOL;
48
-        echo'Province Caching'.PHP_EOL;
49
-        echo'---------------------'.PHP_EOL;
50
-        RajaOngkir::cachingProvince();
51
-        echo PHP_EOL.'---------------------'.PHP_EOL;
52
-        sleep(1); //Cooling Down
53
-        echo'City Caching'.PHP_EOL;
54
-        echo '---------------------'.PHP_EOL;
55
-        RajaOngkir::cachingCity();
56
-        echo PHP_EOL.'---------------------'.PHP_EOL;
57
-    }
35
+	/**
36
+	 * Execute the console command.
37
+	 *
38
+	 * @return mixed
39
+	 */
40
+	public function handle()
41
+	{
42
+		echo '---------------------'.PHP_EOL;
43
+		echo'Refresh Cache'.PHP_EOL;
44
+		echo'---------------------'.PHP_EOL;
45
+		ROCache::clearCache();
46
+		sleep(1); //Cooling Down
47
+		echo PHP_EOL.'---------------------'.PHP_EOL;
48
+		echo'Province Caching'.PHP_EOL;
49
+		echo'---------------------'.PHP_EOL;
50
+		RajaOngkir::cachingProvince();
51
+		echo PHP_EOL.'---------------------'.PHP_EOL;
52
+		sleep(1); //Cooling Down
53
+		echo'City Caching'.PHP_EOL;
54
+		echo '---------------------'.PHP_EOL;
55
+		RajaOngkir::cachingCity();
56
+		echo PHP_EOL.'---------------------'.PHP_EOL;
57
+	}
58 58
 }
Please login to merge, or discard this patch.
console/commands/ROCacheAll.php 1 patch
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -7,46 +7,46 @@
 block discarded – undo
7 7
 
8 8
 class ROCacheAll extends Command
9 9
 {
10
-    /**
11
-     * The name and signature of the console command.
12
-     *
13
-     * @var string
14
-     */
15
-    protected $signature = 'raja-ongkir:cache';
10
+	/**
11
+	 * The name and signature of the console command.
12
+	 *
13
+	 * @var string
14
+	 */
15
+	protected $signature = 'raja-ongkir:cache';
16 16
 
17
-    /**
18
-     * The console command description.
19
-     *
20
-     * @var string
21
-     */
22
-    protected $description = 'Create City and Province list cache for faster loading city and province. ';
17
+	/**
18
+	 * The console command description.
19
+	 *
20
+	 * @var string
21
+	 */
22
+	protected $description = 'Create City and Province list cache for faster loading city and province. ';
23 23
 
24
-    /**
25
-     * Create a new command instance.
26
-     *
27
-     * @return void
28
-     */
29
-    public function __construct()
30
-    {
31
-        parent::__construct();
32
-    }
24
+	/**
25
+	 * Create a new command instance.
26
+	 *
27
+	 * @return void
28
+	 */
29
+	public function __construct()
30
+	{
31
+		parent::__construct();
32
+	}
33 33
 
34
-    /**
35
-     * Execute the console command.
36
-     *
37
-     * @return mixed
38
-     */
39
-    public function handle()
40
-    {
41
-        echo '---------------------'.PHP_EOL;
42
-        echo'Province Caching'.PHP_EOL;
43
-        echo'---------------------'.PHP_EOL;
44
-        RajaOngkir::cachingProvince();
45
-        echo PHP_EOL.'---------------------'.PHP_EOL;
46
-        sleep(1); //Cooling Down
47
-        echo'City Caching'.PHP_EOL;
48
-        echo '---------------------'.PHP_EOL;
49
-        RajaOngkir::cachingCity();
50
-        echo PHP_EOL.'---------------------'.PHP_EOL;
51
-    }
34
+	/**
35
+	 * Execute the console command.
36
+	 *
37
+	 * @return mixed
38
+	 */
39
+	public function handle()
40
+	{
41
+		echo '---------------------'.PHP_EOL;
42
+		echo'Province Caching'.PHP_EOL;
43
+		echo'---------------------'.PHP_EOL;
44
+		RajaOngkir::cachingProvince();
45
+		echo PHP_EOL.'---------------------'.PHP_EOL;
46
+		sleep(1); //Cooling Down
47
+		echo'City Caching'.PHP_EOL;
48
+		echo '---------------------'.PHP_EOL;
49
+		RajaOngkir::cachingCity();
50
+		echo PHP_EOL.'---------------------'.PHP_EOL;
51
+	}
52 52
 }
Please login to merge, or discard this patch.
resources/config/irfa/rajaongkir.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 return [
4
-    /*
4
+	/*
5 5
     |--------------------------------------------------------------------------
6 6
     | End Point Api ( Server Configuration )
7 7
     |--------------------------------------------------------------------------
@@ -11,8 +11,8 @@  discard block
 block discarded – undo
11 11
     | Pro       : http://pro.rajaongkir.com/api
12 12
     |
13 13
     */
14
-    'account_type' => env('RAJAONGKIR_ACCOUNT_TYPE', 'starter'),
15
-    /*
14
+	'account_type' => env('RAJAONGKIR_ACCOUNT_TYPE', 'starter'),
15
+	/*
16 16
     |--------------------------------------------------------------------------
17 17
     | API key
18 18
     |--------------------------------------------------------------------------
@@ -20,8 +20,8 @@  discard block
 block discarded – undo
20 20
     |
21 21
     */
22 22
 
23
-    'api_key' => env('RAJAONGKIR_API_KEY', 'your-api-key'),
24
-    /*
23
+	'api_key' => env('RAJAONGKIR_API_KEY', 'your-api-key'),
24
+	/*
25 25
     |--------------------------------------------------------------------------
26 26
     | For Caching
27 27
     |--------------------------------------------------------------------------
@@ -33,10 +33,10 @@  discard block
 block discarded – undo
33 33
     | Cache supported database,and file. If you can't use cache, set value to null
34 34
     */
35 35
 
36
-    'province_table' => env('RAJAONGKIR_PROV_TABLE', 'ro_province'),
36
+	'province_table' => env('RAJAONGKIR_PROV_TABLE', 'ro_province'),
37 37
 
38
-    'city_table' => env('RAJAONGKIR_CITY_TABLE', 'ro_city'),
38
+	'city_table' => env('RAJAONGKIR_CITY_TABLE', 'ro_city'),
39 39
 
40
-    'cache_type' => env('RAJAONGKIR_CACHE', null),
40
+	'cache_type' => env('RAJAONGKIR_CACHE', null),
41 41
 
42 42
 ];
Please login to merge, or discard this patch.
src/Ongkir/Ongkir.php 1 patch
Indentation   +142 added lines, -142 removed lines patch added patch discarded remove patch
@@ -12,146 +12,146 @@
 block discarded – undo
12 12
 
13 13
 class Ongkir extends Api
14 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
-
88
-    private static function setupConfig()
89
-    {
90
-        self::$cacheType = strtolower(config('irfa.rajaongkir.cache_type'));
91
-        self::$province = config('irfa.rajaongkir.province_table');
92
-        self::$city = config('irfa.rajaongkir.city_table');
93
-    }
94
-
95
-    private static function provinceData()
96
-    {
97
-        if (function_exists('config') && function_exists('app')) {
98
-            self::setupConfig();
99
-            $cache_type = self::$cacheType;
100
-            if ($cache_type == 'database') {
101
-                if (ROCache::checkProv()) {
102
-                    if (count(ROCache::getProv(self::$arr)) > 0) {
103
-                        $ret = ROCache::getProv(self::$arr);
104
-                    } else {
105
-                        $ret = self::get_province(self::$arr);
106
-                    }
107
-                }
108
-            } elseif ($cache_type == 'file') {
109
-                $ret = ROCache::cacheFile(self::$province, self::$arr);
110
-                if ($ret == null) {
111
-                    self::exceptionCache();
112
-                }
113
-            } else {
114
-                $ret = self::get_province(self::$arr);
115
-            }
116
-        } else {
117
-            $ret = self::get_province(self::$arr);
118
-        }
119
-
120
-        return $ret;
121
-    }
122
-
123
-    private static function cityData()
124
-    {
125
-        if (function_exists('config') && function_exists('app')) {
126
-            self::setupConfig();
127
-            $cache_type = self::$cacheType;
128
-            if ($cache_type == 'database') {
129
-                if (ROCache::checkCity()) {
130
-                    if (count(ROCache::getCity(self::$arr)) > 0) {
131
-                        $ret = ROCache::getCity(self::$arr);
132
-                    } else {
133
-                        $ret = self::get_city(self::$arr);
134
-                    }
135
-                }
136
-            } elseif ($cache_type == 'file') {
137
-                $ret = ROCache::cacheFile(self::$city, self::$arr);
138
-                if ($ret == null) {
139
-                    self::exceptionCache();
140
-                }
141
-            } else {
142
-                $ret = self::get_city(self::$arr);
143
-            }
144
-        } else {
145
-            $ret = self::get_city(self::$arr);
146
-        }
147
-
148
-        return $ret;
149
-    }
150
-
151
-    private static function exceptionCache()
152
-    {
153
-        throw new Exception('Cache file is empty. Try php artisan raja-ongkir:cache');
154
-
155
-        return false;
156
-    }
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
+
88
+	private static function setupConfig()
89
+	{
90
+		self::$cacheType = strtolower(config('irfa.rajaongkir.cache_type'));
91
+		self::$province = config('irfa.rajaongkir.province_table');
92
+		self::$city = config('irfa.rajaongkir.city_table');
93
+	}
94
+
95
+	private static function provinceData()
96
+	{
97
+		if (function_exists('config') && function_exists('app')) {
98
+			self::setupConfig();
99
+			$cache_type = self::$cacheType;
100
+			if ($cache_type == 'database') {
101
+				if (ROCache::checkProv()) {
102
+					if (count(ROCache::getProv(self::$arr)) > 0) {
103
+						$ret = ROCache::getProv(self::$arr);
104
+					} else {
105
+						$ret = self::get_province(self::$arr);
106
+					}
107
+				}
108
+			} elseif ($cache_type == 'file') {
109
+				$ret = ROCache::cacheFile(self::$province, self::$arr);
110
+				if ($ret == null) {
111
+					self::exceptionCache();
112
+				}
113
+			} else {
114
+				$ret = self::get_province(self::$arr);
115
+			}
116
+		} else {
117
+			$ret = self::get_province(self::$arr);
118
+		}
119
+
120
+		return $ret;
121
+	}
122
+
123
+	private static function cityData()
124
+	{
125
+		if (function_exists('config') && function_exists('app')) {
126
+			self::setupConfig();
127
+			$cache_type = self::$cacheType;
128
+			if ($cache_type == 'database') {
129
+				if (ROCache::checkCity()) {
130
+					if (count(ROCache::getCity(self::$arr)) > 0) {
131
+						$ret = ROCache::getCity(self::$arr);
132
+					} else {
133
+						$ret = self::get_city(self::$arr);
134
+					}
135
+				}
136
+			} elseif ($cache_type == 'file') {
137
+				$ret = ROCache::cacheFile(self::$city, self::$arr);
138
+				if ($ret == null) {
139
+					self::exceptionCache();
140
+				}
141
+			} else {
142
+				$ret = self::get_city(self::$arr);
143
+			}
144
+		} else {
145
+			$ret = self::get_city(self::$arr);
146
+		}
147
+
148
+		return $ret;
149
+	}
150
+
151
+	private static function exceptionCache()
152
+	{
153
+		throw new Exception('Cache file is empty. Try php artisan raja-ongkir:cache');
154
+
155
+		return false;
156
+	}
157 157
 }
Please login to merge, or discard this patch.
src/Ongkir/Func/Api.php 1 patch
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -12,189 +12,189 @@
 block discarded – undo
12 12
 
13 13
 class Api extends CacheCurl
14 14
 {
15
-    private static $account_type;
16
-    private static $api_key;
17
-    private static $url;
18
-    private static $count = 0;
19
-
20
-    private static function setup_option()
21
-    {
22
-        if (function_exists('config') and function_exists('app')) {//Load Config For Laravel
23
-            self::$account_type = strtolower(config('irfa.rajaongkir.account_type'));
24
-            self::$api_key = config('irfa.rajaongkir.api_key');
25
-        } else {//Load config For PHP Native
26
-            require __DIR__.'../../../../config/config.php';
27
-            self::$account_type = strtolower($config['account_type']);
28
-            self::$api_key = $config['api_key'];
29
-        }
30
-        if (self::$account_type == 'pro') {
31
-            self::$url = 'https://pro.rajaongkir.com/api';
32
-        } else {
33
-            self::$url = 'https://api.rajaongkir.com/'.self::$account_type;
34
-        }
35
-    }
36
-
37
-    protected static function cacheProvince()
38
-    {
39
-        self::setup_option();
40
-        echo "Retrieving data from \033[96m".self::$url."...\033[0m".PHP_EOL;
41
-        CacheCurl::caching(self::get_province())->province();
42
-    }
43
-
44
-    protected static function cacheCity()
45
-    {
46
-        self::setup_option();
47
-        echo "Retrieving data from\033[96m ".self::$url."...\033[0m".PHP_EOL;
48
-        CacheCurl::caching(self::get_city())->city();
49
-    }
50
-
51
-    protected static function get_province($arr = null)
52
-    {
53
-        if ($arr != null) {
54
-            $province_id = array_key_exists('province_id', $arr) ? '?id='.$arr['province_id'] : null;
55
-        } else {
56
-            $province_id = null;
57
-        }
58
-        self::setup_option();
59
-        $curl = curl_init();
60
-        curl_setopt_array($curl, [
61
-            CURLOPT_URL            => self::$url.'/province'.$province_id,
62
-            CURLOPT_RETURNTRANSFER => true,
63
-            CURLOPT_ENCODING       => '',
64
-            CURLOPT_MAXREDIRS      => 10,
65
-            CURLOPT_TIMEOUT        => 30,
66
-            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
67
-            CURLOPT_CUSTOMREQUEST  => 'GET',
68
-            CURLOPT_HTTPHEADER     => [
69
-                'key: '.self::$api_key,
70
-            ],
71
-        ]);
72
-        $response = curl_exec($curl);
73
-        $err = curl_error($curl);
74
-
75
-        curl_close($curl);
76
-
77
-        if ($err) {
78
-            echo "Can't connect to server, please check your internet connection.";
79
-            exit();
80
-        } else {
81
-            $json = json_decode($response, false)->rajaongkir;
82
-            if ($json->status->code == '400') {
83
-                throw new Exception($json->status->description);
84
-            } else {
85
-                $res = $json->results;
86
-
87
-                return $res;
88
-            }
89
-        }
90
-    }
91
-
92
-    protected static function get_city($arr = null)
93
-    {
94
-        if ($arr != null) {
95
-            $province_id = array_key_exists('province_id', $arr) ? '?province='.$arr['province_id'] : null;
96
-        } else {
97
-            $province_id = null;
98
-        }
99
-        self::setup_option();
100
-        $curl = curl_init();
101
-        curl_setopt_array($curl, [
102
-            CURLOPT_URL            => self::$url.'/city'.$province_id,
103
-            CURLOPT_RETURNTRANSFER => true,
104
-            CURLOPT_ENCODING       => '',
105
-            CURLOPT_MAXREDIRS      => 10,
106
-            CURLOPT_TIMEOUT        => 30,
107
-            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
108
-            CURLOPT_CUSTOMREQUEST  => 'GET',
109
-            CURLOPT_HTTPHEADER     => [
110
-                'key: '.self::$api_key,
111
-            ],
112
-        ]);
113
-        $response = curl_exec($curl);
114
-        $err = curl_error($curl);
115
-
116
-        curl_close($curl);
117
-
118
-        if ($err) {
119
-            echo "Can't connect to server, please check your internet connection.";
120
-            exit();
121
-        } else {
122
-            $json = json_decode($response, false)->rajaongkir;
123
-            if ($json->status->code == '400') {
124
-                throw new Exception($json->status->description);
125
-            } else {
126
-                $res = $json->results;
127
-
128
-                return $res;
129
-            }
130
-        }
131
-    }
132
-
133
-    protected static function get_courier($arr)
134
-    {
135
-        $origin = $arr['origin'];
136
-        $destination = $arr['destination'];
137
-        $weight = $arr['weight'];
138
-        $courier = $arr['courier'];
139
-        $res = self::curl_cost_get($origin, $destination, $weight, $courier);
140
-
141
-        return $res;
142
-    }
143
-
144
-    protected static function get_cost_details($arr)
145
-    {
146
-        $origin = $arr['origin'];
147
-        $destination = $arr['destination'];
148
-        $weight = $arr['weight'];
149
-        $courier = $arr['courier'];
150
-        $res = self::curl_cost_get($origin, $destination, $weight, $courier);
151
-
152
-        return $res[0]->costs;
153
-    }
154
-
155
-    private static function curl_cost_get($origin, $destination, $weight, $courier)
156
-    {
157
-        $curl = curl_init();
158
-
159
-        curl_setopt_array($curl, self::curl_cost_option($origin, $destination, $weight, $courier));
160
-
161
-        $response = curl_exec($curl);
162
-        $err = curl_error($curl);
163
-
164
-        curl_close($curl);
165
-
166
-        if ($err) {
167
-            echo "Can't connect to server, please check your internet connection.";
168
-            exit();
169
-        } else {
170
-            $json = json_decode($response, false)->rajaongkir;
171
-            if ($json->status->code == '400') {
172
-                throw new Exception($json->status->description);
173
-            } else {
174
-                $res = $json->results;
175
-
176
-                return $res;
177
-            }
178
-        }
179
-    }
180
-
181
-    private static function curl_cost_option($origin, $destination, $weight, $courier)
182
-    {
183
-        self::setup_option();
184
-
185
-        return [
186
-            CURLOPT_URL            => self::$url.'/cost',
187
-            CURLOPT_RETURNTRANSFER => true,
188
-            CURLOPT_ENCODING       => '',
189
-            CURLOPT_MAXREDIRS      => 10,
190
-            CURLOPT_TIMEOUT        => 30,
191
-            CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
192
-            CURLOPT_CUSTOMREQUEST  => 'POST',
193
-            CURLOPT_POSTFIELDS     => 'origin='.$origin.'&destination='.$destination.'&weight='.$weight.'&courier='.strtolower($courier),
194
-            CURLOPT_HTTPHEADER     => [
195
-                'content-type: application/x-www-form-urlencoded',
196
-                'key: '.self::$api_key,
197
-            ],
198
-        ];
199
-    }
15
+	private static $account_type;
16
+	private static $api_key;
17
+	private static $url;
18
+	private static $count = 0;
19
+
20
+	private static function setup_option()
21
+	{
22
+		if (function_exists('config') and function_exists('app')) {//Load Config For Laravel
23
+			self::$account_type = strtolower(config('irfa.rajaongkir.account_type'));
24
+			self::$api_key = config('irfa.rajaongkir.api_key');
25
+		} else {//Load config For PHP Native
26
+			require __DIR__.'../../../../config/config.php';
27
+			self::$account_type = strtolower($config['account_type']);
28
+			self::$api_key = $config['api_key'];
29
+		}
30
+		if (self::$account_type == 'pro') {
31
+			self::$url = 'https://pro.rajaongkir.com/api';
32
+		} else {
33
+			self::$url = 'https://api.rajaongkir.com/'.self::$account_type;
34
+		}
35
+	}
36
+
37
+	protected static function cacheProvince()
38
+	{
39
+		self::setup_option();
40
+		echo "Retrieving data from \033[96m".self::$url."...\033[0m".PHP_EOL;
41
+		CacheCurl::caching(self::get_province())->province();
42
+	}
43
+
44
+	protected static function cacheCity()
45
+	{
46
+		self::setup_option();
47
+		echo "Retrieving data from\033[96m ".self::$url."...\033[0m".PHP_EOL;
48
+		CacheCurl::caching(self::get_city())->city();
49
+	}
50
+
51
+	protected static function get_province($arr = null)
52
+	{
53
+		if ($arr != null) {
54
+			$province_id = array_key_exists('province_id', $arr) ? '?id='.$arr['province_id'] : null;
55
+		} else {
56
+			$province_id = null;
57
+		}
58
+		self::setup_option();
59
+		$curl = curl_init();
60
+		curl_setopt_array($curl, [
61
+			CURLOPT_URL            => self::$url.'/province'.$province_id,
62
+			CURLOPT_RETURNTRANSFER => true,
63
+			CURLOPT_ENCODING       => '',
64
+			CURLOPT_MAXREDIRS      => 10,
65
+			CURLOPT_TIMEOUT        => 30,
66
+			CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
67
+			CURLOPT_CUSTOMREQUEST  => 'GET',
68
+			CURLOPT_HTTPHEADER     => [
69
+				'key: '.self::$api_key,
70
+			],
71
+		]);
72
+		$response = curl_exec($curl);
73
+		$err = curl_error($curl);
74
+
75
+		curl_close($curl);
76
+
77
+		if ($err) {
78
+			echo "Can't connect to server, please check your internet connection.";
79
+			exit();
80
+		} else {
81
+			$json = json_decode($response, false)->rajaongkir;
82
+			if ($json->status->code == '400') {
83
+				throw new Exception($json->status->description);
84
+			} else {
85
+				$res = $json->results;
86
+
87
+				return $res;
88
+			}
89
+		}
90
+	}
91
+
92
+	protected static function get_city($arr = null)
93
+	{
94
+		if ($arr != null) {
95
+			$province_id = array_key_exists('province_id', $arr) ? '?province='.$arr['province_id'] : null;
96
+		} else {
97
+			$province_id = null;
98
+		}
99
+		self::setup_option();
100
+		$curl = curl_init();
101
+		curl_setopt_array($curl, [
102
+			CURLOPT_URL            => self::$url.'/city'.$province_id,
103
+			CURLOPT_RETURNTRANSFER => true,
104
+			CURLOPT_ENCODING       => '',
105
+			CURLOPT_MAXREDIRS      => 10,
106
+			CURLOPT_TIMEOUT        => 30,
107
+			CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
108
+			CURLOPT_CUSTOMREQUEST  => 'GET',
109
+			CURLOPT_HTTPHEADER     => [
110
+				'key: '.self::$api_key,
111
+			],
112
+		]);
113
+		$response = curl_exec($curl);
114
+		$err = curl_error($curl);
115
+
116
+		curl_close($curl);
117
+
118
+		if ($err) {
119
+			echo "Can't connect to server, please check your internet connection.";
120
+			exit();
121
+		} else {
122
+			$json = json_decode($response, false)->rajaongkir;
123
+			if ($json->status->code == '400') {
124
+				throw new Exception($json->status->description);
125
+			} else {
126
+				$res = $json->results;
127
+
128
+				return $res;
129
+			}
130
+		}
131
+	}
132
+
133
+	protected static function get_courier($arr)
134
+	{
135
+		$origin = $arr['origin'];
136
+		$destination = $arr['destination'];
137
+		$weight = $arr['weight'];
138
+		$courier = $arr['courier'];
139
+		$res = self::curl_cost_get($origin, $destination, $weight, $courier);
140
+
141
+		return $res;
142
+	}
143
+
144
+	protected static function get_cost_details($arr)
145
+	{
146
+		$origin = $arr['origin'];
147
+		$destination = $arr['destination'];
148
+		$weight = $arr['weight'];
149
+		$courier = $arr['courier'];
150
+		$res = self::curl_cost_get($origin, $destination, $weight, $courier);
151
+
152
+		return $res[0]->costs;
153
+	}
154
+
155
+	private static function curl_cost_get($origin, $destination, $weight, $courier)
156
+	{
157
+		$curl = curl_init();
158
+
159
+		curl_setopt_array($curl, self::curl_cost_option($origin, $destination, $weight, $courier));
160
+
161
+		$response = curl_exec($curl);
162
+		$err = curl_error($curl);
163
+
164
+		curl_close($curl);
165
+
166
+		if ($err) {
167
+			echo "Can't connect to server, please check your internet connection.";
168
+			exit();
169
+		} else {
170
+			$json = json_decode($response, false)->rajaongkir;
171
+			if ($json->status->code == '400') {
172
+				throw new Exception($json->status->description);
173
+			} else {
174
+				$res = $json->results;
175
+
176
+				return $res;
177
+			}
178
+		}
179
+	}
180
+
181
+	private static function curl_cost_option($origin, $destination, $weight, $courier)
182
+	{
183
+		self::setup_option();
184
+
185
+		return [
186
+			CURLOPT_URL            => self::$url.'/cost',
187
+			CURLOPT_RETURNTRANSFER => true,
188
+			CURLOPT_ENCODING       => '',
189
+			CURLOPT_MAXREDIRS      => 10,
190
+			CURLOPT_TIMEOUT        => 30,
191
+			CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
192
+			CURLOPT_CUSTOMREQUEST  => 'POST',
193
+			CURLOPT_POSTFIELDS     => 'origin='.$origin.'&destination='.$destination.'&weight='.$weight.'&courier='.strtolower($courier),
194
+			CURLOPT_HTTPHEADER     => [
195
+				'content-type: application/x-www-form-urlencoded',
196
+				'key: '.self::$api_key,
197
+			],
198
+		];
199
+	}
200 200
 }
Please login to merge, or discard this patch.
config/config.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -1,9 +1,9 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-    /* Config for PHP Native or non-Framework */
3
+	/* Config for PHP Native or non-Framework */
4 4
 
5
-    $config = [
6
-        /*
5
+	$config = [
6
+		/*
7 7
     |--------------------------------------------------------------------------
8 8
     | End Point Api ( Server Configuration )
9 9
     |--------------------------------------------------------------------------
@@ -13,13 +13,13 @@  discard block
 block discarded – undo
13 13
     | Pro : http://pro.rajaongkir.com/api
14 14
     |
15 15
     */
16
-        'account_type' => 'starter',
17
-        /*
16
+		'account_type' => 'starter',
17
+		/*
18 18
     |--------------------------------------------------------------------------
19 19
     | API key
20 20
     |--------------------------------------------------------------------------
21 21
     | You can get API key in www.rajaongkir.com
22 22
     |
23 23
     */
24
-        'api_key' => 'your-api-key',
25
-    ];
24
+		'api_key' => 'your-api-key',
25
+	];
Please login to merge, or discard this patch.