Passed
Push — main ( 3cebb0...a2c37a )
by PRATIK
03:46
created

MakeAPIResourceCommand::getModelPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Pratiksh\Adminetic\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Pratiksh\Adminetic\Services\MakeAPIResource;
7
8
class MakeAPIResourceCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'make:api {name : Model Class (Singular), e.g role, Place, Car, Post}  {--rest} {--client}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Make API Resource for Model';
23
24
    /**
25
     * Create a new command instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return int
38
     */
39
    public function handle()
40
    {
41
        $given_name = $this->argument('name');
42
        $name = $this->getModelName($given_name);
43
        $path = $this->getModelPath($given_name);
44
        if ($this->option('rest')) {
45
            MakeAPIResource::makeRestAPI($name, $path);
46
            $this->info('Restful API Resource created for model ' . $name);
47
        } elseif ($this->option('client')) {
48
            MakeAPIResource::makeClientAPI($name, $path);
49
            $this->info('Client API created for model ' . $name);
50
        } else {
51
            MakeAPIResource::makeAPI($name, $path);
52
            $this->info('API Resource created for model ' . $name);
53
        }
54
    }
55
56
    public function getModelName($given_name)
57
    {
58
        $explode_path = preg_split("#/#", $given_name);
59
        return end($explode_path);
60
    }
61
62
    public function getModelPath($given_name)
63
    {
64
        $explode_path = preg_split("#/#", $given_name);
65
        return count($explode_path) > 1 ? str_replace('/', '\\', $given_name) : ("App\\Models\\Admin\\" . $given_name);
66
    }
67
}
68