Completed
Push — master ( eca1e1...16b172 )
by Nicolas
13:25
created

GenerateListenerCommand::fire()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
crap 2.0932
rs 9.4285
c 0
b 0
f 0
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 GenerateListenerCommand extends GeneratorCommand
12
{
13
    use ModuleCommandTrait;
14
15
    protected $argumentName = 'name';
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'module:make-listener';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Generate a new Listener Class for the specified module';
30
31
    /**
32
     * Get the console command arguments.
33
     *
34
     * @return array
35
     */
36 56
    protected function getArguments()
37
    {
38
        return [
39 56
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
40
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
41
        ];
42
    }
43
44
    /**
45
     * Get the console command options.
46
     *
47
     * @return array
48
     */
49 56
    protected function getOptions()
50
    {
51
        return [
52 56
            ['event', null, InputOption::VALUE_REQUIRED, 'Event name this is listening to', null],
53
        ];
54
    }
55
56 2
    protected function getTemplateContents()
57
    {
58 2
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
59
60 2
        return (new Stub('/listener.stub', [
61 2
            'NAMESPACE' => $this->getNamespace($module),
62 2
            "EVENTNAME" => $this->getEventName($module),
63 2
            "EVENTSHORTENEDNAME" => $this->option('event'),
64 2
            "CLASS" => $this->getClass(),
65 2
            'DUMMYNAMESPACE' => $this->laravel->getNamespace() . "Events",
66 2
        ]))->render();
67
    }
68
69 2
    protected function getDestinationFilePath()
70
    {
71 2
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
72
73 2
        $seederPath = $this->laravel['modules']->config('paths.generator.listener');
74
75 2
        return $path . $seederPath . '/' . $this->getFileName() . '.php';
76
    }
77
78
    /**
79
     * @return string
80
     */
81 2
    protected function getFileName()
82
    {
83 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...
84
    }
85
86 2
    public function handle()
87
    {
88 2
        if (!$this->option('event')) {
89
            $this->error('The --event option is necessary');
90
91
            return;
92
        }
93
94 2
        parent::handle();
95 2
    }
96
97 2
    protected function getEventName(Module $module)
98
    {
99 2
        return $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.event') . "\\" . $this->option('event');
100
    }
101
102 2
    private function getNamespace($module)
103
    {
104 2
        $namespace = str_replace('/', '\\', config('modules.paths.generator.listener'));
105
106 2
        return $this->getClassNamespace($module) . "\\" . $namespace;
107
    }
108
}
109