Completed
Pull Request — master (#354)
by Martin
02:25
created

CommandMakeCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

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\Stub;
7
use Nwidart\Modules\Traits\ModuleCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class CommandMakeCommand extends GeneratorCommand
12
{
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
38
    /**
39
     * Get default namespace.
40
     *
41
     * @return string
42
     */
43 3
    public function getDefaultNamespace()
44
    {
45 3
        return 'Console';
46
    }
47
48
49
    /**
50
     * Get the console command arguments.
51
     *
52
     * @return array
53
     */
54 60
    protected function getArguments()
55
    {
56
        return [
57 60
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
58 60
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
59
        ];
60
    }
61
62
63
    /**
64
     * Get the console command options.
65
     *
66
     * @return array
67
     */
68 60
    protected function getOptions()
69
    {
70
        return [
71 60
            ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null],
72
        ];
73
    }
74
75
76
    /**
77
     * @return mixed
78
     */
79 3
    protected function getTemplateContents()
80
    {
81 3
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
82
83 3
        return (new Stub('/command.stub', [
84 3
            'COMMAND_NAME' => $this->getCommandName(),
85 3
            'NAMESPACE'    => $this->getClassNamespace($module),
86 3
            'CLASS'        => $this->getClass(),
87 3
        ]))->render();
88
    }
89
90
91
    /**
92
     * @return string
93
     */
94 3
    private function getCommandName()
95
    {
96 3
        return $this->option('command') ?: 'command:name';
97
    }
98
99
100
    /**
101
     * @return mixed
102
     */
103 3
    protected function getDestinationFilePath()
104
    {
105 3
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
106
107 3
        $seederPath = $this->laravel['modules']->config('paths.generator.command');
108
109 3
        return $path.$seederPath.'/'.$this->getFileName().'.php';
110
    }
111
112
113
    /**
114
     * @return string
115
     */
116 3
    private function getFileName()
117
    {
118 3
        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...
119
    }
120
}
121