Completed
Push — master ( 0fd405...f8ea68 )
by Nicolas
8s
created

CommandMakeCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 97
ccs 22
cts 22
cp 1
rs 10
wmc 8
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 4 1
A getArguments() 0 7 1
A getOptions() 0 6 1
A getTemplateContents() 0 10 1
A getCommandName() 0 4 2
A getDestinationFilePath() 0 8 1
A getFileName() 0 4 1
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 4
    public function getDefaultNamespace() : string
38
    {
39 4
        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 91
    protected function getArguments()
48
    {
49
        return [
50 91
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
51 91
            ['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 91
    protected function getOptions()
61
    {
62
        return [
63 91
            ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null],
64
        ];
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70 4
    protected function getTemplateContents()
71
    {
72 4
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
73
74 4
        return (new Stub('/command.stub', [
75 4
            'COMMAND_NAME' => $this->getCommandName(),
76 4
            'NAMESPACE'    => $this->getClassNamespace($module),
77 4
            'CLASS'        => $this->getClass(),
78 4
        ]))->render();
79
    }
80
81
    /**
82
     * @return string
83
     */
84 4
    private function getCommandName()
85
    {
86 4
        return $this->option('command') ?: 'command:name';
87
    }
88
89
    /**
90
     * @return mixed
91
     */
92 4
    protected function getDestinationFilePath()
93
    {
94 4
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
95
96 4
        $commandPath = GenerateConfigReader::read('command');
97
98 4
        return $path . $commandPath->getPath() . '/' . $this->getFileName() . '.php';
99
    }
100
101
    /**
102
     * @return string
103
     */
104 4
    private function getFileName()
105
    {
106 4
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
107
    }
108
}
109