Passed
Push — master ( aa64af...5cd990 )
by Iman
04:21
created

AdminColumnsTableController::getColumnTable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 2
dl 0
loc 20
rs 9.2
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\DB;
8
9
class AdminColumnsTableController extends CBController
10
{
11
    public function cbInit()
12
    {
13
        $this->table = 'cms_apicustom';
14
        $this->primaryKey = "id";
15
        $this->titleField = "nama";
16
        $this->buttonShow = false;
17
        $this->deleteBtn = false;
18
        $this->buttonAdd = false;
19
        $this->buttonImport = false;
20
        $this->buttonExport = false;
21
    }
22
23
    public function getColumnTable($table, $type = 'list')
24
    {
25
        $this->cbLoader();
26
27
        $columns = array_filter(\Schema::getColumnListing($table), function ($colName) {
28
            return ! (in_array($colName, ['created_at', 'deleted_at', 'updated_at']));
29
        });
30
31
        $newResult = [];
32
        foreach ($columns as $colName) {
33
            $newResult[] = ['name' => $colName, 'type' => $this->getFieldType($colName, \Schema::getColumnType($table, $colName))];
34
35
            if (! in_array($type, ['list', 'detail']) || ! starts_with($colName, 'id_')) {
36
                continue;
37
            }
38
            $relatedTable = str_after($colName, 'id_');
39
            $newResult = $this->addRelatedTableColTypes($relatedTable, $newResult);
40
        }
41
42
        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

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