Passed
Push — master ( 719292...2fce0a )
by Sebastien
05:40
created

Views::buildViews()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 10
nop 1
dl 0
loc 44
rs 8.8337
c 0
b 0
f 0
1
<?php
2
3
namespace Sebastienheyd\BoilerplatePackager\Commands\Crud;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Str;
7
8
class Views extends Command
9
{
10
    protected $signature = 'boilerplate:packager:crud:views {package} {tables}';
11
    protected $description = '';
12
13
    public function handle()
14
    {
15
        foreach ($this->argument('tables') as $table) {
16
            $this->buildViews($table);
17
        }
18
    }
19
20
    private function buildViews($table)
21
    {
22
        $package = $this->argument('package');
23
        [$vendor, $packageName] = explode('/', $package);
0 ignored issues
show
Bug introduced by
It seems like $package can also be of type null and string[]; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

23
        [$vendor, $packageName] = explode('/', /** @scrutinizer ignore-type */ $package);
Loading history...
24
        $columns = $this->getColumnsFromTable($table);
25
        $relations = $this->getTableRelations($table);
26
27
        $fields = [];
28
        foreach ($columns as $column) {
29
            if (in_array($column['name'], ['created_at', 'updated_at', 'deleted_at'])) {
30
                continue;
31
            }
32
33
            if (preg_match('#_id$#', $column['name'])) {
34
                continue;
35
            }
36
37
            $rules = [$column['required'] ? 'required' : 'nullable'];
38
39
            $fields[] = [
40
                'name' => $column['name'],
41
                'type' => $column['type'],
42
                'rules' => join('|', $rules),
43
            ];
44
        }
45
46
        $data = [
47
            'namespace' => $this->getNamespace($package),
48
            'resource' => $table,
49
            'vendor' => $vendor,
50
            'packageName' => $packageName,
51
            'fields' => $fields,
52
            'relations' => $relations,
53
        ];
54
55
        $path = __DIR__.'/../../resources/views/resource';
56
        $files = (new Filesystem())->allFiles($path);
57
58
        foreach ($files as $file) {
59
            $dest = str_replace($path, '', $file);
60
            $view = str_replace(['.blade.php', '/'], '', $dest);
61
62
            $content = (string) view('packager::resource.'.$view, $data);
63
            $this->storage->put($package.'/src/resources/views/'.Str::singular($table).$dest, $content);
64
        }
65
    }
66
}
67