Passed
Push — master ( 71b23e...5dcda6 )
by Iman
05:33
created

AdminColumnsTableController::prepareResults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator\FieldDetector;
7
use Illuminate\Support\Facades\Request;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Route;
10
11
class AdminColumnsTableController extends CBController
12
{
13
    public function cbInit()
14
    {
15
        $this->table = 'cms_apicustom';
16
        $this->primaryKey = "id";
17
        $this->title_field = "nama";
18
        $this->buttonShow = false;
19
        $this->button_new = false;
0 ignored issues
show
Bug Best Practice introduced by
The property button_new does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->deleteBtn = false;
21
        $this->buttonAdd = false;
22
        $this->button_import = false;
23
        $this->buttonExport = false;
24
    }
25
26
    public function getColumnTable($table, $type = 'list')
27
    {
28
        $this->cbLoader();
29
        $except = ['created_at', 'deleted_at', 'updated_at'];
30
31
        $result = \Schema::getColumnListing($table);
32
        $newResult = [];
33
        foreach ($result as $row) {
34
35
            if (in_array($row, $except)) {
36
                continue;
37
            }
38
            $type_field = \Schema::getColumnType($table, $row);
39
            $newResult[] = ['name' => $row, 'type' => $this->getFieldType($row, $type_field)];
40
41
            if (! in_array($type, ['list', 'detail']) || ! starts_with($row, 'id_')) {
42
                continue;
43
            }
44
45
            $newResult = $this->prepareResults($row, $newResult);
46
        }
47
48
        return response()->json($newResult);
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

48
        return response()->/** @scrutinizer ignore-call */ json($newResult);
Loading history...
49
    }
50
51
    /**
52
     * @param $ro string
53
     * @param $default string
54
     * @return string
55
     */
56
    private function getFieldType($ro, $default)
57
    {
58
        $MAP = [
59
            'isEmail' => "email",
60
            'isImage' => "image",
61
            'isPassword' => "password",
62
            'isForeignKey' => "integer",
63
        ];
64
65
        foreach ($MAP as $methodName => $type) {
66
            if (FieldDetector::$methodName($ro)) {
67
                return $type;
68
            }
69
        }
70
71
        return $default;
72
    }
73
74
    /**
75
     * @param $table2
76
     * @param $ro
77
     * @param $new_result
78
     * @return array
79
     */
80
    private function prepareResults($ro, $new_result)
81
    {
82
        if (starts_with($ro, 'id_')) {
83
            return $new_result;
84
        }
85
        $table2 = substr($ro, 3);
86
        $columns = DB::getSchemaBuilder()->getColumnListing($table2);
87
        $columns = array_filter($columns, function ($col) {
88
            return ! FieldDetector::isExceptional($col);
89
        });
90
91
        foreach ($columns as $col) {
92
            $col = str_replace("_$table2", "", $col);
93
            $new_result[] = ['name' => $table2.'_'.$col, 'type' => \Schema::getColumnType($table2, $col)];
94
        }
95
96
        return $new_result;
97
    }
98
}
99