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

Export   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 12.82 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 10
loc 78
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 Illuminate\Support\Str;
4
use Symfony\Component\Console\Input\InputOption;
5
6
class Export extends ClassGenerator {
7
8
    /**
9
     * The name and signature of the console command.
10
     *
11
     * @var string
12
     */
13
    protected $name = 'jig:generate:export';
14
15
    /**
16
     * The console command description.
17
     *
18
     * @var string
19
     */
20
    protected $description = 'Generate an export class';
21
22
    /**
23
     * Path for view
24
     *
25
     * @var string
26
     */
27
    protected string $view = 'export';
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...
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        $force = $this->option('force');
37
38
        if(!empty($template = $this->option('template'))) {
39
            $this->view = 'templates.'.$template.'.export';
40
        }
41
42
        if ($this->generateClass($force)){
43
            $this->info('Generating '.$this->classFullName.' finished');
44
        }
45
46
    }
47
48
    protected function buildClass(): string {
49
        return view('jig::'.$this->view, [
50
            'exportNamespace' => $this->classNamespace,
51
            'modelFullName' => $this->modelFullName,
52
            'classBaseName' => $this->exportBaseName,
53
            'modelBaseName' => $this->modelBaseName,
54
            'modelVariableName' => $this->modelVariableName,
55
            'modelLangFormat' => $this->modelLangFormat,
56
            'columnsToExport' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
57
                return !($column['name'] == "password" || $column['name'] == "remember_token" || $column['name'] == "updated_at" || $column['name'] == "created_at"  || $column['name'] == "deleted_at");
58
            })->pluck('name')->toArray(),
59
        ])->render();
60
    }
61
62
    protected function getOptions() {
63
        return [
64
            ['force', 'f', InputOption::VALUE_NONE, 'Force will delete files before regenerating request'],
65
            ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'],
66
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
67
        ];
68
    }
69
70
    public function generateClassNameFromTable($tableName) {
71
        return $this->exportBaseName;
72
    }
73
    /**
74
     * Get the default namespace for the class.
75
     *
76
     * @param string $rootNamespace
77
     * @return string
78
     */
79
    protected function getDefaultNamespace(string $rootNamespace) : string
80
    {
81
        return $rootNamespace.'\Exports';
82
    }
83
}
84