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

Policy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 117
Duplicated Lines 9.4 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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