Passed
Push — master ( 699130...956a40 )
by Reza
04:10
created

StubParser   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 15
Bugs 2 Features 9
Metric Value
wmc 34
eloc 111
c 15
b 2
f 9
dl 0
loc 214
rs 9.68

17 Methods

Rating   Name   Duplication   Size   Complexity  
A parseBlade() 0 12 1
A uploadCodeParser() 0 12 2
A parseModel() 0 7 2
A parseInputsInBlade() 0 14 2
A parseValidationRules() 0 10 2
A parseProperties() 0 9 2
A inputsHTML() 0 14 2
A getConfig() 0 4 2
A parsePropertiesValue() 0 10 2
A parseExtraValues() 0 9 2
A getDefaultNamespace() 0 6 1
A makeTab() 0 5 2
A parseFields() 0 13 4
A getModelName() 0 5 1
A replaceModel() 0 19 1
A parseTitlesInBlade() 0 15 3
A parseDataInBlade() 0 15 3
1
<?php
2
3
namespace EasyPanel\Commands\Actions;
4
5
use InvalidArgumentException;
6
7
trait StubParser
8
{
9
10
    public function getDefaultNamespace($rootNamespace)
11
    {
12
        $name = ucfirst($this->getNameInput());
0 ignored issues
show
Bug introduced by
It seems like getNameInput() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

12
        $name = ucfirst($this->/** @scrutinizer ignore-call */ getNameInput());
Loading history...
13
        $this->path = parent::getDefaultNamespace($rootNamespace)."\\Http\\Livewire\\Admin\\$name";
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
14
15
        return $this->path;
16
    }
17
18
    public function replaceModel($stub)
19
    {
20
        $fields = $this->getConfig('fields');
21
22
        $modelNamespace = $this->parseModel($this->getConfig('model'));
23
        $modelName = $this->getModelName($modelNamespace);
24
25
        $array = [
26
            '{{ modelName }}' => $modelName,
27
            '{{ modelNamespace }}' => $modelNamespace,
28
            '{{ uploadFile }}' => $this->uploadCodeParser($fields),
29
            '{{ model }}' => strtolower($modelName),
30
            '{{ properties }}' => $this->parseProperties($fields),
31
            '{{ rules }}' => $this->parseValidationRules(),
32
            '{{ fields }}' => $this->parseFields($fields),
33
            '{{ setProperties }}' => $this->parsePropertiesValue($fields),
34
        ];
35
36
        return str_replace(array_keys($array), array_values($array), $stub);
37
    }
38
39
    public function parseBlade($stub){
40
        $modelNamespace = $this->parseModel($this->getConfig('model'));
41
        $modelName = $this->getModelName($modelNamespace);
42
        $array = [
43
            '{{ model }}' => strtolower($modelName),
44
            '{{ modelName }}' => $modelName,
45
            '{{ data }}' => $this->parseDataInBlade($modelName),
46
            '{{ titles }}' => $this->parseTitlesInBlade(),
47
            '{{ inputs }}' => $this->parseInputsInBlade(),
48
        ];
49
50
        return str_replace(array_keys($array), array_values($array), $stub);
51
    }
52
53
    public function parseModel($model)
54
    {
55
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
56
            throw new InvalidArgumentException('Model name contains invalid characters.');
57
        }
58
59
        return $this->qualifyModel($model);
0 ignored issues
show
Bug introduced by
It seems like qualifyModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

59
        return $this->/** @scrutinizer ignore-call */ qualifyModel($model);
Loading history...
60
    }
61
62
    public function getConfig($key, $action = null){
63
        $action = $action ?? $this->getNameInput();
64
65
        return config("easy_panel.crud.$action.$key") ?: [];
66
    }
67
68
    public function getModelName($modelNamespace)
69
    {
70
        $array = explode('\\', $modelNamespace);
71
72
        return end($array);
73
    }
74
75
    public function parseProperties($fields)
76
    {
77
        $fields = array_keys($fields);
78
        $str = '';
79
        foreach ($fields as $field) {
80
            $str .= 'public $'.$field.";".$this->makeTab(1);
81
        }
82
83
        return $str;
84
    }
85
86
    public function uploadCodeParser($fields)
87
    {
88
        $filesInput = array_keys($fields, 'file');
89
        $str = '';
90
        foreach ($filesInput as $file) {
91
            $storePath = $this->getConfig('store')[$file] ?? "{$file}/";
92
            $str .= $this->makeTab(2).'if($this->getPropertyValue(\''.$file.'\') and is_object($this->'.$file.')) {'.$this->makeTab(3);
93
            $str .= '$this->'.$file.' = $this->getPropertyValue(\''.$file.'\')->store(\''.$storePath.'\');'.$this->makeTab(2);
94
            $str .= '}'.PHP_EOL;
95
        }
96
97
        return $str;
98
    }
99
100
    public function parsePropertiesValue($fields)
101
    {
102
        $fields = array_keys($fields);
103
        $str = '';
104
        $action = $this->getNameInput();
105
        foreach ($fields as $field) {
106
            $str .= '$this->'.$field.' = $this->'.$action.'->'.$field.';'.$this->makeTab(2, end($fields) != $field);
107
        }
108
109
        return $str;
110
    }
111
112
    public function parseValidationRules()
113
    {
114
        $rules = $this->getConfig('validation');
115
116
        $str = '';
117
        foreach ($rules as $key => $rule) {
118
            $str .= "'$key' => '$rule',".$this->makeTab(2, $rule != end($rules));
119
        }
120
121
        return $str;
122
    }
123
124
    public function parseFields($fields)
125
    {
126
        $str = '';
127
        foreach ($fields as $key => $field) {
128
            $str .= $field != end($fields) ? "'$key' => " . '$this' . "->$key,".$this->makeTab(3) : "'$key' => " . '$this' . "->$key,";
129
        }
130
131
        $model = $this->getNameInput();
132
        if(config("easy_panel.crud.$model.extra_values")){
133
            $str .= $this->parseExtraValues();
134
        }
135
136
        return $str;
137
    }
138
139
    public function parseExtraValues(){
140
        $str = '';
141
142
        $values = $this->getConfig('extra_values');
143
        foreach ($values as $key => $value) {
144
            $str .= $this->makeTab(3, end($values) != $values)."'$key' => $value,";
145
        }
146
147
        return $str;
148
    }
149
150
    public function parseDataInBlade($modelName)
151
    {
152
        $fields = $this->getConfig('show');
153
        $str = '';
154
        $modelName = strtolower($modelName);
155
        foreach ($fields as $value) {
156
            if (!is_array($value)) {
157
                $str .= '<td> {{ $' . $modelName . '->' . $value . " }} </td>" . $this->makeTab(1, end($fields) != $value);
158
            } else {
159
                $relationName = array_key_first($value);
160
                $str .= '<td> {{ $' . $modelName . '->' . $relationName . '->'. $value[array_key_first($value)] .' }} </td>' . $this->makeTab(1, end($fields) != $value);
161
            }
162
        }
163
164
        return $str;
165
    }
166
167
    public function parseTitlesInBlade()
168
    {
169
        $fields = $this->getConfig('show');
170
        $str = '';
171
        foreach ($fields as $field) {
172
            if (!is_array($field)) {
173
                $field = ucfirst($field);
174
            } else {
175
                $relationName = array_key_first($field);
176
                $field = ucfirst($relationName). ' ' . ucfirst($field[array_key_first($field)]);
177
            }
178
            $str .= "<td> $field </td>".$this->makeTab(6, end($fields) != $field);
179
        }
180
181
        return $str;
182
    }
183
184
    public function parseInputsInBlade()
185
    {
186
        $fields = $this->getConfig('fields');
187
188
        $str = '';
189
        foreach ($fields as $key => $type) {
190
            $str .= '<div class="form-group">'.$this->makeTab(4);
191
            $str .= '<label for="input'.$key.'" class="col-sm-2 control-label">'.ucfirst($key).'</label>'.$this->makeTab(4);
192
            $str = $this->inputsHTML($type, $key, $str).$this->makeTab(4);
193
            $str .= '@error("'.$key.'") <div class="invalid-feedback">{{ $message }}</div> @enderror'.$this->makeTab(3);
194
            $str .= '</div>'.$this->makeTab(3);
195
        }
196
197
        return $str;
198
    }
199
200
    public function inputsHTML($type, $key, string $str): string
201
    {
202
        $mode = config('easy_panel.lazy_mode') ? 'wire:model.lazy' : 'wire:model';
203
        $array = [
204
            'text' => '<input type="text" '. $mode .'="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
205
            'email' => '<input type="email" '. $mode .'="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
206
            'number' => '<input type="number" '. $mode .'="' . $key . '" class="form-control @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
207
            'file' => '<input type="file" wire:model="' . $key . '" class="form-control-file @error(\''.$key.'\')is-invalid @enderror" id="input' . $key . '">',
208
            'textarea' => '<textarea '.$mode.'="' . $key . '" class="form-control @error(\''.$key.'\')is-invalid @enderror"></textarea>',
209
            'password' => '<input type="password" '. $mode .'="' . $key . '" class="form-control  @error(\''.$key.'\') is-invalid @enderror" id="input' . $key . '">',
210
        ];
211
        $str .= $array[$type];
212
213
        return $str;
214
    }
215
216
    public function makeTab($count, $newLine = true){
217
        $count = $count * 4;
218
        $tabs = str_repeat(' ', $count);
219
220
        return $newLine ? "\n".$tabs : $tabs;
221
    }
222
223
}
224