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

ModelFactory::buildClass()   D

Complexity

Conditions 20
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 4.1666
c 0
b 0
f 0
cc 20
nc 1
nop 0

How to fix   Long Method    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 Illuminate\Support\Str;
4
use Symfony\Component\Console\Input\InputOption;
5
6
class ModelFactory extends FileAppender {
7
8
    /**
9
     * The name and signature of the console command.
10
     *
11
     * @var string
12
     */
13
    protected $name = 'jig:generate:factory';
14
15
    /**
16
     * The console command description.
17
     *
18
     * @var string
19
     */
20
    protected $description = 'Append a new factory';
21
22
    /**
23
     * Path for view
24
     *
25
     * @var string
26
     */
27
    protected string $view = 'factory';
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
        //TODO check if exists
37
        //TODO make global for all generator
38
        //TODO also with prefix
39
        if(!empty($template = $this->option('template'))) {
40
            $this->view = 'templates.'.$template.'.factory';
41
        }
42
43
        if ($this->appendIfNotAlreadyAppended(base_path('database/factories/ModelFactory.php'), $this->buildClass())){
44
            $this->info('Appending '.$this->modelBaseName.' model to ModelFactory finished');
45
        }
46
47
        if ($this->option('seed')) {
48
            $this->info('Seeding testing data');
49
            factory($this->modelFullName , 50)->create();
50
        }
51
    }
52
53
    protected function buildClass(): string
54
    {
55
56
        return view('jig::'.$this->view, [
57
            'modelFullName' => $this->modelFullName,
58
59
            'columns' => $this->readColumnsFromTable($this->tableName)
60
                // we skip primary key
61
                ->filter(function($col){
62
                    return $col['name'] != 'id';
63
                })
64
                ->map(function($col) {
65
                if($col['name'] == 'deleted_at') {
66
                    $type = 'null';
67
                } else if($col['name'] == 'remember_token') {
68
                    $type = 'null';
69
                } else {
70
                    if ($col['type'] == 'date') {
71
                        $type = '$faker->date()';
72
                    } elseif ($col['type'] == 'time') {
73
                        $type = '$faker->time()';
74
                    } elseif ($col['type'] == 'datetime') {
75
                        $type = '$faker->dateTime';
76
                    } elseif ($col['type'] == 'text') {
77
                        $type = '$faker->text()';
78
                    } elseif ($col['type'] == 'boolean') {
79
                        $type = '$faker->boolean()';
80
                    } elseif ($col['type'] == 'integer' || $col['type'] == 'numeric' || $col['type'] == 'decimal') {
81
                        $type = '$faker->randomNumber(5)';
82
                    } elseif ($col['type'] == 'float') {
83
                        $type = '$faker->randomFloat';
84
                    } elseif ($col['name'] == 'title') {
85
                        $type = '$faker->sentence';
86
                    } elseif ($col['name'] == 'email') {
87
                        $type = '$faker->email';
88
                    } elseif ($col['name'] == 'name' || $col['name'] == 'first_name') {
89
                        $type = '$faker->firstName';
90
                    } elseif ($col['name'] == 'surname' || $col['name'] == 'last_name') {
91
                        $type = '$faker->lastName';
92
                    } elseif ($col['name'] == 'slug') {
93
                        $type = '$faker->unique()->slug';
94
                    } elseif ($col['name'] == 'password') {
95
                        $type = 'bcrypt($faker->password)';
96
                    } else {
97
                        $type = '$faker->sentence';
98
                    }
99
                }
100
                return [
101
                    'name' => $col['name'],
102
                    'faker' => $type,
103
                ];
104
            }),
105
            'translatable' => $this->readColumnsFromTable($this->tableName)->filter(function($column) {
106
                return $column['type'] == "json";
107
            })->pluck('name'),
108
        ])->render();
109
    }
110
111
    protected function getOptions() {
112
        return [
113
            ['model-name', 'm', InputOption::VALUE_OPTIONAL, 'Generates a code for the given model'],
114
            ['template', 't', InputOption::VALUE_OPTIONAL, 'Specify custom template'],
115
            ['seed', 's', InputOption::VALUE_OPTIONAL, 'Seeds the table with fake data'],
116
            ['model-with-full-namespace', 'fnm', InputOption::VALUE_OPTIONAL, 'Specify model with full namespace'],
117
        ];
118
    }
119
120
}
121