Completed
Pull Request — master (#11)
by alexfloppy
02:41
created

MakeJsonApiDemo::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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