Как можно отрефакторить этот кусок кода?
Всем привет, подскажите, как можно улучшить этот кода? либо разбить может его на более мелкие части?
public function upload(CreateUploadImageAction $createUploadImageAction, $file)
{
$image = Image::make($file);
$filename = $this->getFileName($file->getClientOriginalName(), $file->getClientOriginalExtension());
$data['filename'] = $filename;
$data['path'] = config('filesystems.disks.s3.url') . '/' . $this->storagePath;
foreach ($this->storageSizes as $key => $storageSize) {
$imageResize = $image->resize($storageSize['width'], $storageSize['height'], function ($constraint) {
$constraint->aspectRatio();
});
try {
Storage::disk('s3')->put("{$this->storagePath}/{$key}/{$filename}", $imageResize->stream());
$data['sizes'][] = $key;
} catch (Exception $e) {
return $e->getMessage();
}
}
try {
$query = $createUploadImageAction->handle(['path' => $data['path'], 'image' => $filename]);
$data['id'] = $query->id;
} catch (Exception $e) {
return $e->getMessage();
}
}