Completed
Push — master ( dfff41...bd996f )
by
unknown
11s
created

MakeJsonApiDemo::createDirectories()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 16
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
10
class MakeJsonApiDemo extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'make:demo                    
18
                    {--force : Overwrite existing files by default}
19
                    {--test  : Add files postfix for test}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create JsonApi Demo entities';
27
28
    protected $testPostfix = '-test';
29
30
    protected $migrations = [
31
        'create_likes_table.stub'                   => 'create_likes_table.php',
32
        'create_membership_table.stub'              => 'create_membership_table.php',
33
        'create_skills_table.stub'                  => 'create_skills_table.php',
34
        'create_teams_table.stub'                   => 'create_teams_table.php',
35
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
36
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
37
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
38
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
39
    ];
40
41
    protected $seeds = [
42
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
43
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
44
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
45
    ];
46
47
    protected $controllers = [
48
        'LikesController.stub' => 'LikesController.php',
49
        'SkillsController.stub' => 'SkillsController.php',
50
        'TeamsController.stub' => 'TeamsController.php',
51
        'UsersController.stub' => 'UsersController.php'
52
    ];
53
54
    protected $models = [
55
        'Like.stub' => 'Like.php',
56
        'Skill.stub' => 'Skill.php',
57
        'Team.stub' => 'Team.php'
58
    ];
59
60
    protected $jsonapiEntities = [
61
        'JsonApi/Likes/Hydrator.php' => 'JsonApi/Likes/Hydrator.php',
62
        'JsonApi/Likes/Request.php' => 'JsonApi/Likes/Request.php',
63
        'JsonApi/Likes/Schema.php' => 'JsonApi/Likes/Schema.php',
64
        'JsonApi/Likes/Search.php' => 'JsonApi/Likes/Search.php',
65
        'JsonApi/Likes/Validators.php' => 'JsonApi/Likes/Validators.php',
66
67
        'JsonApi/Skills/Hydrator.php' => 'JsonApi/Skills/Hydrator.php',
68
        'JsonApi/Skills/Request.php' => 'JsonApi/Skills/Request.php',
69
        'JsonApi/Skills/Schema.php' => 'JsonApi/Skills/Schema.php',
70
        'JsonApi/Skills/Search.php' => 'JsonApi/Skills/Search.php',
71
        'JsonApi/Skills/Validators.php' => 'JsonApi/Skills/Validators.php',
72
73
        'JsonApi/Teams/Hydrator.php' => 'JsonApi/Teams/Hydrator.php',
74
        'JsonApi/Teams/Request.php' => 'JsonApi/Teams/Request.php',
75
        'JsonApi/Teams/Schema.php' => 'JsonApi/Teams/Schema.php',
76
        'JsonApi/Teams/Search.php' => 'JsonApi/Teams/Search.php',
77
        'JsonApi/Teams/Validators.php' => 'JsonApi/Teams/Validators.php',
78
79
        'JsonApi/Users/Hydrator.php' => 'JsonApi/Users/Hydrator.php',
80
        'JsonApi/Users/Request.php' => 'JsonApi/Users/Request.php',
81
        'JsonApi/Users/Schema.php' => 'JsonApi/Users/Schema.php',
82
        'JsonApi/Users/Search.php' => 'JsonApi/Users/Search.php',
83
        'JsonApi/Users/Validators.php' => 'JsonApi/Users/Validators.php',
84
    ];
85
86
87
    /**
88
     * Create a new command instance.
89
     *
90
     */
91
    public function __construct()
92
    {
93
        parent::__construct();
94
    }
95
96
    /**
97
     * Execute the console command.
98
     */
99
    public function handle()
100
    {
101
        if ($this->option('test')) {
102
            $this->setupTest();
103
        }
104
105
        $this->fire();
106
107
        $this->exportControllers();
108
        $this->exportModels();
109
110
        $this->exportMigrations();
111
        $this->exportSeeds();
112
113
        if (!$this->option('test')) {
114
            $this::call('optimize');
115
        }
116
117
        $this->info('JsonApi demo entities generated successfully.');
118
    }
119
120
    /**
121
     *
122
     */
123 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...
124
    {
125
        foreach ($this->migrations as $key => $value) {
126
            $this->migrations[$key] = $value . $this->testPostfix;
127
        }
128
        foreach ($this->seeds as $key => $value) {
129
            $this->seeds[$key] = $value . $this->testPostfix;
130
        }
131
        foreach ($this->controllers as $key => $value) {
132
            $this->controllers[$key] = $value . $this->testPostfix;
133
        }
134
        foreach ($this->models as $key => $value) {
135
            $this->models[$key] = $value . $this->testPostfix;
136
        }
137
        foreach ($this->jsonapiEntities as $key => $value) {
138
            $this->jsonapiEntities[$key] = $value . $this->testPostfix;
139
        }
140
    }
141
142
    /**
143
     *
144
     */
145
    public function fire()
146
    {
147
        $this->createDirectories();
148
149
        $this->copyJsonApiEntities();
150
    }
151
152
    /**
153
     *
154
     */
155
    protected function createDirectories()
156
    {
157
        if (!is_dir(app_path('JsonApi/Likes'))) {
158
            mkdir(app_path('JsonApi/Likes'), 0755, true);
159
        }
160
        if (!is_dir(app_path('JsonApi/Skills'))) {
161
            mkdir(app_path('JsonApi/Skills'), 0755, true);
162
        }
163
        if (!is_dir(app_path('JsonApi/Teams'))) {
164
            mkdir(app_path('JsonApi/Teams'), 0755, true);
165
        }
166
        if (!is_dir(app_path('JsonApi/Users'))) {
167
            mkdir(app_path('JsonApi/Users'), 0755, true);
168
        }
169
    }
170
171
    /**
172
     *
173
     */
174 View Code Duplication
    protected function copyJsonApiEntities()
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...
175
    {
176
        foreach ($this->jsonapiEntities as $key => $value) {
177
            if (file_exists(app_path('JsonApi/'.$value)) && ! $this->option('force')) {
178
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
179
                    continue;
180
                }
181
            }
182
183
            copy(
184
                base_path('stubs/'.$key),
185
                app_path($value)
186
            );
187
        }
188
    }
189
190
    /**
191
     *
192
     */
193 View Code Duplication
    protected function exportControllers()
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...
194
    {
195
        foreach ($this->controllers as $key => $value) {
196
            if (file_exists(app_path('Http/Controllers/Api/v1/'.$value)) && ! $this->option('force')) {
197
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
198
                    continue;
199
                }
200
            }
201
202
            copy(
203
                base_path('stubs/Controllers/'.$key),
204
                app_path('Http/Controllers/Api/v1/'.$value)
205
            );
206
        }
207
    }
208
209
    /**
210
     *
211
     */
212 View Code Duplication
    protected function exportModels()
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...
213
    {
214
        foreach ($this->models as $key => $value) {
215
            if (file_exists(app_path('Models/'.$value)) && ! $this->option('force')) {
216
                if (! $this->confirm("The [{$value}] model already exists. Do you want to replace it?")) {
217
                    continue;
218
                }
219
            }
220
221
            copy(
222
                base_path('stubs/Models/'.$key),
223
                app_path('Models/'.$value)
224
            );
225
        }
226
    }
227
228
    /**
229
     *
230
     */
231
    protected function exportMigrations()
232
    {
233
        $counter = 0;
234
        foreach ($this->migrations as $key => $value) {
235
            if (file_exists(database_path('migrations/'.$value)) && ! $this->option('force')) {
236
                if (! $this->confirm("The [{$value}] migration already exists. Do you want to replace it?")) {
237
                    continue;
238
                }
239
            }
240
241
            copy(
242
                base_path('stubs/migrations/' . $key),
243
                database_path('migrations/'. date('Y_m_d_Hi') . '0' . $counter++ . '_' . $value)
244
            );
245
        }
246
    }
247
248
    /**
249
     *
250
     */
251 View Code Duplication
    protected function exportSeeds()
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...
252
    {
253
        foreach ($this->seeds as $key => $value) {
254
            if (file_exists(database_path('seeds/'.$value)) && ! $this->option('force')) {
255
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
256
                    continue;
257
                }
258
            }
259
260
            copy(
261
                base_path('stubs/seeds/'.$key),
262
                database_path('seeds/'.$value)
263
            );
264
        }
265
    }
266
267
    /**
268
     * @param $src
269
     * @param $dst
270
     */
271
    protected function recurse_copy($src,$dst)
272
    {
273
        $dir = opendir($src);
274
        @mkdir($dst);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
275
        while (false !== ($file = readdir($dir))) {
276
            if (($file != '.') && ($file != '..')) {
277
                if (is_dir($src . '/' . $file)) {
278
                    $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
279
                } else {
280
                    copy($src . '/' . $file, $dst . '/' . $file);
281
                }
282
            }
283
        }
284
        closedir($dir);
285
    }
286
}
287