Passed
Push — master ( 4dc21a...de2f8d )
by IRFA
02:13 queued 11s
created

Api::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 0
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
/*
4
    Raja Ongkir API
5
    Author: Irfa Ardiansyah <[email protected]>
6
*/
7
8
namespace Irfa\RajaOngkir\Ongkir\Func;
9
10
use Exception;
11
use Irfa\RajaOngkir\Caching\CacheCurl;
12
13
class Api extends CacheCurl
14
{
15
    private static $account_type;
16
    private static $api_key;
17
    private static $url;
18
    private static $count = 0;
0 ignored issues
show
introduced by
The private property $count is not used, and could be removed.
Loading history...
19
20
    private static function setup_option()
21
    {
22
        if (function_exists('config') and function_exists('app')) {//Load Config For Laravel
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to be never defined.
Loading history...
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, [
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, [
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
73
        $err = curl_error($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $err = curl_error(/** @scrutinizer ignore-type */ $curl);
Loading history...
74
75
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
76
77
        if ($err) {
78
            echo "Can't connect to server, please check your internet connection.";
79
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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, [
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, [
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
114
        $err = curl_error($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        $err = curl_error(/** @scrutinizer ignore-type */ $curl);
Loading history...
115
116
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
117
118
        if ($err) {
119
            echo "Can't connect to server, please check your internet connection.";
120
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        curl_setopt_array(/** @scrutinizer ignore-type */ $curl, self::curl_cost_option($origin, $destination, $weight, $courier));
Loading history...
160
161
        $response = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
162
        $err = curl_error($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_error() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
        $err = curl_error(/** @scrutinizer ignore-type */ $curl);
Loading history...
163
164
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
165
166
        if ($err) {
167
            echo "Can't connect to server, please check your internet connection.";
168
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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
}
201