FleetCart/Modules/Api/Http/Controllers/ImportProductController.php

172 lines
5.2 KiB
PHP

<?php
namespace Modules\Api\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\Api\Http\Requests\ImportProductRequest;
use Modules\Brand\Entities\Brand;
use Modules\Brand\Entities\BrandTranslation;
use Modules\Category\Entities\Category;
use Modules\Category\Entities\CategoryTranslation;
use Modules\Media\Entities\File;
use Modules\Meta\Entities\MetaData;
use Modules\Meta\Entities\MetaDataTranslation;
use Modules\Product\Entities\Product;
class ImportProductController extends Controller {
public function __invoke(ImportProductRequest $request)
{
foreach ($request->data as $data) {
$file = $this->getFilesFromUrl($data['image']);
$product = Product::create($data + [
'brand_id' => $this->getBrandId($data['brand']),
]) ;
$this->createMeta($data['seo'] ?? [], Product::class, $product->id);
$product->files()->attach([$file->id => ['zone' => 'base_image']]);
$product->categories()->attach($this->getCategories($data['categories']));
}
return response()->json([
'status' => 'success'
]);
}
private function getCategories($productCategories) {
$categories = [];
foreach ($productCategories as $productCategory) {
$parendId = null;
foreach ($productCategory as $category) {
$parendId = $this->getCategoryFromName($category, $parendId);
}
$categories[] = $parendId;
}
return $categories;
}
private function getCategoryFromName($data, $parendId) {
$category = CategoryTranslation::where([
'name' => $data['name'],
'locale' => 'en'
])->first();
if (!empty($category)) {
return $category->category_id;
}
$category = new Category();
$category->name = $data['name'];
$category->is_active = true;
$category->is_searchable = true;
$category->parent_id = $parendId;
$category->save();
if (isset($data['image']['logo']) && !empty($data['image']['logo'])) {
$this->createLogo($data['image']['logo'], $category, Category::class);
}
if (isset($data['image']['banner']) && !empty($data['image']['banner'])) {
$this->createBanner($data['image']['banner'], $category, Category::class);
}
return $category->id;
}
private function getBrandId($data) {
// dd($data);
$brand = BrandTranslation::where([
'name' => $data['name'],
'locale' => 'en'
])->first();
if (!empty($brand)) {
return $brand->brand_id;
}
$brand = new Brand();
$brand->name = $data['name'];
$brand->is_active = true;
$brand->save();
$this->createMeta($data['seo'] ?? [], Brand::class, $brand->id);
if (isset($data['image']['logo']) && !empty($data['image']['logo'])) {
$this->createLogo($data['image']['logo'], $brand, Brand::class);
}
if (isset($data['image']['banner']) && !empty($data['image']['banner'])) {
$this->createBanner($data['image']['banner'], $brand, Brand::class);
}
return $brand->id;
}
private function getFilesFromUrl($imageUrl) {
$headers = get_headers($imageUrl, 1);
$fileContents = file_get_contents($imageUrl);
$path = 'media/' . time() . Str::random(6) . '.' . pathinfo($imageUrl, PATHINFO_EXTENSION);
Storage::put($path, $fileContents);
return File::create([
'user_id' => 1,
'disk' => config('filesystems.default'),
'filename' => pathinfo($imageUrl, PATHINFO_BASENAME),
'path' => $path,
'extension' => pathinfo($imageUrl, PATHINFO_EXTENSION) ?? '',
'mime' => $headers['Content-Type'],
'size' => strlen($fileContents),
]);
}
private function createMeta($seo, $class, $id) {
$metaData = new MetaDataTranslation();
$metaData->meta_title = isset($seo['title']) ? $seo['title'] : '';
$metaData->meta_description = isset($seo['description']) ? $seo['description'] : '';
$metaData->locale = 'en';
$metaData->meta_data_id = $this->getMetaId($class, $id);
$metaData->save();
}
private function getMetaId($class, $id) {
$meta = MetaData::where([
'entity_type' => $class,
'entity_id' => $id,
])->first();
if (!empty($meta)) {
return $meta->id;
}
$meta = new MetaData();
$meta->entity_type = $class;
$meta->entity_id = $id;
$meta->save();
return $meta->id;
}
private function createLogo($url, $class, $className) {
$file = $this->getFilesFromUrl($url);
$class->files()->attach([$file->id => ['zone' => 'logo']]);
}
private function createBanner($url, $class, $className) {
$file = $this->getFilesFromUrl($url);
$class->files()->attach([$file->id => ['zone' => 'banner']]);
}
}