Passed
Push — master ( ee094a...0581a6 )
by IRFA
02:19 queued 11s
created

DBImport::extractor()   B

Complexity

Conditions 7
Paths 30

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 19
c 2
b 1
f 0
dl 0
loc 24
rs 8.8333
cc 7
nc 30
nop 2
1
<?php
2
3
namespace Irfa\RajaOngkir\Caching;
4
5
use Illuminate\Support\Facades\DB;
0 ignored issues
show
Bug introduced by
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...
6
7
class DBImport
8
{
9
    protected static $table_DB;
10
11
    protected static function import($table, $result, $type = 'prov')
12
    {
13
        self::$table_DB = $table;
14
        echo 'Creating cache... '.PHP_EOL;
15
        self::extractor($result, $type);
16
    }
17
18
    private static function extractor($result, $type)
19
    {
20
        try {
21
            foreach ($result as $r) {
22
                if ($type == 'prov') {
23
                    $fill = ['province_id' => $r->province_id, 'province' => $r->province];
24
                    $where = ['province_id' => $r->province_id];
25
                } elseif ($type == 'city') {
26
                    $fill = ['city_id'=>$r->city_id, 'province_id'=>$r->province_id, 'province' => $r->province, 'type'=>$r->type, 'city_name'=>$r->city_name, 'postal_code'=>$r->postal_code];
27
                    $where = ['city_id' => $r->city_id];
28
                } elseif ($type == 'subdistrict') {
29
                    $fill = ['subdistrict_id'=>$r->subdistrict_id, 'province_id'=>$r->province_id, 'province' => $r->province, 'city_id'=>$r->city_id, 'city'=>$r->city, 'type'=>$r->type,'subdistrict_name'=>$r->subdistrict_name];
30
                    $where = ['subdistrict_id' => $r->subdistrict_id];
31
                }
32
                if (DB::table(self::$table_DB)->where($where)->count() == 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $where does not seem to be defined for all execution paths leading up to this point.
Loading history...
33
                    DB::table(self::$table_DB)->insert($fill);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fill does not seem to be defined for all execution paths leading up to this point.
Loading history...
34
                } else {
35
                    DB::table(self::$table_DB)->where($where)->update($fill);
36
                }
37
            }
38
            $count = DB::table(self::$table_DB)->count();
39
            echo 'Cache has been created, '.$count.' row(s) affected.';
40
        } catch (\Exception $e) {
41
            echo "\033[91mCan't creating cache. Error\033[0m : ".$e;
42
        }
43
    }
44
}
45