| Conditions | 8 |
| Paths | 12 |
| Total Lines | 76 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 36 | public function handle() |
||
| 37 | { |
||
| 38 | \ini_set('memory_limit', '256M'); |
||
| 39 | |||
| 40 | $tableName = $this->argument('table'); |
||
| 41 | $imageUrlColumn = $this->argument('urlcolumn'); |
||
| 42 | $modelIdColumn = $this->argument('idcolumn'); |
||
| 43 | $ordercolumn = $this->argument('ordercolumn'); |
||
| 44 | $model = $this->argument('linkedmodel'); |
||
| 45 | $unreachable = 0; |
||
| 46 | $files = 0; |
||
| 47 | |||
| 48 | if($ordercolumn){ |
||
| 49 | $results = DB::table($tableName)->select($imageUrlColumn, $modelIdColumn, $ordercolumn)->get(); |
||
| 50 | }else{ |
||
| 51 | $results = DB::table($tableName)->select($imageUrlColumn, $modelIdColumn)->orderBy($modelIdColumn)->get(); |
||
| 52 | } |
||
| 53 | |||
| 54 | $orderedResults = []; |
||
| 55 | $results->each(function($result) use($modelIdColumn, $results, &$orderedResults, $imageUrlColumn){ |
||
| 56 | $orderedResults[$result->$modelIdColumn][] = $result->$imageUrlColumn; |
||
| 57 | }); |
||
| 58 | |||
| 59 | $orderedResults = collect($orderedResults); |
||
| 60 | |||
| 61 | if($this->option('reset')){ |
||
| 62 | $this->info('Resetting the asserts on the models'); |
||
| 63 | $resetbar = $this->output->createProgressBar(count($orderedResults)); |
||
| 64 | |||
| 65 | $orderedResults->each(function($entry, $key) use($model, $resetbar){ |
||
| 66 | optional($model::find($key))->deleteAllAssets(); |
||
| 67 | $resetbar->advance(); |
||
| 68 | }); |
||
| 69 | |||
| 70 | $resetbar->finish(); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | $bar = $this->output->createProgressBar(count($results)); |
||
| 75 | |||
| 76 | $this->info("\n" . 'Migrating images.'); |
||
| 77 | foreach($orderedResults as $key => $result) |
||
| 78 | { |
||
| 79 | $persistedModel = $model::find($key); |
||
| 80 | if(!$persistedModel){ |
||
| 81 | continue; |
||
| 82 | } |
||
| 83 | |||
| 84 | foreach($result as $line){ |
||
| 85 | $bar->advance(); |
||
| 86 | |||
| 87 | try{ |
||
| 88 | $asset = AssetUploader::uploadFromUrl(public_path($line)); |
||
| 89 | }catch(UnreachableUrl $ex){ |
||
| 90 | // increment the amount of unreachable files counter |
||
| 91 | $unreachable++; |
||
| 92 | |||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | $asset->attachToModel($persistedModel); |
||
| 97 | |||
| 98 | if($this->option('force')){ |
||
| 99 | unlink(public_path($line)); |
||
| 100 | } |
||
| 101 | |||
| 102 | // increment the amount of files migrated counter |
||
| 103 | $files++; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $bar->finish(); |
||
| 108 | |||
| 109 | $this->info('Migrating done.'); |
||
| 110 | $this->info('Migrated '. $files . ' files.'); |
||
| 111 | $this->info('Couldn\'t reach '. $unreachable . ' files.'); |
||
| 112 | } |
||
| 114 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.