| Total Complexity | 44 |
| Total Lines | 205 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 4 | Features | 0 |
Complex classes like UploadMedia 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 UploadMedia, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class UploadMedia |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Upload from base64encoded files, usually |
||
| 19 | * coming from slim upload component |
||
| 20 | * |
||
| 21 | * @param HasAsset $model |
||
| 22 | * @param array $files_by_type |
||
| 23 | * @param array $files_order_by_type |
||
| 24 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 25 | */ |
||
| 26 | public function fromUploadComponent(HasAsset $model, array $files_by_type, array $files_order_by_type) |
||
| 27 | { |
||
| 28 | ini_set('memory_limit', '256M'); |
||
| 29 | $files_by_type = $this->sanitizeFilesParameter($files_by_type); |
||
| 30 | $files_order_by_type = $this->sanitizeFilesOrderParameter($files_order_by_type); |
||
| 31 | $this->validateParameters($files_by_type, $files_order_by_type); |
||
| 32 | |||
| 33 | // When no files are uploaded, we still would like to sort our assets duh |
||
| 34 | if (empty($files_by_type)) { |
||
| 35 | foreach ($files_order_by_type as $type => $files) { |
||
| 36 | app(SortAssets::class)->handle($model, $type, $files); |
||
| 37 | } |
||
| 38 | |||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | foreach ($files_by_type as $type => $files_by_locale) { |
||
| 43 | foreach ($files_by_locale as $locale => $files) { |
||
| 44 | $this->validateFileUploads($files); |
||
| 45 | |||
| 46 | $fileIdsCollection = $files_order_by_type[$type] ?? []; |
||
| 47 | |||
| 48 | $this->addFiles($model, $type, $files, $fileIdsCollection, $locale); |
||
| 49 | $this->replaceFiles($model, $files); |
||
| 50 | $this->removeFiles($model, $files); |
||
| 51 | } |
||
| 52 | app(SortAssets::class)->handle($model, $type, $fileIdsCollection ?? []); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | private function addFiles(HasAsset $model, string $type, array $files, array &$files_order, string $locale = null) |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param HasAsset $model |
||
| 73 | * @param array $files |
||
| 74 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
| 75 | */ |
||
| 76 | private function replaceFiles(HasAsset $model, array $files) |
||
| 77 | { |
||
| 78 | if (!$this->actionExists($files, 'replace')) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | foreach ($files['replace'] as $id => $file) { |
||
| 83 | if (!$file) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | $asset = AssetUploader::uploadFromBase64(json_decode($file)->output->image, json_decode($file)->output->name); |
||
| 88 | app(ReplaceAsset::class)->handle($model, $id, $asset->id); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param HasAsset $model |
||
| 94 | * @param array $files |
||
| 95 | */ |
||
| 96 | private function removeFiles(HasAsset $model, array $files) |
||
| 97 | { |
||
| 98 | if (!$this->actionExists($files, 'delete')) { |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | app(DetachAsset::class)->detach($model, $files['delete']); |
||
| 103 | } |
||
| 104 | |||
| 105 | private function actionExists(array $files, string $action) |
||
| 108 | } |
||
| 109 | |||
| 110 | private function addFile(HasAsset $model, string $type, $file, array &$files_order, $locale = null) |
||
| 111 | { |
||
| 112 | if (isset(json_decode($file)->output)) { |
||
| 113 | $image_name = json_decode($file)->output->name; |
||
| 114 | $asset = app(AddAsset::class)->add($model, json_decode($file)->output->image, $type, $locale, $this->sluggifyFilename($image_name)); |
||
|
|
|||
| 115 | } else { |
||
| 116 | if ($file instanceof UploadedFile) { |
||
| 117 | $image_name = $file->getClientOriginalName(); |
||
| 118 | $asset = app(AddAsset::class)->add($model, $file, $type, $locale, $this->sluggifyFilename($image_name)); |
||
| 119 | |||
| 120 | // New files are passed with their filename (instead of their id) |
||
| 121 | // For new files we will replace the filename with the id. |
||
| 122 | if (false !== ($key = array_search($image_name, $files_order))) { |
||
| 123 | $files_order[$key] = (string) $asset->id; |
||
| 124 | } |
||
| 125 | } else { |
||
| 126 | $file = Asset::findOrFail($file); |
||
| 127 | $asset = app(AddAsset::class)->add($model, $file, $type, $locale); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $filename |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | private function sluggifyFilename($filename): string |
||
| 137 | { |
||
| 138 | $extension = substr($filename, strrpos($filename, '.') + 1); |
||
| 139 | $filename = substr($filename, 0, strrpos($filename, '.')); |
||
| 140 | $filename = Str::slug($filename) . '.' . $extension; |
||
| 141 | |||
| 142 | return $filename; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param $files |
||
| 147 | * @throws FileTooBigException |
||
| 148 | */ |
||
| 149 | private function validateFileUploads($files): void |
||
| 150 | { |
||
| 151 | foreach ($files as $_files) { |
||
| 152 | foreach ($_files as $file) { |
||
| 153 | if ($file instanceof UploadedFile && !$file->isValid()) { |
||
| 154 | if ($file->getError() == UPLOAD_ERR_INI_SIZE) { |
||
| 155 | throw new FileTooBigException( |
||
| 156 | 'Cannot upload file because it exceeded the allowed upload_max_filesize: upload_max_filesize is smaller than post size. ' . |
||
| 157 | 'upload_max_filesize: ' . (int)ini_get('upload_max_filesize') . 'MB, ' . |
||
| 158 | 'post_max_size: ' . (int)(ini_get('post_max_size')) . 'MB' |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | private function validateParameters(array $files_by_type, array $files_order_by_type) |
||
| 167 | { |
||
| 168 | $actions = ['new', 'replace', 'delete']; |
||
| 169 | foreach ($files_by_type as $type => $files) { |
||
| 170 | foreach ($files as $locale => $_files) { |
||
| 171 | if (!in_array($locale, config('translatable.locales'))) { |
||
| 172 | throw new \InvalidArgumentException('Corrupt file payload. key is expected to be a valid locale [' . implode(',', config('translatable.locales', [])). ']. Instead [' . $locale . '] is given.'); |
||
| 173 | } |
||
| 174 | |||
| 175 | if (!is_array($_files)) { |
||
| 176 | throw new \InvalidArgumentException('A valid files entry should be an array of files, key with either [new, replace or delete]. Instead a ' . gettype($_files) . ' is given.'); |
||
| 177 | } |
||
| 178 | |||
| 179 | foreach ($_files as $action => $file) { |
||
| 180 | if (!in_array($action, $actions)) { |
||
| 181 | throw new \InvalidArgumentException('A valid files entry should have a key of either ['.implode(',', $actions).']. Instead ' . $action . ' is given.'); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | private function sanitizeFilesParameter(array $files_by_type): array |
||
| 207 | } |
||
| 208 | |||
| 209 | private function sanitizeFilesOrderParameter(array $files_order_by_locale): array |
||
| 220 | } |
||
| 221 | } |
||
| 222 |