Completed
Push — master ( 88bae4...1fc56c )
by Nicolas
06:11
created

GenerateListenerCommand::getDestinationFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Module;
6
use Nwidart\Modules\Traits\ModuleCommandTrait;
7
use Pingpong\Support\Stub;
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 25
    protected function getArguments()
37
    {
38
        return [
39 25
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
40 25
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
41 25
        ];
42
    }
43
44
    /**
45
     * Get the console command options.
46
     *
47
     * @return array
48
     */
49 25
    protected function getOptions()
50
    {
51
        return [
52 25
            ['event', null, InputOption::VALUE_REQUIRED, 'Event name this is listening to', null],
53 25
        ];
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 fire()
87 2
    {
88 2
        if (!$this->option('event')) {
89
            return $this->error('The --event option is necessary');
90
        }
91
92 2
        parent::fire();
93 2
    }
94
95 2
    protected function getEventName(Module $module)
96
    {
97 2
        return $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.event') . "\\" . $this->option('event');
98
    }
99
100 2
    private function getNamespace($module)
101
    {
102 2
        $namespace = str_replace('/', '\\', config('modules.paths.generator.listener'));
103
104 2
        return $this->getClassNamespace($module) . "\\" . $namespace;
105
    }
106
}
107