Completed
Pull Request — master (#11)
by alexfloppy
03:17
created

MakeJsonApiDemo::setupFake()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
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
    /**
65
     * @var array
66
     */
67
    protected $stubs = [];
68
69
    /**
70
     * Create a new command instance.
71
     *
72
     */
73
    public function __construct()
74
    {
75
        parent::__construct();
76
    }
77
78
    /**
79
     * Execute the console command.
80
     */
81
    public function handle()
82
    {
83
        if ($this->option('test')) {
84
            $this->setupTest();
85
        }
86
87
        if ($this->option('fake')) {
88
            $this->setupFake();
89
        }
90
91
        $this->fire();
92
93
        $this->exportControllers();
94
        $this->exportModels();
95
96
        $this->exportMigrations();
97
        $this->exportSeeds();
98
99
        $this::call('optimize');
100
101
        $this->info('JsonApi demo entities generated successfully.');
102
    }
103
104 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...
105
    {
106
        foreach ($this->migrations as $key => $value) {
107
            $this->migrations[$key] = $value . $this->testPostfix;
108
        }
109
        foreach ($this->seeds as $key => $value) {
110
            $this->seeds[$key] = $value . $this->testPostfix;
111
        }
112
        foreach ($this->controllers as $key => $value) {
113
            $this->controllers[$key] = $value . $this->testPostfix;
114
        }
115
        foreach ($this->models as $key => $value) {
116
            $this->models[$key] = $value . $this->testPostfix;
117
        }
118
    }
119
120
    /**
121
     *  use fake file system for tests
122
     */
123
    public function setupFake()
124
    {
125
        $this->stubs = [
126
            'controllers' => base_path('stubs/Controllers'),
127
            'jsonapi' => base_path('stubs/JsonApi'),
128
            'models' => base_path('stubs/Models'),
129
            'migrations' => base_path('stubs/migrations'),
130
            'seeds' => base_path('stubs/seeds')
131
        ];
132
133
        vfsStreamWrapper::register();
134
    }
135
136
    /**
137
     *
138
     */
139
    public function fire()
140
    {
141
        $this->copyJsonApiEntities();
142
    }
143
144
    /**
145
     *
146
     */
147
    protected function copyJsonApiEntities()
148
    {
149
        $source = 'stubs/JsonApi';
150
        $destination = ($this->option('fake')) ? vfsStream::url('app/JsonApi') : app_path('JsonApi');
151
152
        if ($this->option('test')) {
153
            $destination = app_path('JsonApi-test');
154
        }
155
156
        $this->recurse_copy($source, $destination);
157
    }
158
159
    /**
160
     *
161
     */
162 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...
163
    {
164
        if ($this->option('fake')) {
165
            $path = $this->stubs['controllers'];
166
            $baseDir = new vfsStreamDirectory('app');
167
            vfsStream::copyFromFileSystem($path, $baseDir);
168
            return true;
169
        }
170
171
        foreach ($this->controllers as $key => $value) {
172
            if (file_exists(app_path('Http/Controllers/Api/v1/'.$value)) && ! $this->option('force')) {
173
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
174
                    continue;
175
                }
176
            }
177
178
            copy(
179
                base_path('stubs/Controllers/'.$key),
180
                app_path('Http/Controllers/Api/v1/'.$value)
181
            );
182
        }
183
    }
184
185
    /**
186
     *
187
     */
188 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...
189
    {
190
        if ($this->option('fake')) {
191
            $path = $this->stubs['models'];
192
            $baseDir = new vfsStreamDirectory('app');
193
            vfsStream::copyFromFileSystem($path, $baseDir);
194
            return true;
195
        }
196
197
        foreach ($this->models as $key => $value) {
198
            if (file_exists(app_path('Models/'.$value)) && ! $this->option('force')) {
199
                if (! $this->confirm("The [{$value}] model already exists. Do you want to replace it?")) {
200
                    continue;
201
                }
202
            }
203
204
            copy(
205
                base_path('stubs/Models/'.$key),
206
                app_path('Models/'.$value)
207
            );
208
        }
209
    }
210
211
    /**
212
     *
213
     */
214
    protected function exportMigrations()
215
    {
216
        if ($this->option('fake')) {
217
            $path = $this->stubs['migrations'];
218
            $baseDir = new vfsStreamDirectory('app');
219
            vfsStream::copyFromFileSystem($path, $baseDir);
220
            return true;
221
        }
222
223
        $counter = 0;
224
        foreach ($this->migrations as $key => $value) {
225
            if (file_exists(database_path('migrations/'.$value)) && ! $this->option('force')) {
226
                if (! $this->confirm("The [{$value}] migration already exists. Do you want to replace it?")) {
227
                    continue;
228
                }
229
            }
230
231
            copy(
232
                base_path('stubs/migrations/' . $key),
233
                database_path('migrations/'. date('Y_m_d_Hi') . '0' . $counter++ . '_' . $value)
234
            );
235
        }
236
    }
237
238
    /**
239
     *
240
     */
241 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...
242
    {
243
        if ($this->option('fake')) {
244
            $path = $this->stubs['seeds'];
245
            $baseDir = new vfsStreamDirectory('app');
246
            vfsStream::copyFromFileSystem($path, $baseDir);
247
            return true;
248
        }
249
250
        foreach ($this->seeds as $key => $value) {
251
            if (file_exists(database_path('seeds/'.$value)) && ! $this->option('force')) {
252
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
253
                    continue;
254
                }
255
            }
256
257
            copy(
258
                base_path('stubs/seeds/'.$key),
259
                database_path('seeds/'.$value)
260
            );
261
        }
262
    }
263
264
    /**
265
     * @param $src
266
     * @param $dst
267
     */
268
    protected function recurse_copy($src,$dst)
269
    {
270
        $dir = opendir($src);
271
        @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...
272
        while (false !== ($file = readdir($dir))) {
273
            if (($file != '.') && ($file != '..')) {
274
                if (is_dir($src . '/' . $file)) {
275
                    $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
276
                } else {
277
                    copy($src . '/' . $file, $dst . '/' . $file);
278
                }
279
            }
280
        }
281
        closedir($dir);
282
    }
283
}
284