MakeServiceCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 129
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultNamespace() 0 3 1
A getStub() 0 7 2
A buildClass() 0 24 4
A alreadyExists() 0 4 2
A resolveStubPath() 0 5 2
A getOptions() 0 6 1
1
<?php
2
3
namespace Ikechukwukalu\Makeservice\Console\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Attribute\AsCommand;
8
use Symfony\Component\Console\Input\InputOption;
9
10
#[AsCommand(name: 'make:service')]
11
class MakeServiceCommand extends GeneratorCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'make:service';
19
20
    /**
21
     * The name of the console command.
22
     *
23
     * This name is used to identify the command during lazy loading.
24
     *
25
     * @var string|null
26
     *
27
     * @deprecated
28
     */
29
    protected static $defaultName = 'make:service';
30
31
    /**
32
     * The console command description.
33
     *
34
     * @var string
35
     */
36
    protected $description = 'Create a new service class';
37
38
    /**
39
     * The type of class being generated.
40
     *
41
     * @var string
42
     */
43
    protected $type = 'Service';
44
45
    /**
46
     * Build the class with the given name.
47
     *
48
     * @param  string  $name
49
     * @return string
50
     */
51
    protected function buildClass($name)
52
    {
53
        if (!$request = $this->option('request')) {
54
            return parent::buildClass($name);
55
        }
56
57
        if ($this->option('extra')) {
58
            $this->call('make:request', ['name' => $request]);
59
        }
60
61
        if (! Str::startsWith($request, [
62
            $this->laravel->getNamespace(),
63
            'Illuminate',
64
            '\\',
65
        ])) {
66
            $request = $this->laravel->getNamespace().'Http\\Requests\\'.str_replace('/', '\\', $request);
67
        }
68
69
        $stub = str_replace(
70
            ['DummyRequest', '{{ request }}'], class_basename($request), parent::buildClass($name)
71
        );
72
73
        return str_replace(
74
            ['DummyFullRequest', '{{ requestNamespace }}'], trim($request, '\\'), $stub
75
        );
76
    }
77
78
    /**
79
     * Determine if the class already exists.
80
     *
81
     * @param  string  $rawName
82
     * @return bool
83
     */
84
    protected function alreadyExists($rawName)
85
    {
86
        return class_exists($rawName) ||
87
               $this->files->exists($this->getPath($this->qualifyClass($rawName)));
88
    }
89
90
    /**
91
     * Get the stub file for the generator.
92
     *
93
     * @return string
94
     */
95
    protected function getStub()
96
    {
97
        if ($this->option('request')) {
98
            return __DIR__.'/stubs/service.stub';
99
        }
100
101
        return __DIR__.'/stubs/service-duck.stub';
102
    }
103
104
    /**
105
     * Resolve the fully-qualified path to the stub.
106
     *
107
     * @param  string  $stub
108
     * @return string
109
     */
110
    protected function resolveStubPath($stub)
111
    {
112
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
113
                        ? $customPath
114
                        : __DIR__.$stub;
115
    }
116
117
    /**
118
     * Get the default namespace for the class.
119
     *
120
     * @param  string  $rootNamespace
121
     * @return string
122
     */
123
    protected function getDefaultNamespace($rootNamespace)
124
    {
125
        return $rootNamespace.'\Services';
126
    }
127
128
    /**
129
     * Get the console command options.
130
     *
131
     * @return array
132
     */
133
    protected function getOptions()
134
    {
135
        return [
136
            ['extra', 'e', InputOption::VALUE_NONE, 'Create an form request class for this service'],
137
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the service already exists'],
138
            ['request', 'r', InputOption::VALUE_OPTIONAL, 'Create a form request namespace class for this service'],
139
        ];
140
    }
141
}
142