Completed
Pull Request — master (#11)
by alexfloppy
03:17
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
            {--test  : Add files postfix for test}
16
            {--fake : Make fake removing directories and files for test}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Remove JsonApi Demo entities';
24
25
    protected $testPostfix = '-test';
26
27
    /**
28
     * @var array
29
     */
30
    protected $controllers = [
31
        'LikesController.stub' => 'LikesController.php',
32
        'SkillsController.stub' => 'SkillsController.php',
33
        'TeamsController.stub' => 'TeamsController.php',
34
        'UsersController.stub' => 'UsersController.php'
35
    ];
36
37
    protected $models = [
38
        'Like.stub' => 'Like.php',
39
        'Skill.stub' => 'Skill.php',
40
        'Team.stub' => 'Team.php'
41
    ];
42
43
    protected $migrations = [
44
        'create_likes_table.stub'                   => 'create_likes_table.php',
45
        'create_membership_table.stub'              => 'create_membership_table.php',
46
        'create_skills_table.stub'                  => 'create_skills_table.php',
47
        'create_teams_table.stub'                   => 'create_teams_table.php',
48
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
49
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
50
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
51
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
52
    ];
53
54
    protected $seeds = [
55
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
56
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
57
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
58
    ];
59
60
    protected $jsonapiEntities = [
61
        'JsonApi/Likes/Hydrator.stub' => 'JsonApi/Likes/Hydrator.php',
62
        'JsonApi/Likes/Request.stub' => 'JsonApi/Likes/Request.php',
63
        'JsonApi/Likes/Schema.stub' => 'JsonApi/Likes/Schema.php',
64
        'JsonApi/Likes/Search.stub' => 'JsonApi/Likes/Search.php',
65
        'JsonApi/Likes/Validators.stub' => 'JsonApi/Likes/Validators.php',
66
67
        'JsonApi/Skills/Hydrator.stub' => 'JsonApi/Skills/Hydrator.php',
68
        'JsonApi/Skills/Request.stub' => 'JsonApi/Skills/Request.php',
69
        'JsonApi/Skills/Schema.stub' => 'JsonApi/Skills/Schema.php',
70
        'JsonApi/Skills/Search.stub' => 'JsonApi/Skills/Search.php',
71
        'JsonApi/Skills/Validators.stub' => 'JsonApi/Skills/Validators.php',
72
73
        'JsonApi/Teams/Hydrator.stub' => 'JsonApi/Teams/Hydrator.php',
74
        'JsonApi/Teams/Request.stub' => 'JsonApi/Teams/Request.php',
75
        'JsonApi/Teams/Schema.stub' => 'JsonApi/Teams/Schema.php',
76
        'JsonApi/Teams/Search.stub' => 'JsonApi/Teams/Search.php',
77
        'JsonApi/Teams/Validators.stub' => 'JsonApi/Teams/Validators.php',
78
79
        'JsonApi/Users/Hydrator.stub' => 'JsonApi/Users/Hydrator.php',
80
        'JsonApi/Users/Request.stub' => 'JsonApi/Users/Request.php',
81
        'JsonApi/Users/Schema.stub' => 'JsonApi/Users/Schema.php',
82
        'JsonApi/Users/Search.stub' => 'JsonApi/Users/Search.php',
83
        'JsonApi/Users/Validators.stub' => 'JsonApi/Users/Validators.php',
84
    ];
85
86
    protected $jsonapiEntitiesTest = [
87
        'JsonApi/Likes/Hydrator.stub' => 'JsonApi-test/Likes/Hydrator.php',
88
        'JsonApi/Likes/Request.stub' => 'JsonApi-test/Likes/Request.php',
89
        'JsonApi/Likes/Schema.stub' => 'JsonApi-test/Likes/Schema.php',
90
        'JsonApi/Likes/Search.stub' => 'JsonApi-test/Likes/Search.php',
91
        'JsonApi/Likes/Validators.stub' => 'JsonApi-test/Likes/Validators.php',
92
93
        'JsonApi/Skills/Hydrator.stub' => 'JsonApi-test/Skills/Hydrator.php',
94
        'JsonApi/Skills/Request.stub' => 'JsonApi-test/Skills/Request.php',
95
        'JsonApi/Skills/Schema.stub' => 'JsonApi-test/Skills/Schema.php',
96
        'JsonApi/Skills/Search.stub' => 'JsonApi-test/Skills/Search.php',
97
        'JsonApi/Skills/Validators.stub' => 'JsonApi-test/Skills/Validators.php',
98
99
        'JsonApi/Teams/Hydrator.stub' => 'JsonApi-test/Teams/Hydrator.php',
100
        'JsonApi/Teams/Request.stub' => 'JsonApi-test/Teams/Request.php',
101
        'JsonApi/Teams/Schema.stub' => 'JsonApi-test/Teams/Schema.php',
102
        'JsonApi/Teams/Search.stub' => 'JsonApi-test/Teams/Search.php',
103
        'JsonApi/Teams/Validators.stub' => 'JsonApi-test/Teams/Validators.php',
104
105
        'JsonApi/Users/Hydrator.stub' => 'JsonApi-test/Users/Hydrator.php',
106
        'JsonApi/Users/Request.stub' => 'JsonApi-test/Users/Request.php',
107
        'JsonApi/Users/Schema.stub' => 'JsonApi-test/Users/Schema.php',
108
        'JsonApi/Users/Search.stub' => 'JsonApi-test/Users/Search.php',
109
        'JsonApi/Users/Validators.stub' => 'JsonApi-test/Users/Validators.php',
110
    ];
111
112
    /**
113
     * Create a new command instance.
114
     *
115
     */
116
    public function __construct()
117
    {
118
        parent::__construct();
119
    }
120
121
    /**
122
     * Execute the console command.
123
     *
124
     * @return mixed
125
     */
126
    public function handle()
127
    {
128
        if ($this->option('test')) {
129
            $this->setupTest();
130
        }
131
132
        if($this->option('fake')) {
133
            $this->info('JsonApi demo entities fake removed successfully.');
134
            return true;
135
        }
136
137
        $this->removeJsonApiEntities();
138
        $this->removeModels();
139
        $this->removeControllers();
140
141
        $this->removeSeeds();
142
        $this->removeMigrations();
143
144
        $this::call('optimize');
145
146
        $this->info('JsonApi demo entities removed successfully.');
147
    }
148
149 View Code Duplication
    protected function setupTest()
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...
150
    {
151
        foreach ($this->migrations as $key => $value) {
152
            $this->migrations[$key] = $value . $this->testPostfix;
153
        }
154
        foreach ($this->seeds as $key => $value) {
155
            $this->seeds[$key] = $value . $this->testPostfix;
156
        }
157
        foreach ($this->controllers as $key => $value) {
158
            $this->controllers[$key] = $value . $this->testPostfix;
159
        }
160
        foreach ($this->models as $key => $value) {
161
            $this->models[$key] = $value . $this->testPostfix;
162
        }
163
        $this->jsonapiEntities = $this->jsonapiEntitiesTest;
164
165
//        foreach ($this->jsonapiEntities as $key => $value) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
166
//            $this->jsonapiEntities[$key] = $value . $this->testPostfix;
167
//        }
168
    }
169
170 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...
171
    {
172
        foreach ($this->models as $key => $value) {
173
            if (file_exists(app_path('Models/' . $value))) {
174
                unlink(app_path('Models/' . $value));
175
            }
176
        }
177
    }
178
179 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...
180
    {
181
        foreach ($this->controllers as $key => $value) {
182
            if (file_exists(app_path('Http/Controllers/Api/v1/' . $value))) {
183
                unlink(app_path('Http/Controllers/Api/v1/' . $value));
184
            }
185
        }
186
    }
187
188
    protected function removeMigrations()
189
    {
190
        foreach ($this->migrations as $key => $value) {
191
            $mask = database_path('migrations/*' . $value);
192
            array_map( "unlink", glob( $mask ) );
193
        }
194
    }
195
196
    protected function removeSeeds()
197
    {
198
        foreach ($this->seeds as $key => $value) {
199
            if (file_exists(database_path('seeds/' . $value))) {
200
                unlink(database_path('seeds/' . $value));
201
            }
202
        }
203
    }
204
205
    protected function removeJsonApiEntities()
206
    {
207
        try {
208
            foreach ($this->jsonapiEntities as $key => $value) {
209
                if (file_exists(app_path($value))) {
210
                    unlink(app_path($value));
211
                }
212
            }
213
            if (file_exists(app_path('JsonApi/Users'))) {
214
                rmdir(app_path('JsonApi/Users'));
215
            }
216
217
            if (file_exists(app_path('JsonApi/Likes'))) {
218
                rmdir(app_path('JsonApi/Likes'));
219
            }
220
221
            if (file_exists(app_path('JsonApi/Skills'))) {
222
                rmdir(app_path('JsonApi/Skills'));
223
            }
224
225
            if (file_exists(app_path('JsonApi/Teams'))) {
226
                rmdir(app_path('JsonApi/Teams'));
227
            }
228
229
            if (file_exists(app_path('JsonApi'))) {
230
                if(!@rmdir(app_path('JsonApi'))) {
231
                     throw new \League\Flysystem\Exception('JsonApi directory is not empty!');
232
                };
233
            }
234
        } catch (\League\Flysystem\Exception $e) {
235
            $this->warn($e->getMessage());
236
        }
237
    }
238
}
239