Completed
Branch master (8da805)
by Bhavin
02:04 queued 59s
created

LaravelApiGenerator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 26.83 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 22
loc 82
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 13 2
A generateRoute() 0 12 2
A generateResource() 11 11 2
A generateCollection() 11 11 2
A getStubContents() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        if (! file_exists(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'))) {
37
            $template = self::getStubContents('controller.stub');
38
            $template = str_replace('{{modelName}}', $this->model, $template);
39
            $template = str_replace('{{modelNameLower}}', strtolower($this->model), $template);
40
            $template = str_replace('{{modelNameCamel}}', Str::camel($this->model), $template);
41
            file_put_contents(base_path('app/Http/Controllers/Api/'.$this->model.'Controller.php'), $template);
42
            $this->result = true;
43
        }
44
45
        return $this->result;
46
    }
47
48 View Code Duplication
    public function generateResource()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Resource.php'))) {
51
            $template = self::getStubContents('resource.stub');
52
            $template = str_replace('{{modelName}}', $this->model, $template);
53
            file_put_contents(base_path('app/Http/Resources/'.$this->model.'Resource.php'), $template);
54
            $this->result = true;
55
        }
56
57
        return $this->result;
58
    }
59
60 View Code Duplication
    public function generateCollection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if (! file_exists(base_path('app/Http/Resources/'.$this->model.'Collection.php'))) {
63
            $template = self::getStubContents('collection.stub');
64
            $template = str_replace('{{modelName}}', $this->model, $template);
65
            file_put_contents(base_path('app/Http/Resources/'.$this->model.'Collection.php'), $template);
66
            $this->result = true;
67
        }
68
69
        return $this->result;
70
    }
71
72
    public function generateRoute()
73
    {
74
        $template = "Route::apiResource('{{modelNameLower}}', 'Api\{{modelName}}Controller');"."\n";
75
        $route = str_replace('{{modelNameLower}}', Str::camel(Str::plural($this->model)), $template);
76
        $route = str_replace('{{modelName}}', $this->model, $route);
77
        if (! strpos(file_get_contents(base_path('routes/api.php')), $route)) {
78
            file_put_contents(base_path('routes/api.php'), $route, FILE_APPEND);
79
            $this->result = true;
80
        }
81
82
        return $this->result;
83
    }
84
85
    private function getStubContents($stubName){
86
        return file_get_contents(self::STUB_DIR.$stubName);
87
    }
88
}
89