Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
48 / 51
82.35% covered (warning)
82.35%
14 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
ControllerGen
94.12% covered (success)
94.12%
48 / 51
82.35% covered (warning)
82.35%
14 / 17
41.34
0.00% covered (danger)
0.00%
0 / 1
 getControllerName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getControllerAllArgs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getControllerParentArgs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getControllerArgs
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getFindById
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getQuery
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
6.05
 getSearch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getPager
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIndexVars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllVars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParentVars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCreateVars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEditVars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWith
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getSelects
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getValidations
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
8.04
 getStore
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace San\Crud\Generators;
4
5use San\Crud\Utils\NameUtils;
6use San\Crud\Utils\SchemaUtils;
7
8class ControllerGen extends BaseGen {
9
10    public function getControllerName() {
11        return NameUtils::getControllerName($this->tables);
12    }
13
14    public function getControllerAllArgs() {
15        return $this->getControllerArgs($this->tables);
16    }
17
18    public function getControllerParentArgs() {
19        return $this->getControllerArgs($this->parentTables());
20    }
21
22    public function getControllerArgs(array $tables) {
23        $value = join(', ', array_map(fn($table) => sprintf('%s $%s', NameUtils::getModelName((array) $table), NameUtils::getVariableName($table)), $tables));
24        return !empty($value) ? "$value," : "";
25    }
26
27    public function getFindById() {
28        return sprintf('$%s = %s::withTrashed()%s->find($%s_id);', $this->getVarName(), NameUtils::getModelName($this->mainTable()),
29            $this->hasUserId() ? "->where('user_id', auth()->id())" : "", NameUtils::getVariableName($this->mainTable()));
30    }
31
32    public function getQuery() {
33        if ($this->hasParentTable()) {
34            $code[] = sprintf('$%s = $%s->%s();', $this->getVarNamePlural(), $this->getParentVarName(), $this->mainTable());
35        } else {
36            $code[] = sprintf('$%s = %s::query();', $this->getVarNamePlural(), NameUtils::getModelName((array) $this->mainTable()));
37        }
38
39        foreach (array_slice($this->tables, -2) as $table) {
40            if (SchemaUtils::getUserIdField($table)) {
41                $code[] = sprintf("\t\t\$%s->where('%s', auth()->id());", $table === $this->mainTable() ? NameUtils::getVariableNamePlural($table) : NameUtils::getVariableName($table), SchemaUtils::getUserIdField($table));
42            }
43        }
44
45        if ($this->hasSoftDeletes()) {
46            $code[] = sprintf("\n\t\tif (!!\$request->trashed) {\n\t\t\t\$%s->withTrashed();\n\t\t}", $this->getVarNamePlural());
47        }
48
49        return join("\n", $code);
50    }
51
52    public function getSearch() {
53        return sprintf("if(!empty(\$request->search)) {\n\t\t\t\$%s->where('%s', 'like', '%%' . \$request->search . '%%');\n\t\t}", $this->getVarNamePlural(), SchemaUtils::firstHumanReadableField($this->mainTable(), 'id') ?: 'id');
54    }
55
56    public function getPager() {
57        return sprintf('$%s = $%s->paginate(10);', $this->getVarNamePlural(), $this->getVarNamePlural());
58    }
59
60    public function getIndexVars() {
61        return $this->getVars($this->parentTables(), (array) $this->getVarNamePlural());
62    }
63
64    public function getAllVars() {
65        return $this->getVars($this->tables);
66    }
67
68    public function getParentVars() {
69        return $this->getVars($this->parentTables());
70    }
71
72    public function getCreateVars() {
73        return $this->getVars($this->parentTables(), (array) array_map(fn($field) => $field['related_table'], (array) $this->getExternallyRelatedFields()));
74    }
75
76    public function getEditVars() {
77        return $this->getVars($this->tables, (array) array_map(fn($field) => $field['related_table'], (array) $this->getExternallyRelatedFields()));
78    }
79
80    public function getWith() {
81        foreach ($this->getExternallyRelatedFields() as $field) {
82            $code[] = sprintf('$%s->with(\'%s\');', $this->getVarNamePlural(), $field['relation']);
83        }
84
85        return join("\n\t\t", $code ?? []);
86    }
87
88    public function getSelects() {
89        foreach ($this->getExternallyRelatedFields() as $field) {
90            if (SchemaUtils::getUserIdField($field['related_table'])) {
91                $code[] = sprintf("\$%s = \App\Models\%s::where('%s', auth()->id())->get();", NameUtils::getVariableNamePlural($field['related_table']), NameUtils::getModelName($field['related_table']), SchemaUtils::getUserIdField($field['related_table']));
92            } else {
93                $code[] = sprintf("\$%s = \App\Models\%s::all();", NameUtils::getVariableNamePlural($field['related_table']), NameUtils::getModelName($field['related_table']));
94            }
95        }
96
97        return join("\n\t\t", $code ?? []);
98    }
99
100    public function getValidations(bool $edit) {
101        foreach ($this->getFillableFields() as $field) {
102            if (preg_match('/boolean|timestamp/', $field['type'])) continue;
103
104            if (!$field['nullable']) {
105                $validations[$field['id']] = 'required';
106            }
107
108            if (!empty($field['unique'])) {
109                $validations[$field['id']] .= (!empty($validations[$field['id']]) ? '|' : '') . "unique:{$this->mainTable()},{$field['id']}";
110                if ($edit) {
111                    $validations[$field['id']] .= ",\${$this->getVarName()}->id";
112                }
113            }
114        }
115
116        if (!empty($validations)) {
117            $keyValues = array_map(fn($key, $value) => sprintf('"%s" => "%s"', $key, $value), array_keys($validations), $validations);
118            return sprintf("[%s]", join(", ", $keyValues));
119        } else {
120            return '[]';
121        }
122    }
123
124    public function getStore($edit) {
125        foreach (SchemaUtils::getTableFields($this->mainTable()) as $field) {
126            if ($field['id'] === 'user_id') {
127                if (!$edit) $fills[] = sprintf("\$%s->user_id = auth()->id();", $this->getVarName());
128            } else if (in_array($field['related_table'] ?? '', $this->tables)) {
129                if (!$edit) $fills[] = sprintf("\$%s->%s = \$%s->id;", $this->getVarName(), $field['id'], NameUtils::getVariableName($field['related_table']));
130            } else {
131                $bool = preg_match('/bool/', $field['type']) ? '!!' : '';
132                $fills[] = sprintf("\$%s->%s = %s\$request->%s;", $this->getVarName(), $field['id'], $bool, $field['id']);
133            }
134        }
135
136        return join("\n\t\t", $fills ?? []);
137    }
138}