Completed
Pull Request — master (#2)
by Lanre
05:41
created

GenerateListenerCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 91
ccs 30
cts 31
cp 0.9677
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 7 1
A getOptions() 0 6 1
A getTemplateContents() 0 13 1
A getDestinationFilePath() 0 9 1
A getFileName() 0 4 1
A fire() 0 8 2
A getEventName() 0 4 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Nwidart\Modules\Commands\GeneratorCommand;
6
use Nwidart\Modules\Module;
7
use Nwidart\Modules\Traits\ModuleCommandTrait;
8
use Pingpong\Support\Stub;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\InputArgument;
11
12
class GenerateListenerCommand extends GeneratorCommand
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 = 'Generate a new Listener Class for the specified module';
31
32
    /**
33
     * Get the console command arguments.
34
     *
35
     * @return array
36
     */
37 25
    protected function getArguments()
38
    {
39 2
        return [
40 25
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
41 25
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
42 25
        ];
43
    }
44
45
    /**
46
     * Get the console command options.
47
     *
48
     * @return array
49
     */
50 25
    protected function getOptions()
51
    {
52
        return [
53 25
            ['event', null, InputOption::VALUE_REQUIRED, 'An example option.', null],
54 25
        ];
55
    }
56
57 2
    protected function getTemplateContents()
58
    {
59 2
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
60
61 2
        return (new Stub('/listener.stub', [
62 2
            'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config("modules.paths.generator.listener"),
63 2
            "EVENTNAME" => $this->getEventName($module),
64 2
            "EVENTSHORTENEDNAME" => $this->option('event'),
65 2
            "CLASS" => $this->getClass(),
66 2
            'DUMMYNAMESPACE' => $this->laravel->getNamespace() . "Events"
67 2
        ]))->render();
68
69
    }
70
71 2
    protected function getDestinationFilePath()
72
    {
73 2
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
74
75 2
        $seederPath = $this->laravel['modules']->config('paths.generator.listener');
76
77 2
        return $path . $seederPath . '/' . $this->getFileName() . '.php';
78
79
    }
80
81
    /**
82
     * @return string
83
     */
84 2
    protected function getFileName()
85
    {
86 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...
87 2
    }
88
89 2
    public function fire()
90
    {
91 2
        if (!$this->option('event')) {
92
            return $this->error("The --event option is neccessary");
93
        }
94
95 2
        parent::fire();
96 2
    }
97
98 2
    protected function getEventName(Module $module)
99
    {
100 2
        return $this->getClassNamespace($module) . "\\" . config("modules.paths.generator.event") . "\\" . $this->option('event');
0 ignored issues
show
Documentation introduced by
$module is of type object<Nwidart\Modules\Module>, but the function expects a object<Nwidart\Module\Module>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
    }
102
}
103