| Total Complexity | 44 |
| Total Lines | 207 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 2 | 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) |
||
| 106 | { |
||
| 107 | return (isset($files[$action]) && is_array($files[$action]) && !empty($files[$action])); |
||
| 108 | } |
||
| 109 | |||
| 110 | private function addFile(HasAsset $model, string $type, $file, array &$files_order, $locale = null) |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param $filename |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | private function sluggifyFilename($filename): string |
||
| 139 | { |
||
| 140 | $extension = substr($filename, strrpos($filename, '.') + 1); |
||
| 141 | $filename = substr($filename, 0, strrpos($filename, '.')); |
||
| 142 | $filename = Str::slug($filename) . '.' . $extension; |
||
| 143 | |||
| 144 | return $filename; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param $files |
||
| 149 | * @throws FileTooBigException |
||
| 150 | */ |
||
| 151 | private function validateFileUploads($files): void |
||
| 152 | { |
||
| 153 | foreach ($files as $_files) { |
||
| 154 | foreach ($_files as $file) { |
||
| 155 | if ($file instanceof UploadedFile && !$file->isValid()) { |
||
| 156 | if ($file->getError() == UPLOAD_ERR_INI_SIZE) { |
||
| 157 | throw new FileTooBigException( |
||
| 158 | 'Cannot upload file because it exceeded the allowed upload_max_filesize: upload_max_filesize is smaller than post size. ' . |
||
| 159 | 'upload_max_filesize: ' . (int)ini_get('upload_max_filesize') . 'MB, ' . |
||
| 160 | 'post_max_size: ' . (int)(ini_get('post_max_size')) . 'MB' |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | private function validateParameters(array $files_by_type, array $files_order_by_type) |
||
| 169 | { |
||
| 170 | $actions = ['new', 'replace', 'delete']; |
||
| 171 | foreach ($files_by_type as $type => $files) { |
||
| 172 | foreach ($files as $locale => $_files) { |
||
| 173 | if (!in_array($locale, config('translatable.locales'))) { |
||
| 174 | throw new \InvalidArgumentException('Corrupt file payload. key is expected to be a valid locale [' . implode(',', config('translatable.locales', [])). ']. Instead [' . $locale . '] is given.'); |
||
| 175 | } |
||
| 176 | |||
| 177 | if (!is_array($_files)) { |
||
| 178 | 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.'); |
||
| 179 | } |
||
| 180 | |||
| 181 | foreach ($_files as $action => $file) { |
||
| 182 | if (!in_array($action, $actions)) { |
||
| 183 | throw new \InvalidArgumentException('A valid files entry should have a key of either ['.implode(',', $actions).']. Instead ' . $action . ' is given.'); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | private function sanitizeFilesParameter(array $files_by_type): array |
||
| 209 | } |
||
| 210 | |||
| 211 | private function sanitizeFilesOrderParameter(array $files_order_by_locale): array |
||
| 222 | } |
||
| 223 | } |
||
| 224 |