From 757dc9a8ed0e541edd4394e0ac6f78891ee115ee Mon Sep 17 00:00:00 2001 From: Devansh Date: Thu, 3 Feb 2022 16:42:37 +0530 Subject: [PATCH] Refactored Image And Video Repository --- .../Repositories/ProductImageRepository.php | 75 +----------- .../Repositories/ProductMediaRepository.php | 113 ++++++++++++++++++ .../Repositories/ProductVideoRepository.php | 61 +--------- 3 files changed, 119 insertions(+), 130 deletions(-) create mode 100644 packages/Webkul/Product/src/Repositories/ProductMediaRepository.php diff --git a/packages/Webkul/Product/src/Repositories/ProductImageRepository.php b/packages/Webkul/Product/src/Repositories/ProductImageRepository.php index f930acbfb..f7b4c72ef 100755 --- a/packages/Webkul/Product/src/Repositories/ProductImageRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductImageRepository.php @@ -3,12 +3,9 @@ namespace Webkul\Product\Repositories; use Illuminate\Container\Container as App; -use Illuminate\Http\UploadedFile; -use Illuminate\Support\Facades\Storage; -use Webkul\Core\Eloquent\Repository; use Webkul\Product\Repositories\ProductRepository; -class ProductImageRepository extends Repository +class ProductImageRepository extends ProductMediaRepository { /** * Product repository object. @@ -43,17 +40,6 @@ class ProductImageRepository extends Repository return \Webkul\Product\Contracts\ProductImage::class; } - /** - * Get product directory. - * - * @param \Webkul\Product\Contracts\Product $product - * @return string - */ - public function getProductDirectory($product): string - { - return 'product/' . $product->id; - } - /** * Upload images. * @@ -63,66 +49,13 @@ class ProductImageRepository extends Repository */ public function uploadImages($data, $product): void { - $this->upload($product, $data['images'] ?? null); + $this->upload($data, $product, 'images'); if (isset($data['variants'])) { $this->uploadVariantImages($data['variants']); } } - /** - * Upload. - * - * @param \Webkul\Product\Contracts\Product $product - * @param array - * @return void - */ - public function upload($product, $images): void - { - /** - * Previous images ids for filtering. - */ - $previousImageIds = $product->images()->pluck('id'); - - if ( - isset($images['files']) && $images['files'] - && isset($images['positions']) && $images['positions'] - ) { - /** - * Filter out existing images because new image positions are already setuped by index. - */ - $imagePositions = collect($images['positions'])->keys()->filter(function ($imagePosition) { - return is_numeric($imagePosition); - }); - - foreach ($images['files'] as $indexOrImageId => $image) { - if ($image instanceof UploadedFile) { - $this->create([ - 'path' => $image->store($this->getProductDirectory($product)), - 'product_id' => $product->id, - 'position' => $indexOrImageId, - ]); - } else { - $this->update([ - 'position' => $imagePositions->search($indexOrImageId), - ], $indexOrImageId); - - if (is_numeric($index = $previousImageIds->search($indexOrImageId))) { - $previousImageIds->forget($index); - } - } - } - } - - foreach ($previousImageIds as $indexOrImageId) { - if ($image = $this->find($indexOrImageId)) { - Storage::delete($image->path); - - $this->delete($indexOrImageId); - } - } - } - /** * Upload variant images. * @@ -131,14 +64,14 @@ class ProductImageRepository extends Repository */ public function uploadVariantImages($variants): void { - foreach ($variants as $variantsId => $variant) { + foreach ($variants as $variantsId => $variantData) { $product = $this->productRepository->find($variantsId); if (! $product) { break; } - $this->upload($product, $variant['images'] ?? null); + $this->upload($variantData, $product, 'images'); } } } diff --git a/packages/Webkul/Product/src/Repositories/ProductMediaRepository.php b/packages/Webkul/Product/src/Repositories/ProductMediaRepository.php new file mode 100644 index 000000000..68b1c8863 --- /dev/null +++ b/packages/Webkul/Product/src/Repositories/ProductMediaRepository.php @@ -0,0 +1,113 @@ +id; + } + + /** + * Upload. + * + * @param \Webkul\Product\Contracts\Product $product + * @param array + * @return void + */ + public function upload($data, $product, $uploadFileType): void + { + /** + * Previous model ids for filtering. + */ + $previousIds = $this->resolveFileTypeQueryBuilder($product, $uploadFileType)->pluck('id'); + + if ( + isset($data[$uploadFileType]['files']) && $data[$uploadFileType]['files'] + && isset($data[$uploadFileType]['positions']) && $data[$uploadFileType]['positions'] + ) { + /** + * Filter out existing models because new model positions are already setuped by index. + */ + $positions = collect($data[$uploadFileType]['positions'])->keys()->filter(function ($position) { + return is_numeric($position); + }); + + foreach ($data[$uploadFileType]['files'] as $indexOrModelId => $file) { + if ($file instanceof UploadedFile) { + $this->create([ + 'type' => $uploadFileType, + 'path' => $file->store($this->getProductDirectory($product)), + 'product_id' => $product->id, + 'position' => $indexOrModelId, + ]); + } else { + $this->update([ + 'position' => $positions->search($indexOrModelId), + ], $indexOrModelId); + + if (is_numeric($index = $previousIds->search($indexOrModelId))) { + $previousIds->forget($index); + } + } + } + } + + foreach ($previousIds as $indexOrModelId) { + if ($model = $this->find($indexOrModelId)) { + Storage::delete($model->path); + + $this->delete($indexOrModelId); + } + } + } + + /** + * Resolve file type query builder. + * + * @param \Webkul\Product\Contracts\Product $product + * @param string $uploadFileType + * @return mixed + * + * @throws \Exception + */ + private function resolveFileTypeQueryBuilder($product, string $uploadFileType) + { + if ($uploadFileType === 'images') { + return $product->images(); + } + + if ($uploadFileType === 'videos') { + return $product->videos(); + } + + throw new Exception('Unsupported file type.'); + } +} diff --git a/packages/Webkul/Product/src/Repositories/ProductVideoRepository.php b/packages/Webkul/Product/src/Repositories/ProductVideoRepository.php index 91c0523bc..9e7c0d139 100644 --- a/packages/Webkul/Product/src/Repositories/ProductVideoRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductVideoRepository.php @@ -2,11 +2,7 @@ namespace Webkul\Product\Repositories; -use Illuminate\Http\UploadedFile; -use Illuminate\Support\Facades\Storage; -use Webkul\Core\Eloquent\Repository; - -class ProductVideoRepository extends Repository +class ProductVideoRepository extends ProductMediaRepository { /** * Specify model class name. @@ -18,17 +14,6 @@ class ProductVideoRepository extends Repository return \Webkul\Product\Contracts\ProductVideo::class; } - /** - * Get product directory. - * - * @param \Webkul\Product\Contracts\Product $product - * @return string - */ - public function getProductDirectory($product): string - { - return 'product/' . $product->id; - } - /** * Upload videos. * @@ -38,48 +23,6 @@ class ProductVideoRepository extends Repository */ public function uploadVideos($data, $product) { - /** - * Previous images ids for filtering. - */ - $previousVideoIds = $product->videos()->pluck('id'); - - if ( - isset($data['videos']['files']) && $data['videos']['files'] - && isset($data['videos']['positions']) && $data['videos']['positions'] - ) { - /** - * Filter out existing videos because new video positions are already setuped by index. - */ - $videoPositions = collect($data['videos']['positions'])->keys()->filter(function ($videoPosition) { - return is_numeric($videoPosition); - }); - - foreach ($data['videos']['files'] as $indexOrVideoId => $video) { - if ($video instanceof UploadedFile) { - $this->create([ - 'path' => $video->store($this->getProductDirectory($product)), - 'product_id' => $product->id, - 'position' => $indexOrVideoId, - 'type' => 'video', - ]); - } else { - $this->update([ - 'position' => $videoPositions->search($indexOrVideoId), - ], $indexOrVideoId); - - if (is_numeric($index = $previousVideoIds->search($indexOrVideoId))) { - $previousVideoIds->forget($index); - } - } - } - } - - foreach ($previousVideoIds as $indexOrVideoId) { - if ($video = $this->find($indexOrVideoId)) { - Storage::delete($video->path); - - $this->delete($indexOrVideoId); - } - } + $this->upload($data, $product, 'videos'); } }