Passed
Push — master ( 139939...b4b8e8 )
by Arthur
21:54 queued 17s
created

AbstractGeneratorCommand::getFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
12
use Foundation\Generator\Support\Stub;
13
use Illuminate\Support\Str;
14
use Nwidart\Modules\Commands\GeneratorCommand;
15
use Symfony\Component\Console\Input\InputArgument;
16
17
abstract class AbstractGeneratorCommand extends GeneratorCommand
18
{
19
20
    /**
21
     * The name of the generated resource.
22
     *
23
     * @var string
24
     */
25
    protected $generatorName;
26
27
    /**
28
     * The stub name.
29
     *
30
     * @var string
31
     */
32
    protected $stub;
33
34
    /**
35
     * The file path.
36
     *
37
     * @var string
38
     */
39
    protected $filePath;
40
41
    /**
42
     * @return mixed
43
     */
44
    protected function getDestinationFilePath()
45
    {
46
        return $this->getModulePath() . $this->filePath . '/' . $this->getFileName();
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    protected function getFileName()
53
    {
54
        return $this->getClassName() . '.php';
55
    }
56
57
    protected function getModulePath(): string
58
    {
59
        return get_module_path($this->getModuleName());
60
    }
61
62
    protected function getModuleName(): string
63
    {
64
        return once(function () {
65
            return Str::studly($this->askModuleName());
66
        });
67
    }
68
69
    private function askModuleName(): string
70
    {
71
        $moduleName = $this->argument('module') ?? $this->ask('For what module would you like to generate a ' . $this->getGeneratorName() . '.');
72
73
        if ($moduleName === null)
74
            throw new \Exception("Name of module not set.");
75
76
        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...
77
    }
78
79
    /**
80
     * Get class namespace.
81
     *
82
     *
83
     * @return string
84
     */
85
    public function getClassNamespace($module = null): string
86
    {
87
        return $this->getModuleNamespace() . str_replace('/', '\\', $this->filePath);
88
    }
89
90
91
    protected function getModuleNamespace(): string
92
    {
93
        return 'Modules' . '\\' . $this->getModuleName();
94
    }
95
96
    /**
97
     * @return mixed
98
     */
99
    protected function getTemplateContents()
100
    {
101
        $this->beforeGeneration();
102
        $stub = (new Stub($this->stubName(), $this->stubOptions()))->render();
103
        $this->afterGeneration();
104
        return $stub;
105
    }
106
107
    protected function beforeGeneration(): void
108
    {
109
    }
110
111
    protected function afterGeneration(): void
112
    {
113
    }
114
115
    protected abstract function stubOptions(): array;
116
117
118
    protected function getClassName(): string
119
    {
120
        return once(function () {
121
            return Str::studly($this->askClassName());
122
        });
123
    }
124
125
    private function askClassName(): string
126
    {
127
        $className = $this->argument('name') ?? $this->ask('Specify the name of the ' . $this->getGeneratorName() . '.');;
128
        if ($className === null)
129
            throw new \Exception("Name of " . $this->getGeneratorName() . " not set.");
130
131
        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...
132
    }
133
134
    /**
135
     * Get the console command arguments.
136
     *
137
     * @return array
138
     */
139
    protected function getArguments()
140
    {
141
        return [
142
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
143
            ['name', InputArgument::OPTIONAL, 'The name of the ' . $this->getGeneratorName() . '.'],
144
        ];
145
    }
146
147
    /**
148
     * Get the console command options.
149
     *
150
     * @return array
151
     */
152
    protected function getOptions()
153
    {
154
        return [
155
156
        ];
157
    }
158
159
    protected function getGeneratorName(): string
160
    {
161
        return $this->generatorName ?? 'class';
162
    }
163
164
    /**
165
     * Get the stub file name
166
     * @return string
167
     */
168
    protected function stubName()
169
    {
170
        return $this->stub;
171
    }
172
}
173