Passed
Pull Request — master (#1121)
by Iman
03:55
created

ControllerGenerator::generateControllerCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 9
rs 9.6666
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
use CRUDBooster;
0 ignored issues
show
Bug introduced by
The type CRUDBooster 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...
10
use CB;
11
12
class ControllerGenerator
13
{
14
    public static function generateController($table, $name = null){
15
16
        $controllerName = self::getControllerName($table, $name);
17
18
        $php = self::generateControllerCode($table, $controllerName);
19
        //create file controller
20
        FileManipulator::putCtrlContent('Admin'.$controllerName, $php);
21
22
        return 'Admin'.$controllerName;
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 $coloms
52
     * @param $pk
53
     * @return array
54
     */
55
    private static function addCol($table, $coloms, $pk)
56
    {
57
        $coloms_col = array_slice($coloms, 0, 8);
58
        $joinList = [];
59
        $cols = [];
60
61
        foreach ($coloms_col as $c) {
62
            $label = str_replace("id_", "", $c);
63
            $label = ucwords(str_replace("_", " ", $label));
64
            $label = str_replace('Cms ', '', $label);
65
            $field = $c;
66
67
            if (FieldDetector::isExceptional($field) || FieldDetector::isPassword($field)) {
68
                continue;
69
            }
70
71
            if (FieldDetector::isForeignKey($field)) {
72
                $jointable = str_replace(['id_', '_id'], '', $field);
73
74
                if (Schema::hasTable($jointable)) {
75
                    $joincols = DbInspector::getTableCols($jointable);
76
                    $joinname = DbInspector::colName($joincols);
77
                    $cols[] = ['label' => $label, 'name' =>  $jointable.$joinname];
78
                    $jointablePK = DbInspector::findPk($jointable);
79
                    $joinList[] = [
80
                        'table' => $jointable,
81
                        'field1' => $jointable.'.'.$jointablePK,
82
                        'field2' => $table.'.'.$pk,
83
                    ];
84
                }
85
            } else {
86
                $image = '';
87
                if (FieldDetector::isImage($field)) {
88
                    $image = '"image" => true';
89
                }
90
                $cols[] = ['label' => $label, 'name' =>  "'$field', $image"];
91
            }
92
        }
93
94
        return [$cols, $joinList];
95
    }
96
97
    /**
98
     * @param $table
99
     * @param $controllerName
100
     * @return string
101
     * @throws \Exception
102
     * @throws \Throwable
103
     */
104
    private static function generateControllerCode($table, $controllerName)
105
    {
106
        $coloms = DbInspector::getTableCols($table);
107
        $pk = DbInspector::findPk($table);
108
        $formArrayString = FormConfigGenerator::generateFormConfig($table, $coloms);
109
        list($cols, $joinList) = self::addCol($table, $coloms, $pk);
110
111
        $data = compact('controllerName', 'table', 'pk', 'coloms', 'cols', 'formArrayString', 'joinList');
112
        return '<?php '.view('CbModulesGen::controller_stub', $data)->render();
113
    }
114
}