Completed
Pull Request — master (#11)
by alexfloppy
02:49
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
                    {--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->info('JsonApi demo entities fake generated successfully.');
82
            return true;
83
            $this->setupFake();
0 ignored issues
show
Unused Code introduced by
$this->setupFake(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
        }
85
86
        $this->fire();
87
88
        $this->exportControllers();
89
        $this->exportModels();
90
91
        $this->exportMigrations();
92
        $this->exportSeeds();
93
94
        $this::call('optimize');
95
96
        $this->info('JsonApi demo entities generated successfully.');
97
    }
98
99
    /**
100
     *  use fake file system for tests
101
     */
102
    public function setupFake()
103
    {
104
        $this->stubs = [
105
            'controllers' => base_path('stubs/Controllers'),
106
            'jsonapi' => base_path('stubs/JsonApi'),
107
            'models' => base_path('stubs/Models'),
108
            'migrations' => base_path('stubs/migrations'),
109
            'seeds' => base_path('stubs/seeds')
110
        ];
111
112
        vfsStreamWrapper::register();
113
    }
114
115
    /**
116
     *
117
     */
118
    public function fire()
119
    {
120
        $this->copyJsonApiEntities();
121
    }
122
123
    /**
124
     *
125
     */
126
    protected function copyJsonApiEntities()
127
    {
128
        $source = 'stubs/JsonApi';
129
        $destination = ($this->option('fake')) ? vfsStream::url('app/JsonApi') : app_path('JsonApi');
130
131
        $this->recurse_copy($source, $destination);
132
    }
133
134
    /**
135
     *
136
     */
137 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...
138
    {
139
        if ($this->option('fake')) {
140
            $path = $this->stubs['controllers'];
141
            $baseDir = new vfsStreamDirectory('app');
142
            vfsStream::copyFromFileSystem($path, $baseDir);
143
            return true;
144
        }
145
146
        foreach ($this->controllers as $key => $value) {
147
            if (file_exists(app_path('Http/Controllers/Api/v1/'.$value)) && ! $this->option('force')) {
148
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
149
                    continue;
150
                }
151
            }
152
153
            copy(
154
                base_path('stubs/Controllers/'.$key),
155
                app_path('Http/Controllers/Api/v1/'.$value)
156
            );
157
        }
158
    }
159
160
    /**
161
     *
162
     */
163 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...
164
    {
165
        if ($this->option('fake')) {
166
            $path = $this->stubs['models'];
167
            $baseDir = new vfsStreamDirectory('app');
168
            vfsStream::copyFromFileSystem($path, $baseDir);
169
            return true;
170
        }
171
172
        foreach ($this->models as $key => $value) {
173
            if (file_exists(app_path('Models/'.$value)) && ! $this->option('force')) {
174
                if (! $this->confirm("The [{$value}] model already exists. Do you want to replace it?")) {
175
                    continue;
176
                }
177
            }
178
179
            copy(
180
                base_path('stubs/Models/'.$key),
181
                app_path('Models/'.$value)
182
            );
183
        }
184
    }
185
186
    /**
187
     *
188
     */
189
    protected function exportMigrations()
190
    {
191
        if ($this->option('fake')) {
192
            $path = $this->stubs['migrations'];
193
            $baseDir = new vfsStreamDirectory('app');
194
            vfsStream::copyFromFileSystem($path, $baseDir);
195
            return true;
196
        }
197
198
        $counter = 0;
199
        foreach ($this->migrations as $key => $value) {
200
            if (file_exists(database_path('migrations/'.$value)) && ! $this->option('force')) {
201
                if (! $this->confirm("The [{$value}] migration already exists. Do you want to replace it?")) {
202
                    continue;
203
                }
204
            }
205
206
            copy(
207
                base_path('stubs/migrations/' . $key),
208
                database_path('migrations/'. date('Y_m_d_Hi') . '0' . $counter++ . '_' . $value)
209
            );
210
        }
211
    }
212
213
    /**
214
     *
215
     */
216 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...
217
    {
218
        if ($this->option('fake')) {
219
            $path = $this->stubs['seeds'];
220
            $baseDir = new vfsStreamDirectory('app');
221
            vfsStream::copyFromFileSystem($path, $baseDir);
222
            return true;
223
        }
224
225
        foreach ($this->seeds as $key => $value) {
226
            if (file_exists(database_path('seeds/'.$value)) && ! $this->option('force')) {
227
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
228
                    continue;
229
                }
230
            }
231
232
            copy(
233
                base_path('stubs/seeds/'.$key),
234
                database_path('seeds/'.$value)
235
            );
236
        }
237
    }
238
239
    /**
240
     * @param $src
241
     * @param $dst
242
     */
243
    protected function recurse_copy($src,$dst)
244
    {
245
        $dir = opendir($src);
246
        @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...
247
        while (false !== ($file = readdir($dir))) {
248
            if (($file != '.') && ($file != '..')) {
249
                if (is_dir($src . '/' . $file)) {
250
                    $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
251
                } else {
252
                    copy($src . '/' . $file, $dst . '/' . $file);
253
                }
254
            }
255
        }
256
        closedir($dir);
257
    }
258
}
259