ListenerMakeCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 26
c 1
b 0
f 0
dl 0
loc 69
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 6 2
A getEventName() 0 5 1
A getDestinationFilePath() 0 7 1
A getStubName() 0 15 4
A getTemplateContents() 0 11 1
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Rawilk\LaravelModules\Module;
6
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
7
use Rawilk\LaravelModules\Support\Stub;
8
use Rawilk\LaravelModules\Traits\ModuleCommands;
9
10
class ListenerMakeCommand extends GeneratorCommand
11
{
12
    use ModuleCommands;
13
14
    /** @var string */
15
    protected $argumentName = 'name';
16
17
    /** @var string */
18
    protected $signature = 'module:make-listener
19
                            {name : The name of the listener}
20
                            {module? : The name of the module to create a new listener for}
21
                            {--e|event= : The event class being listened for}
22
                            {--queued : Indicates the event listener should be queued}';
23
24
    /** @var string */
25
    protected $description = 'Create a new event listener class for the specified module.';
26
27
    protected function getDefaultNamespace(): string
28
    {
29
        /** @var \Rawilk\LaravelModules\Contracts\Repository $module */
30
        $module = $this->laravel['modules'];
31
32
        return $module->config('paths.generator.listener.namespace') ?: $module->config('paths.generator.listener.path', 'Listeners');
33
    }
34
35
    protected function getDestinationFilePath(): string
36
    {
37
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
38
39
        $listenerPath = GenerateConfigReader::read('listener');
40
41
        return $path . $listenerPath->getPath() . '/' . $this->getFileName() . '.php';
42
    }
43
44
    protected function getTemplateContents(): string
45
    {
46
        /** @var \Rawilk\LaravelModules\Module $module */
47
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
48
49
        return (new Stub($this->getStubName(), [
50
            'NAMESPACE'      => $this->getClassNamespace($module),
51
            'EVENTNAME'      => $this->getEventName($module),
52
            'SHORTEVENTNAME' => $this->option('event'),
53
            'CLASS'          => $this->getClass(),
54
        ]))->render();
55
    }
56
57
    private function getEventName(Module $module): string
58
    {
59
        $eventPath = GenerateConfigReader::read('event');
60
61
        return $this->getClassNamespace($module) . '\\' . $eventPath->getPath() . '\\' . $this->option('event');
62
    }
63
64
    private function getStubName(): string
65
    {
66
        if ($this->option('queued')) {
67
            if ($this->option('event')) {
68
                return '/listener-queued.stub';
69
            }
70
71
            return '/listener-queued-duck.stub';
72
        }
73
74
        if ($this->option('event')) {
75
            return '/listener.stub';
76
        }
77
78
        return '/listener-duck.stub';
79
    }
80
}
81