MakeAPIForAllModelCommand::handle()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
nc 6
nop 0
dl 0
loc 19
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Schema;
7
use Illuminate\Support\Str;
8
use Pratiksh\Adminetic\Services\MakeAPIResource;
9
10
class MakeAPIForAllModelCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'make:api-all {--rest} {--client} {--v=v1} {--except=*} {--path=}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Make API Resource for all available model';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return int
40
     */
41
    public function handle()
42
    {
43
        if ($this->confirm('Do you wish to continue? Any existing api resources might be replaced.')) {
44
            $excluded_models = $this->option('except');
45
            $path = $this->option('path') ?? 'Models';
46
            $models = getAllModelNames(app_path($path));
47
            foreach ($models as $name) {
48
                if (Schema::hasTable(Str::plural($name)) && ! in_array($name, $excluded_models ?? [])) {
49
                    $path = $this->getModelPath($name);
50
                    $version = $this->option('v');
51
                    if ($this->option('rest')) {
52
                        MakeAPIResource::makeRestAPI($name, $path, $version);
53
                        $this->info('Restful API Resource created for model '.$name.' ... ✅');
54
                    } elseif ($this->option('client')) {
55
                        MakeAPIResource::makeClientAPI($name, $path, $version);
56
                        $this->info('Client API created for model '.$name.' ... ✅');
57
                    } else {
58
                        MakeAPIResource::makeAPI($name, $path, $version);
59
                        $this->info('API Resource created for model '.$name.' ... ✅');
60
                    }
61
                }
62
            }
63
        }
64
    }
65
66
    public function getModelName($given_name)
67
    {
68
        $explode_path = preg_split('#/#', $given_name);
69
70
        return end($explode_path);
71
    }
72
73
    public function getModelPath($given_name)
74
    {
75
        $explode_path = preg_split('#/#', $given_name);
76
77
        return count($explode_path) > 1 ? str_replace('/', '\\', $given_name) : ('App\\Models\\Admin\\'.$given_name);
78
    }
79
}
80