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

ListenerMakeCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 105
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 2
A getArguments() 0 7 1
A getOptions() 0 6 1
A getTemplateContents() 0 10 1
A getNamespace() 0 6 1
A getEventName() 0 4 1
A getDestinationFilePath() 0 8 1
A getFileName() 0 4 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Module;
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 ListenerMakeCommand extends GeneratorCommand
12
{
13
14
    use ModuleCommandTrait;
15
16
    protected $argumentName = 'name';
17
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'module:make-listener';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new event listener class for the specified module';
31
32
33 2
    public function handle()
34
    {
35 2
        if ( ! $this->option('event')) {
36
            $this->error('The --event option is necessary');
37
38
            return;
39
        }
40
41 2
        parent::handle();
42 2
    }
43
44
45
    /**
46
     * Get the console command arguments.
47
     *
48
     * @return array
49
     */
50 60
    protected function getArguments()
51
    {
52
        return [
53 60
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
54
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
55
        ];
56
    }
57
58
59
    /**
60
     * Get the console command options.
61
     *
62
     * @return array
63
     */
64 60
    protected function getOptions()
65
    {
66
        return [
67 60
            ['event', null, InputOption::VALUE_REQUIRED, 'Event name this is listening to', null],
68
        ];
69
    }
70
71
72 2
    protected function getTemplateContents()
73
    {
74 2
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
75
76 2
        return (new Stub('/listener.stub', [
77 2
            'NAMESPACE' => $this->getNamespace($module),
78 2
            "EVENTNAME" => $this->getEventName($module),
79 2
            "CLASS"     => $this->getClass(),
80 2
        ]))->render();
81
    }
82
83
84 2
    private function getNamespace($module)
85
    {
86 2
        $namespace = str_replace('/', '\\', config('modules.paths.generator.listener'));
87
88 2
        return $this->getClassNamespace($module)."\\".$namespace;
89
    }
90
91
92 2
    protected function getEventName(Module $module)
93
    {
94 2
        return $this->getClassNamespace($module)."\\".config('modules.paths.generator.event')."\\".$this->option('event');
95
    }
96
97
98 2
    protected function getDestinationFilePath()
99
    {
100 2
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
101
102 2
        $seederPath = $this->laravel['modules']->config('paths.generator.listener');
103
104 2
        return $path.$seederPath.'/'.$this->getFileName().'.php';
105
    }
106
107
108
    /**
109
     * @return string
110
     */
111 2
    protected function getFileName()
112
    {
113 2
        return studly_case($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, studly_case() 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...
114
    }
115
}
116