Total Complexity | 40 |
Total Lines | 302 |
Duplicated Lines | 0 % |
Changes | 16 | ||
Bugs | 0 | Features | 0 |
Complex classes like Rebuild often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Rebuild, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Rebuild extends Command |
||
9 | { |
||
10 | /** |
||
11 | * The name and signature of the console command. |
||
12 | * |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $signature = 'rebuild'; |
||
16 | |||
17 | /** |
||
18 | * The console command description. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $description = 'Rebuild the app from scratch'; |
||
23 | |||
24 | /** |
||
25 | * Create a new command instance. |
||
26 | * |
||
27 | * @return void |
||
28 | */ |
||
29 | public function __construct(Composer $composer) |
||
30 | { |
||
31 | parent::__construct(); |
||
32 | |||
33 | $this->composer = $composer; |
||
|
|||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Execute the console command. |
||
38 | * |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function handle() |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Command operation sequence |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | protected function rebuildSequence() |
||
60 | { |
||
61 | if ($this->confirm('You are about to rebuild the app from scratch. Continue?')) { |
||
62 | $this->setToMaintenanceMode(); |
||
63 | $this->rebuildDatabaseSchema(); |
||
64 | $this->seedInitialData(); |
||
65 | $this->seedDummyData(); |
||
66 | $this->seedExampleData(); |
||
67 | $this->clearCache(); |
||
68 | $this->clearConfig(); |
||
69 | $this->clearRoute(); |
||
70 | $this->clearView(); |
||
71 | $this->flushExpiredPasswordResetToken(); |
||
72 | $this->clearCompiledClasses(); |
||
73 | $this->rediscoverPackages(); |
||
74 | $this->createSymbolicLink(); |
||
75 | $this->runSelfDiagnosis(); |
||
76 | $this->wakeUpFromMaintenanceMode(); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Rebuild database schema |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | protected function setToMaintenanceMode() |
||
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Rebuild database schema |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | protected function rebuildDatabaseSchema() |
||
99 | { |
||
100 | if (config('rebuild.should_rebuild_database_schema')) { |
||
101 | $this->call('migrate:fresh', ['--force' => true]); |
||
102 | $this->info('Rebuilding database schema is done.'); |
||
103 | $this->line(''); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Seeding initial data |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | protected function seedInitialData() |
||
113 | { |
||
114 | if (config('rebuild.should_seed_initial_data')) { |
||
115 | $this->composer->dumpAutoloads(); |
||
116 | |||
117 | $this->call('db:seed', ['--force' => true]); |
||
118 | $this->info('Seeding initial data is done.'); |
||
119 | $this->line(''); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Seeding dummy data |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | protected function seedDummyData() |
||
129 | { |
||
130 | if (config('rebuild.dummy.should_seed')) { |
||
131 | if ($this->confirm('Install dummy data?')) { |
||
132 | try { |
||
133 | $this->call('db:seed', [ |
||
134 | '--class' => config('rebuild.dummy.seeder_name'), |
||
135 | '--force' => true, |
||
136 | ]); |
||
137 | } catch (\Exception $e) { |
||
138 | $this->error($e->getMessage()); |
||
139 | $this->info('Seeding dummy data is aborted.'); |
||
140 | $this->line(''); |
||
141 | |||
142 | return true; |
||
143 | } |
||
144 | |||
145 | $this->info('Seeding dummy data is done.'); |
||
146 | $this->line(''); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | protected function seedExampleData() |
||
152 | { |
||
153 | if (config('rebuild.example.should_seed')) { |
||
154 | if ($this->confirm('Install example data?')) { |
||
155 | try { |
||
156 | $this->call('db:seed', [ |
||
157 | '--class' => config('rebuild.example.seeder_name'), |
||
158 | '--force' => true, |
||
159 | ]); |
||
160 | } catch (\Exception $e) { |
||
161 | $this->error($e->getMessage()); |
||
162 | $this->info('Seeding example data is aborted.'); |
||
163 | $this->line(''); |
||
164 | |||
165 | return true; |
||
166 | } |
||
167 | |||
168 | $this->info('Seeding example data is done.'); |
||
169 | $this->line(''); |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Clear cache |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | protected function clearCache() |
||
180 | { |
||
181 | if (config('rebuild.should_clear_cache')) { |
||
182 | $this->line('Run artisan \'cache:clear\' command:'); |
||
183 | $this->call('cache:clear'); |
||
184 | $this->line(''); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Clear config |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | protected function clearConfig() |
||
194 | { |
||
195 | if (config('rebuild.should_celar_config')) { |
||
196 | $this->line('Run artisan \'config:clear\' command:'); |
||
197 | $this->call('config:clear'); |
||
198 | $this->line(''); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Clear route |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function clearRoute() |
||
208 | { |
||
209 | if (config('rebuild.should_clear_route')) { |
||
210 | $this->line('Run artisan \'route:clear\' command:'); |
||
211 | $this->call('route:clear'); |
||
212 | $this->line(''); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Clear view |
||
218 | * |
||
219 | * @return void |
||
220 | */ |
||
221 | protected function clearView() |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Flush expired password reset token |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | protected function flushExpiredPasswordResetToken() |
||
236 | { |
||
237 | if (config('rebuild.should_flush_expired_password_reset_token')) { |
||
238 | $this->line('Run artisan \'auth:clear-resets\' command:'); |
||
239 | $this->call('auth:clear-resets'); |
||
240 | $this->line(''); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Clear compiled classes |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | protected function clearCompiledClasses() |
||
250 | { |
||
251 | if (config('rebuild.should_clear_compiled_classes')) { |
||
252 | $this->line('Run artisan \'clear-compiled\' command:'); |
||
253 | $this->call('clear-compiled'); |
||
254 | $this->line(''); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Rebuild packages manifest cache |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | protected function rediscoverPackages() |
||
264 | { |
||
265 | if (config('rebuild.should_rediscover_packages')) { |
||
266 | $this->line('Run artisan \'package:discover\' command:'); |
||
267 | $this->call('package:discover'); |
||
268 | $this->line(''); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Create symbolic link |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | protected function createSymbolicLink() |
||
278 | { |
||
279 | if (config('rebuild.should_create_symbolic_link')) { |
||
280 | $this->line('Run artisan \'storage:link\' command:'); |
||
281 | $this->call('storage:link'); |
||
282 | $this->line(''); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Run BeyondCode's Laravel Self-Diagnosis command |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | protected function runSelfDiagnosis() |
||
297 | } |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Rebuild database schema |
||
302 | * |
||
303 | * @return void |
||
304 | */ |
||
305 | protected function wakeUpFromMaintenanceMode() |
||
306 | { |
||
310 | } |
||
311 | } |
||
313 |