Completed
Push — master ( 105fde...b25eac )
by Sam
01:40
created

IndexRequest::buildClass()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8333
c 0
b 0
f 0
cc 7
nc 1
nop 0
1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Symfony\Component\Console\Input\InputOption;
4
5
class IndexRequest extends ClassGenerator {
6
7
    /**
8
     * The name and signature of the console command.
9
     *
10
     * @var string
11
     */
12
    protected $name = 'jig:generate:request:index';
13
14
    /**
15
     * The console command description.
16
     *
17
     * @var string
18
     */
19
    protected $description = 'Generate an Index request class';
20
    protected string $view  = 'index-request';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
21
22
    /**
23
     * Execute the console command.
24
     *
25
     * @return void
26
     */
27
    public function handle()
28
    {
29
        $force = $this->option('force');
30
31
        if(!empty($template = $this->option('template'))) {
32
            $this->view = 'templates.'.$template.'.index-request';
33
        }
34
35
        if ($this->generateClass($force)){
36
            $this->info('Generating '.$this->classFullName.' finished');
37
        }
38
    }
39
40
    protected function buildClass() :string {
41
42
        return view('jig::'.$this->view, [
43
            'modelBaseName'                 => $this->modelBaseName,
44
            'modelDotNotation'              => $this->modelDotNotation,
45
            'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault,
46
            'modelVariableName'             => $this->modelVariableName,
47
            'modelFullName'                 => $this->modelFullName,
48
            'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
49
                return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "slug" || $column['name'] == "created_at" || $column['name'] == "updated_at" || $column['name'] == "deleted_at");
50
            })->pluck('name')->toArray(),
51
        ])->render();
52
    }
53
54
    protected function getOptions() {
55
        return [
56
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
57
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'],
58
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'],
59
        ];
60
    }
61
62
    public function generateClassNameFromTable($tableName): string
63
    {
64
        return 'Index'.$this->modelBaseName;
65
    }
66
67
    /**
68
     * Get the default namespace for the class.
69
     *
70
     * @param string $rootNamespace
71
     * @return string
72
     */
73
    protected function getDefaultNamespace(string $rootNamespace) :string
74
    {
75
        return $rootNamespace.'\Http\Requests\\'.$this->modelWithNamespaceFromDefault;
76
    }
77
78
}
79