Passed
Push — master ( b3b4c7...2fd2da )
by Arthur
24:27
created

AbstractGeneratorCommand::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 09.03.19
6
 * Time: 17:15.
7
 */
8
9
namespace Foundation\Generator\Abstracts;
10
11
use Foundation\Core\Larapi;
12
use Foundation\Core\Module;
13
use Foundation\Generator\Events\FileGeneratedEvent;
14
use Foundation\Generator\Support\Stub;
15
use Illuminate\Support\Str;
16
use Nwidart\Modules\Commands\GeneratorCommand;
17
use Symfony\Component\Console\Input\InputArgument;
18
19
abstract class AbstractGeneratorCommand extends GeneratorCommand
20
{
21
    /**
22
     * The name of the generated resource.
23
     *
24
     * @var string
25
     */
26
    protected $generatorName;
27
28
    /**
29
     * The stub name.
30
     *
31
     * @var string
32
     */
33
    protected $stub;
34
35
    /**
36
     * The file path.
37
     *
38
     * @var string
39
     */
40
    protected $filePath;
41
42
    /**
43
     * @return mixed
44
     */
45
    protected function getDestinationFilePath()
46
    {
47
        return $this->getModule()->getPath() . $this->filePath . '/' . $this->getFileName();
48
    }
49
50
    protected function getTemplateContents(){
51
52
    }
53
54
    public function handle()
55
    {
56
        $this->beforeGeneration();
57
        $path = str_replace('\\', '/', $this->getDestinationFilePath());
58
59
        if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
60
            $this->laravel['files']->makeDirectory($dir, 0777, true);
61
        }
62
63
        if(file_exists($path)){
64
            $this->error("File : {$path} already exists.");
65
        }
66
67
        $this->info("Created : {$path}");
68
69
        event(new FileGeneratedEvent($this->getDestinationFilePath(), $this->stubName(), $this->stubOptions()));
70
71
        $this->afterGeneration();
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    protected function getFileName()
78
    {
79
        return $this->getClassName() . '.php';
80
    }
81
82
    protected function getModule(): Module
83
    {
84
        return once(function () {
85
            return Larapi::getModule($this->getModuleName());
86
        });
87
    }
88
89
    protected function getModuleName(): string
90
    {
91
        return once(function () {
92
            return Str::studly($this->askModuleName());
93
        });
94
    }
95
96
    private function askModuleName(): string
97
    {
98
        $moduleName = $this->argument('module') ?? $this->ask('For what module would you like to generate a ' . $this->getGeneratorName() . '.');
99
100
        if ($moduleName === null) {
101
            throw new \Exception('Name of module not set.');
102
        }
103
104
        return $moduleName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $moduleName could return the type string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
105
    }
106
107
    /**
108
     * Get class namespace.
109
     *
110
     *
111
     * @return string
112
     */
113
    public function getClassNamespace($module = null): string
114
    {
115
        return $this->getModule()->getNamespace() . str_replace('/', '\\', $this->filePath);
116
    }
117
118
119
120
    protected function beforeGeneration(): void
121
    {
122
    }
123
124
    protected function afterGeneration(): void
125
    {
126
    }
127
128
    abstract protected function stubOptions(): array;
129
130
    protected function getClassName(): string
131
    {
132
        return once(function () {
133
            return Str::studly($this->askClassName());
134
        });
135
    }
136
137
    private function askClassName(): string
138
    {
139
        $className = $this->argument('name') ?? $this->ask('Specify the name of the ' . $this->getGeneratorName() . '.');
140
141
        if ($className === null) {
142
            throw new \Exception('Name of ' . $this->getGeneratorName() . ' not set.');
143
        }
144
145
        return $className;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $className could return the type string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
146
    }
147
148
    /**
149
     * Get the console command arguments.
150
     *
151
     * @return array
152
     */
153
    protected function getArguments()
154
    {
155
        return [
156
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
157
            ['name', InputArgument::OPTIONAL, 'The name of the ' . $this->getGeneratorName() . '.'],
158
        ];
159
    }
160
161
    /**
162
     * Get the console command options.
163
     *
164
     * @return array
165
     */
166
    protected function getOptions()
167
    {
168
        return [];
169
    }
170
171
    protected function getGeneratorName(): string
172
    {
173
        return $this->generatorName ?? 'class';
174
    }
175
176
    /**
177
     * Get the stub file name.
178
     * @return string
179
     */
180
    protected function stubName()
181
    {
182
        return $this->stub;
183
    }
184
}
185