LaravelApiGenerator   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 108
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generate() 0 4 1
A directoryCreate() 0 9 3
A generateController() 0 15 3
A generateResource() 0 19 4
A generateCollection() 0 12 2
A generateRoute() 0 26 5
A getStubContents() 0 4 1
1
<?php
2
3
namespace Bhavingajjar\LaravelApiGenerator;
4
5
use Illuminate\Support\Str;
6
7
class LaravelApiGenerator
8
{
9
    const STUB_DIR = __DIR__.'/resources/stubs/';
10
    protected $model;
11
    protected $result = false;
12
13
    public function __construct(string $model)
14
    {
15
        $this->model = $model;
16
        self::generate();
17
    }
18
19
    public function generate()
20
    {
21
        self::directoryCreate();
22
    }
23
24
    public function directoryCreate()
25
    {
26
        if (! file_exists(base_path('app/Http/Controllers/Api'))) {
27
            mkdir(base_path('app/Http/Controllers/Api'));
28
        }
29
        if (! file_exists(base_path('app/Http/Resources'))) {
30
            mkdir(base_path('app/Http/Resources'));
31
        }
32
    }
33
34
    public function generateController()
35
    {
36
        $this->result = false;
37
        if (! file_exists(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'))) {
38
            $template = self::getStubContents('controller.stub');
39
            $template = str_replace('{{modelName}}', $this->model, $template);
40
            $template = str_replace('{{modelNameLower}}', strtolower($this->model), $template);
41
            $template = str_replace('{{modelNameCamel}}', Str::camel($this->model), $template);
42
            $template = str_replace('{{modelNameSpace}}', is_dir(base_path('app/Models')) ? 'Models\\'.$this->model : $this->model, $template);
43
            file_put_contents(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'), $template);
44
            $this->result = true;
45
        }
46
47
        return $this->result;
48
    }
49
50
    public function generateResource()
51
    {
52
        $this->result = false;
53
        if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Resource.php'))) {
54
            $model = is_dir(base_path('app/Models')) ? app('App\\Models\\'.$this->model) : app('App\\'.$this->model);
55
            $columns = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
56
            $print_columns = null;
57
            foreach ($columns as $key => $column) {
58
                $print_columns .= "'".$column."'".' => $this->'.$column.', '."\n \t\t\t";
59
            }
60
            $template = self::getStubContents('resource.stub');
61
            $template = str_replace('{{modelName}}', $this->model, $template);
62
            $template = str_replace('{{columns}}', $print_columns, $template);
63
            file_put_contents(base_path('app/Http/Resources/'.$this->model.'Resource.php'), $template);
64
            $this->result = true;
65
        }
66
67
        return $this->result;
68
    }
69
70
    public function generateCollection()
71
    {
72
        $this->result = false;
73
        if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Collection.php'))) {
74
            $template = self::getStubContents('collection.stub');
75
            $template = str_replace('{{modelName}}', $this->model, $template);
76
            file_put_contents(base_path('app/Http/Resources/'.$this->model.'Collection.php'), $template);
77
            $this->result = true;
78
        }
79
80
        return $this->result;
81
    }
82
83
    public function generateRoute()
84
    {
85
        $this->result = false;
86
        if (app()->version() >= 8) {
87
            $nameSpace = "\nuse App\Http\Controllers\Api\{{modelName}}Controller;";
88
            $template = "Route::apiResource('{{modelNameLower}}', {{modelName}}Controller::class);\n";
89
            $nameSpace = str_replace('{{modelName}}', $this->model, $nameSpace);
90
        } else {
91
            $template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');\n";
92
        }
93
        $route = str_replace('{{modelNameLower}}', Str::camel(Str::plural($this->model)), $template);
94
        $route = str_replace('{{modelName}}', $this->model, $route);
95
        if (! strpos(file_get_contents(base_path('routes/api.php')), $route)) {
96
            file_put_contents(base_path('routes/api.php'), $route, FILE_APPEND);
97
            if (app()->version() >= 8) {
98
                if (! strpos(file_get_contents(base_path('routes/api.php')), $nameSpace)) {
99
                    $lines = file(base_path('routes/api.php'));
100
                    $lines[0] = $lines[0]."\n".$nameSpace;
0 ignored issues
show
Bug introduced by
The variable $nameSpace does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
101
                    file_put_contents(base_path('routes/api.php'), $lines);
102
                }
103
            }
104
            $this->result = true;
105
        }
106
107
        return $this->result;
108
    }
109
110
    private function getStubContents($stubName)
111
    {
112
        return file_get_contents(self::STUB_DIR.$stubName);
113
    }
114
}
115