Completed
Push — master ( 4fafa1...e65f27 )
by Nicolas
12:12
created

src/Commands/ListenerMakeCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Module;
7
use Nwidart\Modules\Support\Config\GenerateConfigReader;
8
use Nwidart\Modules\Support\Stub;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class ListenerMakeCommand extends GeneratorCommand
14
{
15
    use ModuleCommandTrait;
16
17
    protected $argumentName = 'name';
18
19
    /**
20
     * The console command name.
21
     *
22
     * @var string
23
     */
24
    protected $name = 'module:make-listener';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Create a new event listener class for the specified module';
32
33
    /**
34
     * Get the console command arguments.
35
     *
36
     * @return array
37
     */
38 120
    protected function getArguments()
39
    {
40
        return [
41 120
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
42
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
43
        ];
44
    }
45
46
    /**
47
     * Get the console command options.
48
     *
49
     * @return array
50
     */
51 120
    protected function getOptions()
52
    {
53
        return [
54 120
            ['event', 'e', InputOption::VALUE_OPTIONAL, 'The event class being listened for.'],
55
            ['queued', null, InputOption::VALUE_NONE, 'Indicates the event listener should be queued.'],
56
        ];
57
    }
58
59 7
    protected function getTemplateContents()
60
    {
61 7
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
62
63 7
        return (new Stub($this->getStubName(), [
64 7
            'NAMESPACE' => $this->getClassNamespace($module),
65 7
            'EVENTNAME' => $this->getEventName($module),
66 7
            'SHORTEVENTNAME' => $this->option('event'),
67 7
            'CLASS' => $this->getClass(),
68 7
        ]))->render();
69
    }
70
71 7
    public function getDefaultNamespace() : string
72
    {
73 7
        $module = $this->laravel['modules'];
74
75 7
        return $module->config('paths.generator.listener.namespace') ?: $module->config('paths.generator.listener.path', 'Listeners');
76
    }
77
78 7
    protected function getEventName(Module $module)
79
    {
80 7
        $eventPath = GenerateConfigReader::read('event');
81
82 7
        return $this->getClassNamespace($module) . "\\" . $eventPath->getPath() . "\\" . $this->option('event');
83
    }
84
85 7
    protected function getDestinationFilePath()
86
    {
87 7
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
88
89 7
        $listenerPath = GenerateConfigReader::read('listener');
90
91 7
        return $path . $listenerPath->getPath() . '/' . $this->getFileName() . '.php';
92
    }
93
94
    /**
95
     * @return string
96
     */
97 7
    protected function getFileName()
98
    {
99 7
        return Str::studly($this->argument('name'));
0 ignored issues
show
It seems like $this->argument('name') targeting Illuminate\Console\Command::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...
100
    }
101
102
    /**
103
     * @return string
104
     */
105 7
    protected function getStubName(): string
106
    {
107 7
        if ($this->option('queued')) {
108 2
            if ($this->option('event')) {
109 1
                return '/listener-queued.stub';
110
            }
111
112 1
            return '/listener-queued-duck.stub';
113
        }
114
115 5
        if ($this->option('event')) {
116 2
            return '/listener.stub';
117
        }
118
119 3
        return '/listener-duck.stub';
120
    }
121
}
122