Passed
Push — master ( 6bd04a...b9eac4 )
by Iman
04:02
created

ControllerGenerator::handleForeignKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 6
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
use crocodicstudio\crudbooster\helpers\DbInspector;
6
use crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator\FormConfigGenerator;
7
use crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator\FieldDetector;
8
use Schema;
9
10
class ControllerGenerator
11
{
12
    public static function generateController($table, $name = null)
13
    {
14
15
        $controllerName = self::getControllerName($table, $name);
16
17
        $php = self::generateControllerCode($table, $controllerName);
18
        //create file controller
19
        FileManipulator::putCtrlContent('Admin'.$controllerName, $php);
20
21
        return 'Admin'.$controllerName;
22
    }
23
24
    /**
25
     * @param $table
26
     * @param $name
27
     * @return string
28
     */
29
    private static function getControllerName($table, $name)
30
    {
31
        $controllername = ucwords(str_replace('_', ' ', $table));
32
        $controllername = str_replace(' ', '', $controllername).'Controller';
33
        if ($name) {
34
            $controllername = ucwords(str_replace(['_', '-'], ' ', $name));
35
            $controllername = str_replace(' ', '', $controllername).'Controller';
36
        }
37
38
        $countSameFile = count(glob(base_path(controllers_dir()).'Admin'.$controllername.'.php'));
39
40
        if ($countSameFile != 0) {
41
            $suffix = $countSameFile;
42
            $controllername = ucwords(str_replace(['_', '-'], ' ', $name)).$suffix;
43
            $controllername = str_replace(' ', '', $controllername).'Controller';
44
        }
45
46
        return $controllername;
47
    }
48
49
    /**
50
     * @param $table
51
     * @param $controllerName
52
     * @return string
53
     * @throws \Exception
54
     * @throws \Throwable
55
     */
56
    private static function generateControllerCode($table, $controllerName)
57
    {
58
        $coloms = \Schema::getColumnListing($table);
59
        $pk = DbInspector::findPk($table);
60
        $formArrayString = FormConfigGenerator::generateFormConfig($table, $coloms);
61
        list($cols, $joinList) = self::addCol($table, $coloms, $pk);
62
63
        $data = compact('controllerName', 'table', 'pk', 'coloms', 'cols', 'formArrayString', 'joinList');
64
65
        return '<?php '.view('CbModulesGen::controller_stub', $data)->render();
66
    }
67
68
    /**
69
     * @param $table
70
     * @param $coloms
71
     * @param $pk
72
     * @return array
73
     */
74
    private static function addCol($table, $coloms, $pk)
75
    {
76
        $coloms_col = array_slice($coloms, 0, 8);
77
        $joinList = [];
78
        $cols = [];
79
        array_filter($coloms_col, function ($field) {
80
            return (! FieldDetector::isExceptional($field) && ! FieldDetector::isPassword($field));
81
        });
82
83
        foreach ($coloms_col as $field) {
84
            $label = str_replace("id_", "", $field);
85
            $label = ucwords(str_replace("_", " ", $label));
86
            $label = str_replace('Cms ', '', $label);
87
88
            if (FieldDetector::isForeignKey($field)) {
89
                list($cols, $joinList) = self::handleForeignKey($table, $pk, $field, $label, $cols, $joinList);
90
            } else {
91
                $cols = self::handleOtherFields($field, $label, $cols);
92
            }
93
        }
94
95
        return [$cols, $joinList];
96
    }
97
98
    /**
99
     * @param $table
100
     * @param $pk
101
     * @param $field
102
     * @param $label
103
     * @param $cols
104
     * @param $joinList
105
     * @return array
106
     * @throws \Exception
107
     */
108
    private static function handleForeignKey($table, $pk, $field, $label, $cols, $joinList)
109
    {
110
        $jointable = str_replace(['id_', '_id'], '', $field);
111
112
        if (! Schema::hasTable($jointable)) {
113
            return [$cols, $joinList];
114
        }
115
        $joincols = \Schema::getColumnListing($jointable);
116
        $joinname = DbInspector::colName($joincols);
117
        $cols[] = ['label' => $label, 'name' => $jointable.$joinname];
118
        $jointablePK = DbInspector::findPk($jointable);
119
        $joinList[] = [
120
            'table' => $jointable,
121
            'field1' => $jointable.'.'.$jointablePK,
122
            'field2' => $table.'.'.$pk,
123
        ];
124
125
        return [$cols, $joinList];
126
    }
127
128
    /**
129
     * @param $field
130
     * @param $label
131
     * @param $cols
132
     * @return array
133
     */
134
    private static function handleOtherFields($field, $label, $cols)
135
    {
136
        $image = '';
137
        if (FieldDetector::isImage($field)) {
138
            $image = '"image" => true';
139
        }
140
        $cols[] = ['label' => $label, 'name' => "'$field', $image"];
141
142
        return $cols;
143
    }
144
}