Completed
Push — master ( 250f6a...af675f )
by Sam
07:31
created

StoreRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 28.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 27
loc 94
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
4
use Illuminate\Support\Str;
5
use Symfony\Component\Console\Input\InputOption;
6
7
class StoreRequest extends ClassGenerator {
8
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $name = 'jig:generate:request:store';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Generate a Store request class';
22
23
    /**
24
     * Path for view
25
     *
26
     * @var string
27
     */
28
    protected string $view = 'store-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...
29
30
    /**
31
     * Execute the console command.
32
     *
33
     * @return void
34
     */
35
    public function handle()
36
    {
37
        $force = $this->option('force');
38
39
        //TODO check if exists
40
        //TODO make global for all generator
41
        //TODO also with prefix
42
        if(!empty($template = $this->option('template'))) {
43
            $this->view = 'templates.'.$template.'.store-request';
44
        }
45
46
        if(!empty($belongsToMany = $this->option('belongs-to-many'))) {
47
            $this->setBelongToManyRelation($belongsToMany);
48
        }
49
50
        if ($this->generateClass($force)){
51
            $this->info('Generating '.$this->classFullName.' finished');
52
        }
53
    }
54
55
    protected function buildClass() : string {
56
57
        $this->setBelongsToRelations();
58
        return view('jig::'.$this->view, [
59
            'modelBaseName' => $this->modelBaseName,
60
            'modelDotNotation' => $this->modelDotNotation,
61
            'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault,
62
            'tableName' => $this->tableName,
63
            'modelFullName'                 => $this->modelFullName,
64
65
            // validation in store/update
66
            'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName),
67
            'translatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
68
                return $column['type'] == "json";
69
            })->pluck('name'),
70
            'relatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
71
                return in_array($column['name'],$this->relations["belongsTo"]->pluck('foreign_key')->toArray());
72
            })->keyBy('name'),
73
            'relations' => $this->relations,
74
        ])->render();
75
    }
76
77
    protected function getOptions() {
78
        return [
79
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'],
80
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
81
            ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'],
82
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'],
83
        ];
84
    }
85
86
    public function generateClassNameFromTable($tableName): string
87
    {
88
        return 'Store'.$this->modelBaseName;
89
    }
90
91
    /**
92
     * Get the default namespace for the class.
93
     *
94
     * @param string $rootNamespace
95
     * @return string
96
     */
97
    protected function getDefaultNamespace(string $rootNamespace) : string
98
    {
99
        return $rootNamespace.'\Http\Requests\\'.$this->modelWithNamespaceFromDefault;
100
    }
101
}
102