Completed
Pull Request — master (#11)
by alexfloppy
03:25
created

MakeJsonApiDemoRemove::removeJsonApiEntities()   F

Complexity

Conditions 10
Paths 332

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 33
rs 3.1304
cc 10
eloc 18
nc 332
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
7
class MakeJsonApiDemoRemove extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'make:demo-remove
15
        {--fake : Make fake removing directories and files for test}';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Remove JsonApi Demo entities';
23
24
    /**
25
     * @var array
26
     */
27
    protected $controllers = [
28
        'LikesController.stub' => 'LikesController.php',
29
        'SkillsController.stub' => 'SkillsController.php',
30
        'TeamsController.stub' => 'TeamsController.php',
31
        'UsersController.stub' => 'UsersController.php'
32
    ];
33
34
    protected $models = [
35
        'Like.stub' => 'Like.php',
36
        'Skill.stub' => 'Skill.php',
37
        'Team.stub' => 'Team.php'
38
    ];
39
40
    protected $migrations = [
41
        'create_likes_table.stub'                   => 'create_likes_table.php',
42
        'create_membership_table.stub'              => 'create_membership_table.php',
43
        'create_skills_table.stub'                  => 'create_skills_table.php',
44
        'create_teams_table.stub'                   => 'create_teams_table.php',
45
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
46
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
47
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
48
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
49
    ];
50
51
    protected $seeds = [
52
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
53
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
54
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
55
    ];
56
57
    protected $jsonapiEntities = [
58
        'JsonApi/Likes/Hydrator.stub' => 'JsonApi/Likes/Hydrator.php',
59
        'JsonApi/Likes/Request.stub' => 'JsonApi/Likes/Request.php',
60
        'JsonApi/Likes/Schema.stub' => 'JsonApi/Likes/Schema.php',
61
        'JsonApi/Likes/Search.stub' => 'JsonApi/Likes/Search.php',
62
        'JsonApi/Likes/Validators.stub' => 'JsonApi/Likes/Validators.php',
63
64
        'JsonApi/Skills/Hydrator.stub' => 'JsonApi/Skills/Hydrator.php',
65
        'JsonApi/Skills/Request.stub' => 'JsonApi/Skills/Request.php',
66
        'JsonApi/Skills/Schema.stub' => 'JsonApi/Skills/Schema.php',
67
        'JsonApi/Skills/Search.stub' => 'JsonApi/Skills/Search.php',
68
        'JsonApi/Skills/Validators.stub' => 'JsonApi/Skills/Validators.php',
69
70
        'JsonApi/Teams/Hydrator.stub' => 'JsonApi/Teams/Hydrator.php',
71
        'JsonApi/Teams/Request.stub' => 'JsonApi/Teams/Request.php',
72
        'JsonApi/Teams/Schema.stub' => 'JsonApi/Teams/Schema.php',
73
        'JsonApi/Teams/Search.stub' => 'JsonApi/Teams/Search.php',
74
        'JsonApi/Teams/Validators.stub' => 'JsonApi/Teams/Validators.php',
75
76
        'JsonApi/Users/Hydrator.stub' => 'JsonApi/Users/Hydrator.php',
77
        'JsonApi/Users/Request.stub' => 'JsonApi/Users/Request.php',
78
        'JsonApi/Users/Schema.stub' => 'JsonApi/Users/Schema.php',
79
        'JsonApi/Users/Search.stub' => 'JsonApi/Users/Search.php',
80
        'JsonApi/Users/Validators.stub' => 'JsonApi/Users/Validators.php',
81
    ];
82
83
    /**
84
     * Create a new command instance.
85
     *
86
     */
87
    public function __construct()
88
    {
89
        parent::__construct();
90
    }
91
92
    /**
93
     * Execute the console command.
94
     *
95
     * @return mixed
96
     */
97
    public function handle()
98
    {
99
        if($this->option('fake')) {
100
            $this->info('JsonApi demo entities fake removed successfully.');
101
            return true;
102
        }
103
104
        $this->removeJsonApiEntities();
105
        $this->removeModels();
106
        $this->removeControllers();
107
108
        $this->removeSeeds();
109
        $this->removeMigrations();
110
111
        $this::call('optimize');
112
113
        $this->info('JsonApi demo entities removed successfully.');
114
    }
115
116 View Code Duplication
    protected function removeModels()
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...
117
    {
118
        foreach ($this->models as $key => $value) {
119
            if (file_exists(app_path('Models/' . $value))) {
120
                unlink(app_path('Models/' . $value));
121
            }
122
        }
123
    }
124
125 View Code Duplication
    protected function removeControllers()
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...
126
    {
127
        foreach ($this->controllers as $key => $value) {
128
            if (file_exists(app_path('Http/Controllers/Api/v1/' . $value))) {
129
                unlink(app_path('Http/Controllers/Api/v1/' . $value));
130
            }
131
        }
132
    }
133
134
    protected function removeMigrations()
135
    {
136
        foreach ($this->migrations as $key => $value) {
137
            $mask = database_path('migrations/*' . $value);
138
            array_map( "unlink", glob( $mask ) );
139
        }
140
    }
141
142
    protected function removeSeeds()
143
    {
144
        foreach ($this->seeds as $key => $value) {
145
            if (file_exists(database_path('seeds/' . $value))) {
146
                unlink(database_path('seeds/' . $value));
147
            }
148
        }
149
    }
150
151
    protected function removeJsonApiEntities()
152
    {
153
        try {
154
            foreach ($this->jsonapiEntities as $key => $value) {
155
                if (file_exists(app_path($value))) {
156
                    unlink(app_path($value));
157
                }
158
            }
159
            if (file_exists(app_path('JsonApi/Users'))) {
160
                rmdir(app_path('JsonApi/Users'));
161
            }
162
163
            if (file_exists(app_path('JsonApi/Likes'))) {
164
                rmdir(app_path('JsonApi/Likes'));
165
            }
166
167
            if (file_exists(app_path('JsonApi/Skills'))) {
168
                rmdir(app_path('JsonApi/Skills'));
169
            }
170
171
            if (file_exists(app_path('JsonApi/Teams'))) {
172
                rmdir(app_path('JsonApi/Teams'));
173
            }
174
175
            if (file_exists(app_path('JsonApi'))) {
176
                if(!@rmdir(app_path('JsonApi'))) {
177
                     throw new \League\Flysystem\Exception('JsonApi directory is not empty!');
178
                };
179
            }
180
        } catch (\League\Flysystem\Exception $e) {
181
            $this->warn($e->getMessage());
182
        }
183
    }
184
}
185