Test Setup Failed
Push — master ( 22888c...730ea3 )
by alexfloppy
01:21
created

MakeJsonApiDemo::exportMigrations()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class MakeJsonApiDemo extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'make:demo
17
                    {--force : Overwrite existing files by default}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create JsonApi Demo entities';
25
26
    protected $migrations = [
27
        'create_likes_table.stub'                   => 'create_likes_table.php',
28
        'create_membership_table.stub'              => 'create_membership_table.php',
29
        'create_skills_table.stub'                  => 'create_skills_table.php',
30
        'create_teams_table.stub'                   => 'create_teams_table.php',
31
        'add_foreign_keys_to_likes_table.stub'      => 'add_foreign_keys_to_likes_table.php',
32
        'add_foreign_keys_to_membership_table.stub' => 'add_foreign_keys_to_membership_table.php',
33
        'add_foreign_keys_to_skills_table.stub'     => 'add_foreign_keys_to_skills_table.php',
34
        'add_foreign_keys_to_teams_table.stub'      => 'add_foreign_keys_to_teams_table.php'
35
    ];
36
37
    protected $seeds = [
38
        'TeamsTableSeeder.stub'     => 'TeamsTableSeeder.php',
39
        'TeamUsersTableSeeder.stub' => 'TeamUsersTableSeeder.php',
40
        'JsonApiSeeder.stub' => 'JsonApiSeeder.php'
41
    ];
42
43
    protected $controllers = [
44
        'LikesController.stub' => 'LikesController.php',
45
        'SkillsController.stub' => 'SkillsController.php',
46
        'TeamsController.stub' => 'TeamsController.php',
47
        'UsersController.stub' => 'UsersController.php'
48
    ];
49
50
    protected $models = [
51
        'Like.stub' => 'Like.php',
52
        'Skill.stub' => 'Skill.php',
53
        'Team.stub' => 'Team.php'
54
    ];
55
56
    /**
57
     * Create a new command instance.
58
     *
59
     */
60
    public function __construct()
61
    {
62
        parent::__construct();
63
    }
64
65
    /**
66
     * Execute the console command.
67
     *
68
     * @return mixed
69
     */
70
    public function handle()
71
    {
72
        $this->fire();
73
        $this->exportControllers();
74
        $this->exportModels();
75
76
        $this->exportMigrations();
77
        $this->exportSeeds();
78
79
        $this::call('optimize');
80
81
        $this->info('JsonApi demo entities generated successfully.');
82
    }
83
84
    /**
85
     *
86
     */
87
    public function fire()
88
    {
89
        $this->copyJsonApiEntities();
90
    }
91
92
    /**
93
     *
94
     */
95
    protected function copyJsonApiEntities()
96
    {
97
        $this->recurse_copy(('stubs/JsonApi'),app_path('JsonApi'));
98
    }
99
100
    /**
101
     *
102
     */
103 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...
104
    {
105
        foreach ($this->controllers as $key => $value) {
106
            if (file_exists(app_path('Http/Controllers/Api/v1/'.$value)) && ! $this->option('force')) {
107
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
108
                    continue;
109
                }
110
            }
111
112
            copy(
113
                base_path('stubs/Controllers/'.$key),
114
                app_path('Http/Controllers/Api/v1/'.$value)
115
            );
116
        }
117
    }
118
119
    /**
120
     *
121
     */
122 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...
123
    {
124
        foreach ($this->models as $key => $value) {
125
            if (file_exists(app_path('Models/'.$value)) && ! $this->option('force')) {
126
                if (! $this->confirm("The [{$value}] model already exists. Do you want to replace it?")) {
127
                    continue;
128
                }
129
            }
130
131
            copy(
132
                base_path('stubs/Models/'.$key),
133
                app_path('Models/'.$value)
134
            );
135
        }
136
    }
137
138
    /**
139
     *
140
     */
141
    protected function exportMigrations()
142
    {
143
        $counter = 0;
144
        foreach ($this->migrations as $key => $value) {
145
            if (file_exists(database_path('migrations/'.$value)) && ! $this->option('force')) {
146
                if (! $this->confirm("The [{$value}] migration already exists. Do you want to replace it?")) {
147
                    continue;
148
                }
149
            }
150
151
            copy(
152
                base_path('stubs/migrations/' . $key),
153
                database_path('migrations/'. date('Y_m_d_Hi') . '0' . $counter++ . '_' . $value)
154
            );
155
        }
156
    }
157
158
    /**
159
     *
160
     */
161 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...
162
    {
163
        foreach ($this->seeds as $key => $value) {
164
            if (file_exists(database_path('seeds/'.$value)) && ! $this->option('force')) {
165
                if (! $this->confirm("The [{$value}] already exists. Do you want to replace it?")) {
166
                    continue;
167
                }
168
            }
169
170
            copy(
171
                base_path('stubs/seeds/'.$key),
172
                database_path('seeds/'.$value)
173
            );
174
        }
175
    }
176
177
    /**
178
     * @param $src
179
     * @param $dst
180
     */
181
    protected function recurse_copy($src,$dst)
182
    {
183
        $dir = opendir($src);
184
        @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...
185
        while (false !== ($file = readdir($dir))) {
186
            if (($file != '.') && ($file != '..')) {
187
                if (is_dir($src . '/' . $file)) {
188
                    $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
189
                } else {
190
                    copy($src . '/' . $file, $dst . '/' . $file);
191
                }
192
            }
193
        }
194
        closedir($dir);
195
    }
196
}
197