GenerateListenerCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 96
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getArguments() 0 7 1
A getOptions() 0 6 1
A getTemplateContents() 0 12 1
A getDestinationFilePath() 0 8 1
A getFileName() 0 4 1
A fire() 0 8 2
A getEventName() 0 4 1
A getNamespace() 0 6 1
1
<?php
2
3
namespace Consigliere\Components\Commands;
4
5
use Consigliere\Components\Component;
6
use Consigliere\Components\Support\Stub;
7
use Consigliere\Components\Traits\ComponentCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class GenerateListenerCommand extends Command
12
{
13
    use ComponentCommandTrait;
14
15
    protected $argumentName = 'name';
16
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'component: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 component';
30
31
    /**
32
     * Get the console command arguments.
33
     *
34
     * @return array
35
     */
36
    protected function getArguments()
37
    {
38
        return [
39
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
40
            ['component', InputArgument::OPTIONAL, 'The name of component will be used.'],
41
        ];
42
    }
43
44
    /**
45
     * Get the console command options.
46
     *
47
     * @return array
48
     */
49
    protected function getOptions()
50
    {
51
        return [
52
            ['event', null, InputOption::VALUE_REQUIRED, 'Event name this is listening to', null],
53
        ];
54
    }
55
56
    protected function getTemplateContents()
57
    {
58
        $component = $this->laravel['components']->findOrFail($this->getComponentName());
59
60
        return (new Stub('/listener.stub', [
61
            'NAMESPACE'          => $this->getNamespace($component),
62
            "EVENTNAME"          => $this->getEventName($component),
63
            "EVENTSHORTENEDNAME" => $this->option('event'),
64
            "CLASS"              => $this->getClass(),
65
            'DUMMYNAMESPACE'     => $this->laravel->getNamespace() . "Events",
66
        ]))->render();
67
    }
68
69
    protected function getDestinationFilePath()
70
    {
71
        $path = $this->laravel['components']->getComponentPath($this->getComponentName());
72
73
        $seederPath = $this->laravel['components']->config('paths.generator.listener');
74
75
        return $path . $seederPath . '/' . $this->getFileName() . '.php';
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    protected function getFileName()
82
    {
83
        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
    public function fire()
87
    {
88
        if (!$this->option('event')) {
89
            return $this->error('The --event option is necessary');
90
        }
91
92
        parent::fire();
93
    }
94
95
    protected function getEventName(Component $component)
96
    {
97
        return $this->getClassNamespace($component) . "\\" . config('components.paths.generator.event') . "\\" . $this->option('event');
98
    }
99
100
    private function getNamespace($component)
101
    {
102
        $namespace = str_replace('/', '\\', config('components.paths.generator.listener'));
103
104
        return $this->getClassNamespace($component) . "\\" . $namespace;
105
    }
106
}
107