Skip to content

Commit

Permalink
Copy file before transforming it into uploadedFile
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLe committed Nov 6, 2024
1 parent 7803d62 commit 7879b0a
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions Service/CsvProductImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public function importProductsFromCsv($filePath, Country $country = null, string
throw new \RuntimeException("Cannot open file: $filePath");
}

$headers = fgetcsv($handle, 1000, ',');
while (($data = fgetcsv($handle, 1000, ',')) !== false) {
$headers = fgetcsv($handle, 1000);
while (($data = fgetcsv($handle, 1000)) !== false) {
if (!$productData = array_combine($headers, $data)) {
throw new \RuntimeException('Problem while combining headers and data.');
}
Expand All @@ -126,6 +126,10 @@ public function importProductsFromCsv($filePath, Country $country = null, string
Tlog::getInstance()->addWarning('Missing Product reference');
continue;
}
if (!$productData[self::PRICE_EXCL_TAX_COLUMN]) {
Tlog::getInstance()->addWarning('Missing Product price for:'. $productData[self::REF_COLUMN]);
continue;
}
$product = $this->findOrCreateProduct(
$productData,
$country,
Expand Down Expand Up @@ -304,13 +308,9 @@ private function dispatchProductEvent(ProductCreateEvent|ProductUpdateEvent $eve
->setDefaultCategory($category->getId())
->setBasePrice($productData[self::PRICE_EXCL_TAX_COLUMN])
->setBaseWeight($productData[self::WEIGHT_COLUMN] ?? 0)
->setTaxRuleId($this->findOrCreateTax($productData, $country, $locale)->getId());

if ($isNew) {
$event->setVisible(1)
->setCurrencyId(1);
}

->setTaxRuleId($this->findOrCreateTax($productData, $country, $locale)->getId())
->setVisible(1)
->setCurrencyId(1);
if ($event instanceof ProductUpdateEvent) {
$event
->setChapo($productData[self::SHORT_DESCRIPTION_COLUMN])
Expand Down Expand Up @@ -342,7 +342,7 @@ private function createOrUpdateProductSaleElements(Product $product, array $prod
$attributeAvList = [];
$attributeAvListIds = [];
foreach ($productData[self::ATTRIBUTES] as $attributeColumn => $attributeTitle) {
$attribute = $this->findOrCreateAttribute(substr($attributeColumn, 2), $locale);
$attribute = $this->findOrCreateAttribute($attributeColumn, $locale);
$attributeAv = $this->findOrCreateAttributeAv($attributeTitle, $attribute, $locale);
$attributeAvList[] = $attributeAv;
$attributeAvListIds[] = $attributeAv->getId();
Expand Down Expand Up @@ -501,7 +501,7 @@ private function addAttributeToTemplate(Template $template, Attribute $attribute
private function addFeaturesToProduct(Product $product, array $productData, string $locale): void
{
foreach ($productData[self::FEATURES] as $featureColumn => $featureTitle) {
$feature = $this->findOrCreateFeature(substr($featureColumn, 2), $locale);
$feature = $this->findOrCreateFeature($featureColumn, $locale);
$featureAv = $this->findOrCreateFeatureAv($featureTitle, $feature, $locale);
$this->addFeatureToTemplate($product->getTemplate(), $feature);
$featureProduct = FeatureProductQuery::create()
Expand Down Expand Up @@ -588,11 +588,25 @@ private function addImages(Product $product, ProductSaleElements $productSaleEle
if (!$productImage->isNew() && file_exists($filePath) && is_file($filePath)) {
$this->fileManager->deleteFile($filePath);
}
$uploadedFile = new UploadedFile($filePath, $fileName);

$uploadedFile = new UploadedFile($this->copyFile($filePath), $fileName);
$event = new FileCreateOrUpdateEvent($product->getId());
$event->setModel($productImage)
->setUploadedFile($uploadedFile);
$this->dispatcher->dispatch($event, TheliaEvents::IMAGE_SAVE);
$this->addProductSaleElementImage($productSaleElements, $product, $event->getUploadedFile()->getFilename());
}

private function copyFile(string $filePath): string
{
$fileInfo = pathinfo($filePath);
$directory = $fileInfo['dirname'];
$filename = $fileInfo['filename'];
$extension = $fileInfo['extension'];

$newFilename = $filename . '_copy.' . $extension;
$newFilePath = $directory . '/' . $newFilename;
copy($filePath, $newFilePath);
return $newFilePath;
}
}

0 comments on commit 7879b0a

Please sign in to comment.