Completed
Push — development ( f127c5...68c904 )
by Kyle
04:11
created

CrudGenerator::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 23
ccs 0
cts 14
cp 0
crap 2
rs 9.8333
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
        $this->info("Create unit tests for $targetName");
50
        $this->generateTest($targetName);
51
    }
52
53
    /**
54
     * Generate Controller file.
55
     *
56
     * @param $name
57
     */
58
    protected function generateController($name)
59
    {
60
        //Generate Requests
61
        $this->callSilent('make:request', [
62
            'name' => 'Create'.ucwords($name).'Request',
63
        ]);
64
        $this->callSilent('make:request', [
65
            'name' => 'Update'.ucwords($name).'Request',
66
        ]);
67
68
        $this->proceedAndSaveFile($name, 'controller',
69
            $this->getControllerPath().ucwords($name).'Controller.php');
70
    }
71
72
    /**
73
     * Generate Model file.
74
     *
75
     * @param $name
76
     */
77
    protected function generateModel($name)
78
    {
79
        $this->callSilent('make:model', [
80
            'name'        => 'Models/'.ucwords($name),
81
            '--migration' => true,
82
            '--factory'   => true,
83
        ]);
84
    }
85
86
    /**
87
     * Generate Views
88
     *
89
     * @param $name
90
     */
91
    protected function generateViews($name)
92
    {
93
        $viewPath = $this->getViewPath().strtolower(Str::plural($name));
94
        //Create view folder
95
        if (!$this->filesystem->isDirectory($viewPath)) {
96
            $this->filesystem->makeDirectory($viewPath);
97
        }
98
99
        $this->proceedAndSaveFile($name, 'view_index', $viewPath.'/index.blade.php');
100
        $this->proceedAndSaveFile($name, 'view_create', $viewPath.'/create.blade.php');
101
        $this->proceedAndSaveFile($name, 'view_show', $viewPath.'/show.blade.php');
102
        $this->proceedAndSaveFile($name, 'view_edit', $viewPath.'/edit.blade.php');
103
    }
104
105
    protected function generateTest($name)
106
    {
107
        $testPath = $this->getTestsPath() . 'Feature/' . ucwords($name) . 'Test.php';
108
        $this->proceedAndSaveFile($name, 'unittest', $testPath);
109
    }
110
}
111