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

Repository::buildClass()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 40

Duplication

Lines 3
Ratio 7.5 %

Importance

Changes 0
Metric Value
dl 3
loc 40
rs 7.6666
c 0
b 0
f 0
cc 10
nc 1
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators;
2
3
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
4
use Savannabits\JetstreamInertiaGenerator\Generators\Traits\FileManipulations;
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Arr;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class Repository extends ClassGenerator {
10
11
    use FileManipulations;
12
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'jig:generate:repository';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Generate an Repository class';
26
27
    /**
28
     * Path for view
29
     *
30
     * @var string
31
     */
32
    protected string $view = 'repository';
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...
33
34
    /**
35
     * Controller has also export method
36
     *
37
     * @return mixed
38
     */
39
    protected bool $export = false;
40
41
    /**
42
     * Controller has also bulk options method
43
     *
44
     * @return mixed
45
     */
46
    protected bool $withoutBulk = false;
47
48
    public function handle()
49
    {
50
        $force = $this->option('force');
51
52
        if($this->option('with-export')){
53
            $this->export = true;
54
        }
55
56
        if($this->option('without-bulk')){
57
            $this->withoutBulk = true;
58
        }
59
60
        if(!empty($belongsToMany = $this->option('belongs-to-many'))) {
61
            $this->setBelongToManyRelation($belongsToMany);
62
        }
63
64
        if ($this->generateClass($force)){
65
66
            $this->info('Generating '.$this->classFullName.' finished');
67
        }
68
69
    }
70
71
    protected function buildClass(): string {
72
73
        //Set belongsTo Relations
74
        $this->setBelongsToRelations();
75
76
        return view('jig::'.$this->view, [
77
            'repoBaseName' => $this->classBaseName,
78
            'repoNamespace' => $this->classNamespace,
79
            'modelBaseName' => $this->modelBaseName,
80
            'modelFullName' => $this->modelFullName,
81
            'modelPlural' => $this->modelPlural,
82
            'modelTitle' => $this->titleSingular,
83
            'modelVariableName' => $this->modelVariableName,
84
            'modelVariableNamePlural' => Str::plural($this->modelVariableName),
85
            'modelRouteAndViewName' => $this->modelRouteAndViewName,
86
            'modelViewsDirectory' => $this->modelViewsDirectory,
87
            'modelDotNotation' => $this->modelDotNotation,
88
            'modelWithNamespaceFromDefault' => $this->modelWithNamespaceFromDefault,
89
            'export' => $this->export,
90
            'withoutBulk' => $this->withoutBulk,
91
            'exportBaseName' => $this->exportBaseName,
92
            'resource' => $this->resource,
93
            // index
94
            'columnsToQuery' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
95
                return !($column['type'] == 'text' || $column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "deleted_at"||Str::contains($column['name'],"_id"));
96
            })->pluck('name')->toArray(),
97
            'columnsToSearchIn' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
98
                return ($column['type'] == 'json' || $column['type'] == 'text' || $column['type'] == 'string' || $column['name'] == "id") && !($column['name'] == "password" || $column['name'] == "remember_token");
99
            })->pluck('name')->toArray(),
100
            //            'filters' => $this->readColumnsFromTable($tableName)->filter(function($column) {
101
            //                return $column['type'] == 'boolean' || $column['type'] == 'date';
102
            //            }),
103
            // validation in store/update
104
            'columns' => $this->getVisibleColumns($this->tableName, $this->modelVariableName),
105
            'relations' => $this->relations,
106
            'hasSoftDelete' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
107
                    return $column['name'] == "deleted_at";
108
            })->count() > 0,
109
        ])->render();
110
    }
111
112
    protected function getOptions() {
113
        return [
114
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'],
115
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
116
            ['belongs-to-many', 'btm', InputOption::VALUE_OPTIONAL, 'Specify belongs to many relations'],
117
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating controller'],
118
            ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'],
119
            ['with-export', 'e', InputOption::VALUE_NONE, 'Generate an option to Export as Excel'],
120
            ['without-bulk', 'wb', InputOption::VALUE_NONE, 'Generate without bulk options'],
121
        ];
122
    }
123
124
    public function generateClassNameFromTable($tableName) {
125
        return Str::studly(Str::plural($tableName));
126
    }
127
128
    /**
129
     * Get the default namespace for the class.
130
     *
131
     * @param string $rootNamespace
132
     * @return string
133
     */
134
    protected function getDefaultNamespace(string $rootNamespace): string
135
    {
136
        return $rootNamespace.'\Repositories';
137
    }
138
}
139