ResourceMakeCommand::makeControllers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
3
namespace JustSteveKing\Laravel\ApiToolkit\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
8
class ResourceMakeCommand extends Command
9
{
10
    public $signature = 'api-toolkit:resource {name : The name of the Model you want to generate}';
11
12
    public $description = 'Generate all the usual parts for an API Resource';
13
14
    public function handle()
15
    {
16
        if (! is_string($this->argument('name'))) {
17
            return;
18
        }
19
20
        $name = Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type string[]; 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

20
        $name = Str::studly(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
21
        $this->comment("Generating boilerplate for {$name}");
22
23
        $this->makeModel($name);
24
        $this->makeFactory($name);
25
        $this->makeSeeder($name);
26
        $this->makePolicy($name);
27
        $this->makeFormRequests($name);
28
        $this->makeApiResource($name);
29
        $this->makeControllers($name);
30
    }
31
32
    protected function makeModel(string $name): void
33
    {
34
        $this->comment("Creating Model and Migration for {$name}");
35
        $this->call("make:model", [
36
            'name' => $name,
37
            '-m',
38
        ]);
39
    }
40
41
    protected function makeFactory(string $name): void
42
    {
43
        $this->comment("Creating Factory for {$name}");
44
        $this->call("make:factory", [
45
            'name' => "{$name}Factory",
46
            '--model' => $name,
47
        ]);
48
    }
49
50
    protected function makeSeeder(string $name): void
51
    {
52
        $this->comment("Creating Seeder for {$name}");
53
        $this->call("make:seeder", [
54
            'name' => sprintf(config('api-toolkit.seeder_name'), $name),
55
        ]);
56
    }
57
58
    protected function makePolicy(string $name): void
59
    {
60
        $this->comment("Creating Model Policy for {$name}");
61
        $this->call('make:policy', [
62
            'name' => sprintf(config('api-toolkit.policy_name'), $name),
63
            '--model' => $name,
64
        ]);
65
    }
66
67
    protected function makeFormRequests(string $name): void
68
    {
69
        foreach (config('api-toolkit.form_requests') as $request) {
70
            $this->comment("Creating {$request} for model {$name}");
71
            $this->call('make:request', [
72
                'name' => "Api\\{$name}\\{$request}",
73
            ]);
74
        }
75
    }
76
77
    protected function makeApiResource(string $name): void
78
    {
79
        $this->comment("Creating API Resource for {$name}");
80
        $this->call('make:resource', [
81
            'name' => sprintf(config('api-toolkit.resource_name'), $name),
82
        ]);
83
    }
84
85
    protected function makeControllers(string $name): void
86
    {
87
        foreach (config('api-toolkit.controllers') as $controller) {
88
            $this->comment("Creating {$controller['name']} for model {$name}");
89
            $this->call('make:controller', [
90
                'name' => "{$name}\\{$controller['name']}",
91
                implode(' ', $controller['options']),
92
            ]);
93
        }
94
    }
95
}
96