Issues (64)

src/Caching/ROCache.php (11 issues)

1
<?php
2
3
/*
4
    Raja Ongkir API
5
    Author: Irfa Ardiansyah <[email protected]>
6
*/
7
8
namespace Irfa\RajaOngkir\Caching;
9
10
use Illuminate\Support\Facades\Cache;
0 ignored issues
show
The type Illuminate\Support\Facades\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Illuminate\Support\Facades\DB;
0 ignored issues
show
The type Illuminate\Support\Facades\DB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Illuminate\Support\Facades\Schema;
0 ignored issues
show
The type Illuminate\Support\Facades\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class ROCache
15
{
16
    private static $prov;
17
    private static $city;
18
19
    public static function cacheFile($name, $find = null)
20
    {
21
        $cache = Cache::get('ro-cache-'.$name);
22
        if ($find != null) {
23
            $cache = collect($cache)->where('province_id', $find['province_id']);
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

23
            $cache = /** @scrutinizer ignore-call */ collect($cache)->where('province_id', $find['province_id']);
Loading history...
24
        }
25
26
        return $cache;
27
    }
28
29
    public static function clearCache()
30
    {
31
        self::$prov = config('irfa.rajaongkir.province_table');
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

31
        self::$prov = /** @scrutinizer ignore-call */ config('irfa.rajaongkir.province_table');
Loading history...
32
        self::$city = config('irfa.rajaongkir.city_table');
33
        $cache_type = strtolower(config('irfa.rajaongkir.cache_type'));
34
        if ($cache_type == 'database') {
35
            if (Schema::hasTable(self::$city) and Schema::hasTable(self::$prov)) {
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...
36
                echo 'Clearing Cache...'.PHP_EOL;
37
                self::clearTable();
38
                echo 'Cache Cleared.';
39
            } else {
40
                echo 'Failed. Cache table not found.';
41
42
                return false;
43
            }
44
        } elseif ($cache_type == 'file') {
45
            echo 'Clearing Cache...'.PHP_EOL;
46
            self::clearFile();
47
            echo 'Cache Cleared.';
48
        } else {
49
            echo 'Failed. Cache type not support.';
50
51
            return false;
52
        }
53
        self::$prov = null;
54
        self::$city = null;
55
    }
56
57
    private static function clearTable()
58
    {
59
        DB::table(self::$prov)->truncate();
60
        DB::table(self::$city)->truncate();
61
    }
62
63
    private static function clearFile()
64
    {
65
        Cache::forget('ro-cache-'.self::$city);
66
        Cache::forget('ro-cache-'.self::$prov);
67
    }
68
69
    public static function checkProv()
70
    {
71
        $table = config('irfa.rajaongkir.province_table');
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

71
        $table = /** @scrutinizer ignore-call */ config('irfa.rajaongkir.province_table');
Loading history...
72
        if (Schema::hasTable($table)) {
73
            $count = DB::table($table)->count();
74
            if ($count > 0) {
75
                return true;
76
            } else {
77
                return false;
78
            }
79
        } else {
80
            return false;
81
        }
82
    }
83
84
    public static function checkCity()
85
    {
86
        $table = config('irfa.rajaongkir.city_table');
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

86
        $table = /** @scrutinizer ignore-call */ config('irfa.rajaongkir.city_table');
Loading history...
87
        if (Schema::hasTable($table)) {
88
            $count = DB::table($table)->count();
89
            if ($count > 0) {
90
                return true;
91
            } else {
92
                return false;
93
            }
94
        } else {
95
            return false;
96
        }
97
    }
98
99
    public static function getProv($arr)
100
    {
101
        $db = DB::table(config('irfa.rajaongkir.province_table'));
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
        $db = DB::table(/** @scrutinizer ignore-call */ config('irfa.rajaongkir.province_table'));
Loading history...
102
        if (!empty($arr)) {
103
            $db->where($arr);
104
        }
105
        $ret = $db->orderBy('province', 'DESC')->get();
106
107
        return $ret;
108
    }
109
110
    public static function getSubdistrict($arr)
111
    {
112
        $db = DB::table(config('irfa.rajaongkir.subdistrict_table'));
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

112
        $db = DB::table(/** @scrutinizer ignore-call */ config('irfa.rajaongkir.subdistrict_table'));
Loading history...
113
        if (!empty($arr)) {
114
            $db->where($arr);
115
        }
116
        $ret = $db->orderBy('subdistrict_name', 'DESC')->get();
117
118
        return $ret;
119
    }
120
121
    public static function getCity($arr)
122
    {
123
        $db = DB::table(config('irfa.rajaongkir.city_table'));
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

123
        $db = DB::table(/** @scrutinizer ignore-call */ config('irfa.rajaongkir.city_table'));
Loading history...
124
        if (!empty($arr)) {
125
            $db->where($arr);
126
        }
127
        $ret = $db->orderBy('city_name', 'ASC')->get();
128
129
        return $ret;
130
    }
131
}
132