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

MakeJsonApiDemo::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
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
                    {--fake  : Make fake directories and files for test}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Create JsonApi Demo entities';
30
31
    protected $migrations = [
32
        'create_likes_table.stub'                   => 'create_likes_table.php',
33
        'create_membership_table.stub'              => 'create_membership_table.php',
34
        'create_skills_table.stub'                  => 'create_skills_table.php',
35
        'create_teams_table.stub'                   => 'create_teams_table.php',
36
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
37
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
38
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
39
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
40
    ];
41
42
    protected $seeds = [
43
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
44
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
45
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
46
    ];
47
48
    protected $controllers = [
49
        'LikesController.stub' => 'LikesController.php',
50
        'SkillsController.stub' => 'SkillsController.php',
51
        'TeamsController.stub' => 'TeamsController.php',
52
        'UsersController.stub' => 'UsersController.php'
53
    ];
54
55
    protected $models = [
56
        'Like.stub' => 'Like.php',
57
        'Skill.stub' => 'Skill.php',
58
        'Team.stub' => 'Team.php'
59
    ];
60
61
    /**
62
     * @var array
63
     */
64
    protected $stubs = [];
65
66
    /**
67
     * Create a new command instance.
68
     *
69
     */
70
    public function __construct()
71
    {
72
        parent::__construct();
73
    }
74
75
    /**
76
     * Execute the console command.
77
     */
78
    public function handle()
79
    {
80
        if ($this->option('fake')) {
81
            $this->setupFake();
82
        }
83
84
        $this->fire();
85
86
        $this->exportControllers();
87
        $this->exportModels();
88
89
        $this->exportMigrations();
90
        $this->exportSeeds();
91
92
        $this::call('optimize');
93
94
        $this->info('JsonApi demo entities generated successfully.');
95
    }
96
97
    /**
98
     *  use fake file system for tests
99
     */
100
    public function setupFake()
101
    {
102
        $this->stubs = [
103
            'controllers' => base_path('stubs/Controllers'),
104
            'jsonapi' => base_path('stubs/JsonApi'),
105
            'models' => base_path('stubs/Models'),
106
            'migrations' => base_path('stubs/migrations'),
107
            'seeds' => base_path('stubs/seeds')
108
        ];
109
110
        vfsStreamWrapper::register();
111
    }
112
113
    /**
114
     *
115
     */
116
    public function fire()
117
    {
118
        $this->copyJsonApiEntities();
119
    }
120
121
    /**
122
     *
123
     */
124
    protected function copyJsonApiEntities()
125
    {
126
        $source = 'stubs/JsonApi';
127
        $destination = ($this->option('fake')) ? vfsStream::url('app/JsonApi') : app_path('JsonApi');
128
129
        $this->recurse_copy($source, $destination);
130
    }
131
132
    /**
133
     *
134
     */
135 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...
136
    {
137
        if ($this->option('fake')) {
138
            $path = $this->stubs['controllers'];
139
            $baseDir = new vfsStreamDirectory('app');
140
            vfsStream::copyFromFileSystem($path, $baseDir);
141
            return true;
142
        }
143
144
        foreach ($this->controllers as $key => $value) {
145
            if (file_exists(app_path('Http/Controllers/Api/v1/'.$value)) && ! $this->option('force')) {
146
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
147
                    continue;
148
                }
149
            }
150
151
            copy(
152
                base_path('stubs/Controllers/'.$key),
153
                app_path('Http/Controllers/Api/v1/'.$value)
154
            );
155
        }
156
    }
157
158
    /**
159
     *
160
     */
161 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...
162
    {
163
        if ($this->option('fake')) {
164
            $path = $this->stubs['models'];
165
            $baseDir = new vfsStreamDirectory('app');
166
            vfsStream::copyFromFileSystem($path, $baseDir);
167
            return true;
168
        }
169
170
        foreach ($this->models as $key => $value) {
171
            if (file_exists(app_path('Models/'.$value)) && ! $this->option('force')) {
172
                if (! $this->confirm("The [{$value}] model already exists. Do you want to replace it?")) {
173
                    continue;
174
                }
175
            }
176
177
            copy(
178
                base_path('stubs/Models/'.$key),
179
                app_path('Models/'.$value)
180
            );
181
        }
182
    }
183
184
    /**
185
     *
186
     */
187
    protected function exportMigrations()
188
    {
189
        if ($this->option('fake')) {
190
            $path = $this->stubs['migrations'];
191
            $baseDir = new vfsStreamDirectory('app');
192
            vfsStream::copyFromFileSystem($path, $baseDir);
193
            return true;
194
        }
195
196
        $counter = 0;
197
        foreach ($this->migrations as $key => $value) {
198
            if (file_exists(database_path('migrations/'.$value)) && ! $this->option('force')) {
199
                if (! $this->confirm("The [{$value}] migration already exists. Do you want to replace it?")) {
200
                    continue;
201
                }
202
            }
203
204
            copy(
205
                base_path('stubs/migrations/' . $key),
206
                database_path('migrations/'. date('Y_m_d_Hi') . '0' . $counter++ . '_' . $value)
207
            );
208
        }
209
    }
210
211
    /**
212
     *
213
     */
214 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...
215
    {
216
        if ($this->option('fake')) {
217
            $path = $this->stubs['seeds'];
218
            $baseDir = new vfsStreamDirectory('app');
219
            vfsStream::copyFromFileSystem($path, $baseDir);
220
            return true;
221
        }
222
223
        foreach ($this->seeds as $key => $value) {
224
            if (file_exists(database_path('seeds/'.$value)) && ! $this->option('force')) {
225
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
226
                    continue;
227
                }
228
            }
229
230
            copy(
231
                base_path('stubs/seeds/'.$key),
232
                database_path('seeds/'.$value)
233
            );
234
        }
235
    }
236
237
    /**
238
     * @param $src
239
     * @param $dst
240
     */
241
    protected function recurse_copy($src,$dst)
242
    {
243
        $dir = opendir($src);
244
        @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...
245
        while (false !== ($file = readdir($dir))) {
246
            if (($file != '.') && ($file != '..')) {
247
                if (is_dir($src . '/' . $file)) {
248
                    $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
249
                } else {
250
                    copy($src . '/' . $file, $dst . '/' . $file);
251
                }
252
            }
253
        }
254
        closedir($dir);
255
    }
256
}
257