Completed
Pull Request — master (#11)
by alexfloppy
06:30
created

MakeJsonApiDemoRemove::removeSeeds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
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
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Remove JsonApi Demo entities';
22
23
    /**
24
     * @var array
25
     */
26
    protected $controllers = [
27
        'LikesController.stub' => 'LikesController.php',
28
        'SkillsController.stub' => 'SkillsController.php',
29
        'TeamsController.stub' => 'TeamsController.php',
30
        'UsersController.stub' => 'UsersController.php'
31
    ];
32
33
    protected $models = [
34
        'Like.stub' => 'Like.php',
35
        'Skill.stub' => 'Skill.php',
36
        'Team.stub' => 'Team.php'
37
    ];
38
39
    protected $migrations = [
40
        'create_likes_table.stub'                   => 'create_likes_table.php',
41
        'create_membership_table.stub'              => 'create_membership_table.php',
42
        'create_skills_table.stub'                  => 'create_skills_table.php',
43
        'create_teams_table.stub'                   => 'create_teams_table.php',
44
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
45
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
46
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
47
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
48
    ];
49
50
    protected $seeds = [
51
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
52
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
53
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
54
    ];
55
56
    protected $jsonapiEntities = [
57
        'JsonApi/Likes/Hydrator.stub' => 'JsonApi/Likes/Hydrator.php',
58
        'JsonApi/Likes/Request.stub' => 'JsonApi/Likes/Request.php',
59
        'JsonApi/Likes/Schema.stub' => 'JsonApi/Likes/Schema.php',
60
        'JsonApi/Likes/Search.stub' => 'JsonApi/Likes/Search.php',
61
        'JsonApi/Likes/Validators.stub' => 'JsonApi/Likes/Validators.php',
62
63
        'JsonApi/Skills/Hydrator.stub' => 'JsonApi/Skills/Hydrator.php',
64
        'JsonApi/Skills/Request.stub' => 'JsonApi/Skills/Request.php',
65
        'JsonApi/Skills/Schema.stub' => 'JsonApi/Skills/Schema.php',
66
        'JsonApi/Skills/Search.stub' => 'JsonApi/Skills/Search.php',
67
        'JsonApi/Skills/Validators.stub' => 'JsonApi/Skills/Validators.php',
68
69
        'JsonApi/Teams/Hydrator.stub' => 'JsonApi/Teams/Hydrator.php',
70
        'JsonApi/Teams/Request.stub' => 'JsonApi/Teams/Request.php',
71
        'JsonApi/Teams/Schema.stub' => 'JsonApi/Teams/Schema.php',
72
        'JsonApi/Teams/Search.stub' => 'JsonApi/Teams/Search.php',
73
        'JsonApi/Teams/Validators.stub' => 'JsonApi/Teams/Validators.php',
74
75
        'JsonApi/Users/Hydrator.stub' => 'JsonApi/Users/Hydrator.php',
76
        'JsonApi/Users/Request.stub' => 'JsonApi/Users/Request.php',
77
        'JsonApi/Users/Schema.stub' => 'JsonApi/Users/Schema.php',
78
        'JsonApi/Users/Search.stub' => 'JsonApi/Users/Search.php',
79
        'JsonApi/Users/Validators.stub' => 'JsonApi/Users/Validators.php',
80
    ];
81
82
    /**
83
     * Create a new command instance.
84
     *
85
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
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
        $this->removeJsonApiEntities();
100
        $this->removeModels();
101
        $this->removeControllers();
102
103
        $this->removeSeeds();
104
        $this->removeMigrations();
105
106
        $this::call('optimize');
107
108
        $this->info('JsonApi demo entities removed successfully.');
109
    }
110
111 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...
112
    {
113
        foreach ($this->models as $key => $value) {
114
            if (file_exists(app_path('Models/' . $value))) {
115
                unlink(app_path('Models/' . $value));
116
            }
117
        }
118
    }
119
120 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...
121
    {
122
        foreach ($this->controllers as $key => $value) {
123
            if (file_exists(app_path('Http/Controllers/Api/v1/' . $value))) {
124
                unlink(app_path('Http/Controllers/Api/v1/' . $value));
125
            }
126
        }
127
    }
128
129
    protected function removeMigrations()
130
    {
131
        foreach ($this->migrations as $key => $value) {
132
            $mask = database_path('migrations/*' . $value);
133
            array_map( "unlink", glob( $mask ) );
134
        }
135
    }
136
137
    protected function removeSeeds()
138
    {
139
        foreach ($this->seeds as $key => $value) {
140
            if (file_exists(database_path('seeds/' . $value))) {
141
                unlink(database_path('seeds/' . $value));
142
            }
143
        }
144
    }
145
146
    protected function removeJsonApiEntities()
147
    {
148
        try {
149
            foreach ($this->jsonapiEntities as $key => $value) {
150
                if (file_exists(app_path($value))) {
151
                    unlink(app_path($value));
152
                }
153
            }
154
            if (file_exists(app_path('JsonApi/Users'))) {
155
                rmdir(app_path('JsonApi/Users'));
156
            }
157
158
            if (file_exists(app_path('JsonApi/Likes'))) {
159
                rmdir(app_path('JsonApi/Likes'));
160
            }
161
162
            if (file_exists(app_path('JsonApi/Skills'))) {
163
                rmdir(app_path('JsonApi/Skills'));
164
            }
165
166
            if (file_exists(app_path('JsonApi/Teams'))) {
167
                rmdir(app_path('JsonApi/Teams'));
168
            }
169
170
            if (file_exists(app_path('JsonApi'))) {
171
                if(!@rmdir(app_path('JsonApi'))) {
172
                     throw new \League\Flysystem\Exception('JsonApi directory is not empty!');
173
                };
174
            }
175
        } catch (\League\Flysystem\Exception $e) {
176
            $this->warn($e->getMessage());
177
        }
178
    }
179
}
180