ListenerMakeCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 36
dl 0
loc 115
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventName() 0 8 1
A getDefaultNamespace() 0 5 2
A getStubName() 0 15 4
A getOptions() 0 5 1
A getDestinationFilePath() 0 7 1
A getShortEventName() 0 3 1
A getArguments() 0 5 1
A getTemplateContents() 0 10 1
A getFileName() 0 3 1
1
<?php
2
3
namespace Salah3id\Domains\Commands;
4
5
use Illuminate\Support\Str;
6
use Salah3id\Domains\Domain;
7
use Salah3id\Domains\Support\Config\GenerateConfigReader;
8
use Salah3id\Domains\Support\Stub;
9
use Salah3id\Domains\Traits\DomainCommandTrait;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class ListenerMakeCommand extends GeneratorCommand
14
{
15
    use DomainCommandTrait;
16
17
    protected $argumentName = 'name';
18
19
    /**
20
     * The console command name.
21
     *
22
     * @var string
23
     */
24
    protected $name = 'domain: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 domain';
32
33
    /**
34
     * Get the console command arguments.
35
     *
36
     * @return array
37
     */
38
    protected function getArguments()
39
    {
40
        return [
41
            ['name', InputArgument::REQUIRED, 'The name of the command.'],
42
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
43
        ];
44
    }
45
46
    /**
47
     * Get the console command options.
48
     *
49
     * @return array
50
     */
51
    protected function getOptions()
52
    {
53
        return [
54
            ['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
    protected function getTemplateContents()
60
    {
61
        $domain = $this->laravel['domains']->findOrFail($this->getDomainName());
62
63
        return (new Stub($this->getStubName(), [
64
            'NAMESPACE' => $this->getClassNamespace($domain),
65
            'EVENTNAME' => $this->getEventName($domain),
66
            'SHORTEVENTNAME' => $this->getShortEventName(),
67
            'CLASS' => $this->getClass(),
68
        ]))->render();
69
    }
70
71
    public function getDefaultNamespace(): string
72
    {
73
        $domain = $this->laravel['domains'];
74
75
        return $domain->config('paths.generator.listener.namespace') ?: $domain->config('paths.generator.listener.path', 'Listeners');
76
    }
77
78
    protected function getEventName(Domain $domain)
79
    {
80
        $namespace = $this->laravel['domains']->config('namespace') . "\\" . $domain->getStudlyName();
81
        $eventPath = GenerateConfigReader::read('event');
82
83
        $eventName = $namespace . "\\" . $eventPath->getPath() . "\\" . $this->option('event');
84
85
        return str_replace('/', '\\', $eventName);
86
    }
87
88
    protected function getShortEventName()
89
    {
90
        return class_basename($this->option('event'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('event') can also be of type array; however, parameter $class of class_basename() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        return class_basename(/** @scrutinizer ignore-type */ $this->option('event'));
Loading history...
91
    }
92
93
    protected function getDestinationFilePath()
94
    {
95
        $path = $this->laravel['domains']->getDomainPath($this->getDomainName());
96
97
        $listenerPath = GenerateConfigReader::read('listener');
98
99
        return $path . $listenerPath->getPath() . '/' . $this->getFileName() . '.php';
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    protected function getFileName()
106
    {
107
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type array; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return Str::studly(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    protected function getStubName(): string
114
    {
115
        if ($this->option('queued')) {
116
            if ($this->option('event')) {
117
                return '/listener-queued.stub';
118
            }
119
120
            return '/listener-queued-duck.stub';
121
        }
122
123
        if ($this->option('event')) {
124
            return '/listener.stub';
125
        }
126
127
        return '/listener-duck.stub';
128
    }
129
}
130