Completed
Pull Request — master (#666)
by reallyli
02:28
created

CommandMakeCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class CommandMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The name of argument name.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'name';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'module:make-command';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Generate new Artisan command for the specified module.';
36
37
    public function getDefaultNamespace() : string
38
    {
39
        return $this->laravel['modules']->config('paths.generator.command.path', 'Console');
40
    }
41
42
    /**
43
     * Get the console command arguments.
44
     *
45
     * @return array
46
     */
47
    protected function getArguments()
48
    {
49
        return [
50
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
51
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
52
        ];
53
    }
54
55
    /**
56
     * Get the console command options.
57
     *
58
     * @return array
59
     */
60
    protected function getOptions()
61
    {
62
        return [
63
            ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null],
64
        ];
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70
    protected function getTemplateContents()
71
    {
72
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
73
74
        return (new Stub('/command.stub', [
75
            'COMMAND_NAME' => $this->getCommandName(),
76
            'NAMESPACE'    => $this->getClassNamespace($module),
77
            'CLASS'        => $this->getClass(),
78
        ]))->render();
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    private function getCommandName()
85
    {
86
        return $this->option('command') ?: 'command:name';
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92
    protected function getDestinationFilePath()
93
    {
94
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
95
96
        $commandPath = GenerateConfigReader::read('command');
97
98
        return $path . $commandPath->getPath() . '/' . $this->getFileName() . '.php';
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    private function getFileName()
105
    {
106
        return Str::studly($this->argument('name'));
107
    }
108
}
109