Completed
Pull Request — master (#1085)
by
unknown
54:16 queued 45:25
created

CommandMakeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 6 2
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
    public function getDefaultNamespace() : string
38
    {
39
        $module = $this->laravel['modules'];
40
41
        return $module->config('paths.generator.command.namespace') ?: $module->config('paths.generator.command.path', 'Console');
42
    }
43
44
    /**
45
     * Get the console command arguments.
46
     *
47
     * @return array
48
     */
49
    protected function getArguments()
50
    {
51
        return [
52
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
53
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
54
        ];
55
    }
56
57
    /**
58
     * Get the console command options.
59
     *
60
     * @return array
61
     */
62
    protected function getOptions()
63
    {
64
        return [
65
            ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned.', null],
66
        ];
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72
    protected function getTemplateContents()
73
    {
74
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
75
76
        return (new Stub('/command.stub', [
77
            'COMMAND_NAME' => $this->getCommandName(),
78
            'NAMESPACE'    => $this->getClassNamespace($module),
79
            'CLASS'        => $this->getClass(),
80
        ]))->render();
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    private function getCommandName()
87
    {
88
        return $this->option('command') ?: 'command:name';
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    protected function getDestinationFilePath()
95
    {
96
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
97
98
        $commandPath = GenerateConfigReader::read('command');
99
100
        return $path . $commandPath->getPath() . '/' . $this->getFileName() . '.php';
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    private function getFileName()
107
    {
108
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; 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...
109
    }
110
}
111