Passed
Push — analysis-XNGW1g ( 55039c )
by Kyle
07:59 queued 32s
created

CrudGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands\Generator;
4
5
use Illuminate\Support\Str;
6
7
class CrudGenerator extends BaseGenerator
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'make:crud {name}';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Create a full-set of Admin CRUD';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return mixed
27
     */
28
    public function handle()
29
    {
30
        $targetName = $this->getTargetName();
31
32
        $this->info("Create model: $targetName");
33
        $this->generateModel($targetName);
34
35
        $this->call('make:repository', [
36
            'name' => $targetName,
37
        ]);
38
39
        $this->call('make:services', [
40
            'name' => $targetName,
41
        ]);
42
43
        $this->info("Create $targetName".'Controller');
44
        $this->generateController($targetName);
45
46
        $this->info("Create views for $targetName");
47
        $this->generateViews($targetName);
48
    }
49
50
    /**
51
     * Generate Controller file.
52
     *
53
     * @param $name
54
     */
55
    protected function generateController($name)
56
    {
57
        //Generate Requests
58
        $this->callSilent('make:request', [
59
            'name' => 'Create'.ucwords($name).'Request',
60
        ]);
61
        $this->callSilent('make:request', [
62
            'name' => 'Update'.ucwords($name).'Request',
63
        ]);
64
65
        $this->proceedAndSaveFile($name, 'controller',
66
            $this->getControllerPath().ucwords($name).'Controller.php');
67
    }
68
69
    /**
70
     * Generate Model file.
71
     *
72
     * @param $name
73
     */
74
    protected function generateModel($name)
75
    {
76
        $this->callSilent('make:model', [
77
            'name'        => 'Models/'.ucwords($name),
78
            '--migration' => true,
79
            '--factory'   => true,
80
        ]);
81
    }
82
83
    protected function generateViews($name)
84
    {
85
        $viewPath = $this->getViewPath().strtolower(Str::plural($name));
86
        //Create view folder
87
        if (!$this->filesystem->isDirectory($viewPath)) {
88
            $this->filesystem->makeDirectory($viewPath);
89
        }
90
91
        $this->proceedAndSaveFile($name, 'view_index', $viewPath.'/index.blade.php');
92
        $this->proceedAndSaveFile($name, 'view_create', $viewPath.'/create.blade.php');
93
        $this->proceedAndSaveFile($name, 'view_show', $viewPath.'/show.blade.php');
94
        $this->proceedAndSaveFile($name, 'view_edit', $viewPath.'/edit.blade.php');
95
    }
96
}
97