MagicApiCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

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

6 Methods

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