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

MakeJsonApiDemo::setupTest()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 32
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
                    {--fake  : Make fake directories and files for test}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create JsonApi Demo entities';
31
32
    protected $testPostfix = '-test';
33
34
    protected $migrations = [
35
        'create_likes_table.stub'                   => 'create_likes_table.php',
36
        'create_membership_table.stub'              => 'create_membership_table.php',
37
        'create_skills_table.stub'                  => 'create_skills_table.php',
38
        'create_teams_table.stub'                   => 'create_teams_table.php',
39
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
40
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
41
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
42
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
43
    ];
44
45
    protected $seeds = [
46
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
47
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
48
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
49
    ];
50
51
    protected $controllers = [
52
        'LikesController.stub' => 'LikesController.php',
53
        'SkillsController.stub' => 'SkillsController.php',
54
        'TeamsController.stub' => 'TeamsController.php',
55
        'UsersController.stub' => 'UsersController.php'
56
    ];
57
58
    protected $models = [
59
        'Like.stub' => 'Like.php',
60
        'Skill.stub' => 'Skill.php',
61
        'Team.stub' => 'Team.php'
62
    ];
63
64
    protected $jsonapiEntities = [
65
        'JsonApi/Likes/Hydrator.php' => 'JsonApi/Likes/Hydrator.php',
66
        'JsonApi/Likes/Request.php' => 'JsonApi/Likes/Request.php',
67
        'JsonApi/Likes/Schema.php' => 'JsonApi/Likes/Schema.php',
68
        'JsonApi/Likes/Search.php' => 'JsonApi/Likes/Search.php',
69
        'JsonApi/Likes/Validators.php' => 'JsonApi/Likes/Validators.php',
70
71
        'JsonApi/Skills/Hydrator.php' => 'JsonApi/Skills/Hydrator.php',
72
        'JsonApi/Skills/Request.php' => 'JsonApi/Skills/Request.php',
73
        'JsonApi/Skills/Schema.php' => 'JsonApi/Skills/Schema.php',
74
        'JsonApi/Skills/Search.php' => 'JsonApi/Skills/Search.php',
75
        'JsonApi/Skills/Validators.php' => 'JsonApi/Skills/Validators.php',
76
77
        'JsonApi/Teams/Hydrator.php' => 'JsonApi/Teams/Hydrator.php',
78
        'JsonApi/Teams/Request.php' => 'JsonApi/Teams/Request.php',
79
        'JsonApi/Teams/Schema.php' => 'JsonApi/Teams/Schema.php',
80
        'JsonApi/Teams/Search.php' => 'JsonApi/Teams/Search.php',
81
        'JsonApi/Teams/Validators.php' => 'JsonApi/Teams/Validators.php',
82
83
        'JsonApi/Users/Hydrator.php' => 'JsonApi/Users/Hydrator.php',
84
        'JsonApi/Users/Request.php' => 'JsonApi/Users/Request.php',
85
        'JsonApi/Users/Schema.php' => 'JsonApi/Users/Schema.php',
86
        'JsonApi/Users/Search.php' => 'JsonApi/Users/Search.php',
87
        'JsonApi/Users/Validators.php' => 'JsonApi/Users/Validators.php',
88
    ];
89
90
91
    /**
92
     * Create a new command instance.
93
     *
94
     */
95
    public function __construct()
96
    {
97
        parent::__construct();
98
    }
99
100
    /**
101
     * Execute the console command.
102
     */
103
    public function handle()
104
    {
105
        if ($this->option('test')) {
106
            $this->setupTest();
107
        }
108
109
        if ($this->option('fake')) {
110
            $this->setupFake();
111
        }
112
113
        $this->fire();
114
115
        $this->exportControllers();
116
        $this->exportModels();
117
118
        $this->exportMigrations();
119
        $this->exportSeeds();
120
121
        if (!$this->option('test')) {
122
            $this::call('optimize');
123
        }
124
125
        $this->info('JsonApi demo entities generated successfully.');
126
    }
127
128
    /**
129
     *
130
     */
131 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...
132
    {
133
        foreach ($this->migrations as $key => $value) {
134
            $this->migrations[$key] = $value . $this->testPostfix;
135
        }
136
        foreach ($this->seeds as $key => $value) {
137
            $this->seeds[$key] = $value . $this->testPostfix;
138
        }
139
        foreach ($this->controllers as $key => $value) {
140
            $this->controllers[$key] = $value . $this->testPostfix;
141
        }
142
        foreach ($this->models as $key => $value) {
143
            $this->models[$key] = $value . $this->testPostfix;
144
        }
145
        foreach ($this->jsonapiEntities as $key => $value) {
146
            $this->jsonapiEntities[$key] = $value . $this->testPostfix;
147
        }
148
    }
149
150
    /**
151
     *  use fake file system for tests
152
     */
153
    public function setupFake()
154
    {
155
        $this->stubs = [
0 ignored issues
show
Bug introduced by
The property stubs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
156
            'controllers' => base_path('stubs/Controllers'),
157
            'jsonapi' => base_path('stubs/JsonApi'),
158
            'models' => base_path('stubs/Models'),
159
            'migrations' => base_path('stubs/migrations'),
160
            'seeds' => base_path('stubs/seeds')
161
        ];
162
163
        vfsStreamWrapper::register();
164
    }
165
166
    /**
167
     *
168
     */
169
    public function fire()
170
    {
171
        $this->createDirectories();
172
173
        $this->copyJsonApiEntities();
174
    }
175
176
    /**
177
     *
178
     */
179
    protected function createDirectories()
180
    {
181
        if (!is_dir(app_path('JsonApi/Likes'))) {
182
            mkdir(app_path('JsonApi/Likes'), 0755, true);
183
        }
184
        if (!is_dir(app_path('JsonApi/Skills'))) {
185
            mkdir(app_path('JsonApi/Skills'), 0755, true);
186
        }
187
        if (!is_dir(app_path('JsonApi/Teams'))) {
188
            mkdir(app_path('JsonApi/Teams'), 0755, true);
189
        }
190
        if (!is_dir(app_path('JsonApi/Users'))) {
191
            mkdir(app_path('JsonApi/Users'), 0755, true);
192
        }
193
    }
194
195
    /**
196
     *
197
     */
198
    protected function copyJsonApiEntities()
199
    {
200
        foreach ($this->jsonapiEntities as $key => $value) {
201
            if (file_exists(app_path('JsonApi/'.$value)) && ! $this->option('force')) {
202
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
203
                    continue;
204
                }
205
            }
206
207
            copy(
208
                base_path('stubs/'.$key),
209
                app_path($value)
210
            );
211
        }
212
213
214
//        $source = 'stubs/JsonApi';
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
215
//        $destination = ($this->option('fake')) ? vfsStream::url('app/JsonApi') : app_path('JsonApi');
216
//
217
//        if ($this->option('test')) {
218
//            $destination = app_path('JsonApi-test');
219
//        }
220
//
221
//        $this->recurse_copy($source, $destination);
222
    }
223
224
    /**
225
     *
226
     */
227 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...
228
    {
229
        if ($this->option('fake')) {
230
            $path = $this->stubs['controllers'];
231
            $baseDir = new vfsStreamDirectory('app');
232
            vfsStream::copyFromFileSystem($path, $baseDir);
233
            return true;
234
        }
235
236
        foreach ($this->controllers as $key => $value) {
237
            if (file_exists(app_path('Http/Controllers/Api/v1/'.$value)) && ! $this->option('force')) {
238
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
239
                    continue;
240
                }
241
            }
242
243
            copy(
244
                base_path('stubs/Controllers/'.$key),
245
                app_path('Http/Controllers/Api/v1/'.$value)
246
            );
247
        }
248
    }
249
250
    /**
251
     *
252
     */
253 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...
254
    {
255
        if ($this->option('fake')) {
256
            $path = $this->stubs['models'];
257
            $baseDir = new vfsStreamDirectory('app');
258
            vfsStream::copyFromFileSystem($path, $baseDir);
259
            return true;
260
        }
261
262
        foreach ($this->models as $key => $value) {
263
            if (file_exists(app_path('Models/'.$value)) && ! $this->option('force')) {
264
                if (! $this->confirm("The [{$value}] model already exists. Do you want to replace it?")) {
265
                    continue;
266
                }
267
            }
268
269
            copy(
270
                base_path('stubs/Models/'.$key),
271
                app_path('Models/'.$value)
272
            );
273
        }
274
    }
275
276
    /**
277
     *
278
     */
279
    protected function exportMigrations()
280
    {
281
        if ($this->option('fake')) {
282
            $path = $this->stubs['migrations'];
283
            $baseDir = new vfsStreamDirectory('app');
284
            vfsStream::copyFromFileSystem($path, $baseDir);
285
            return true;
286
        }
287
288
        $counter = 0;
289
        foreach ($this->migrations as $key => $value) {
290
            if (file_exists(database_path('migrations/'.$value)) && ! $this->option('force')) {
291
                if (! $this->confirm("The [{$value}] migration already exists. Do you want to replace it?")) {
292
                    continue;
293
                }
294
            }
295
296
            copy(
297
                base_path('stubs/migrations/' . $key),
298
                database_path('migrations/'. date('Y_m_d_Hi') . '0' . $counter++ . '_' . $value)
299
            );
300
        }
301
    }
302
303
    /**
304
     *
305
     */
306 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...
307
    {
308
        if ($this->option('fake')) {
309
            $path = $this->stubs['seeds'];
310
            $baseDir = new vfsStreamDirectory('app');
311
            vfsStream::copyFromFileSystem($path, $baseDir);
312
            return true;
313
        }
314
315
        foreach ($this->seeds as $key => $value) {
316
            if (file_exists(database_path('seeds/'.$value)) && ! $this->option('force')) {
317
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
318
                    continue;
319
                }
320
            }
321
322
            copy(
323
                base_path('stubs/seeds/'.$key),
324
                database_path('seeds/'.$value)
325
            );
326
        }
327
    }
328
329
    /**
330
     * @param $src
331
     * @param $dst
332
     */
333
    protected function recurse_copy($src,$dst)
334
    {
335
        $dir = opendir($src);
336
        @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...
337
        while (false !== ($file = readdir($dir))) {
338
            if (($file != '.') && ($file != '..')) {
339
                if (is_dir($src . '/' . $file)) {
340
                    $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
341
                } else {
342
                    copy($src . '/' . $file, $dst . '/' . $file);
343
                }
344
            }
345
        }
346
        closedir($dir);
347
    }
348
}
349