Skip to content

Instantly share code, notes, and snippets.

@janmyszkier
Created January 3, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janmyszkier/90618d662dbfabc2051e8ed7c6174278 to your computer and use it in GitHub Desktop.
Save janmyszkier/90618d662dbfabc2051e8ed7c6174278 to your computer and use it in GitHub Desktop.
Product Import Multiple Images Importer Step
<?php
/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
namespace Pyz\Zed\DataImport\Business\Model\ProductImage;
use Orm\Zed\ProductImage\Persistence\SpyProductImage;
use Orm\Zed\ProductImage\Persistence\SpyProductImageQuery;
use Orm\Zed\ProductImage\Persistence\SpyProductImageSet;
use Orm\Zed\ProductImage\Persistence\SpyProductImageSetQuery;
use Orm\Zed\ProductImage\Persistence\SpyProductImageSetToProductImageQuery;
use Pyz\Zed\DataImport\Business\Model\Locale\Repository\LocaleRepositoryInterface;
use Pyz\Zed\DataImport\Business\Model\Product\Repository\ProductRepositoryInterface;
use Spryker\Install\Configuration\ConfigurationFactory;
use Spryker\Shared\Config\Config;
use Spryker\Zed\DataImport\Business\Model\DataImportStep\DataImportStepInterface;
use Spryker\Zed\DataImport\Business\Model\DataImportStep\PublishAwareStep;
use Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface;
use Spryker\Zed\Product\Dependency\ProductEvents;
use Spryker\Zed\ProductImage\Dependency\ProductImageEvents;
use SprykerEco\Shared\AkeneoPim\AkeneoPimConstants;
class ProductMultipleImagesWriterStep extends PublishAwareStep implements DataImportStepInterface
{
public const BULK_SIZE = 100;
public const KEY_LOCALE = 'locale';
public const KEY_IMAGE_SET_NAME = 'image_set_name';
public const KEY_ABSTRACT_SKU = 'abstract_sku';
public const KEY_CONCRETE_SKU = 'concrete_sku';
public const KEY_EXTERNAL_URL_LARGE = 'external_url_large';
public const KEY_EXTERNAL_URL_SMALL = 'external_url_small';
public const DEFAULT_IMAGE_SORT_ORDER = 0;
/**
* @var \Pyz\Zed\DataImport\Business\Model\Locale\Repository\LocaleRepositoryInterface
*/
protected $localeRepository;
/**
* @var \Pyz\Zed\DataImport\Business\Model\Product\Repository\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @param \Pyz\Zed\DataImport\Business\Model\Locale\Repository\LocaleRepositoryInterface $localeRepository
* @param \Pyz\Zed\DataImport\Business\Model\Product\Repository\ProductRepositoryInterface $productRepository
*/
public function __construct(LocaleRepositoryInterface $localeRepository, ProductRepositoryInterface $productRepository)
{
$this->localeRepository = $localeRepository;
$this->productRepository = $productRepository;
}
/**
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
*
* @return void
*/
public function execute(DataSetInterface $dataSet)
{
$imageSetEntity = $this->findOrCreateImageSet($dataSet);
$images = $this->createImagesFromAttributes($dataSet);
foreach($images as $imageInfo) {
$productImageEntity = $this->findOrCreateImage($imageInfo);
$this->updateOrCreateImageToImageSetRelation($imageSetEntity, $productImageEntity);
}
}
/**
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
*
* @return \Orm\Zed\ProductImage\Persistence\SpyProductImageSet
*/
protected function findOrCreateImageSet(DataSetInterface $dataSet)
{
$idLocale = $this->getIdLocaleByLocale($dataSet);
if(empty($dataSet[static::KEY_IMAGE_SET_NAME])){
$dataSet[static::KEY_IMAGE_SET_NAME] = 'Akeneo';
}
$query = SpyProductImageSetQuery::create()
->filterByName($dataSet[static::KEY_IMAGE_SET_NAME])
->filterByFkLocale($idLocale);
if (!empty($dataSet[static::KEY_ABSTRACT_SKU])) {
$idProductAbstract = $this->productRepository->getIdProductAbstractByAbstractSku($dataSet[static::KEY_ABSTRACT_SKU]);
$query->filterByFkProductAbstract($idProductAbstract);
}
if (!empty($dataSet[static::KEY_CONCRETE_SKU])) {
$idProduct = $this->productRepository->getIdProductByConcreteSku($dataSet[static::KEY_CONCRETE_SKU]);
$query->filterByFkProduct($idProduct);
}
$productImageSetEntity = $query->findOneOrCreate();
if ($productImageSetEntity->isNew() || $productImageSetEntity->isModified()) {
$productImageSetEntity->save();
$this->addImagePublishEvents($productImageSetEntity);
}
return $productImageSetEntity;
}
/**
* @param \Spryker\Zed\DataImport\Business\Model\DataSet\DataSetInterface $dataSet
*
* @return int
*/
protected function getIdLocaleByLocale(DataSetInterface $dataSet)
{
$idLocale = null;
if (!empty($dataSet[static::KEY_LOCALE])) {
$idLocale = $this->localeRepository->getIdLocaleByLocale($dataSet[static::KEY_LOCALE]);
}
return $idLocale;
}
/**
* We expect that the large URL is the unique identifier for an image.
*
* @param $dataSet
*
* @return \Orm\Zed\ProductImage\Persistence\SpyProductImage
*/
protected function findOrCreateImage($dataSet)
{
$productImageEntity = SpyProductImageQuery::create()
->filterByExternalUrlLarge($dataSet[static::KEY_EXTERNAL_URL_LARGE])
->findOneOrCreate();
$productImageEntity
->setExternalUrlSmall($dataSet[static::KEY_EXTERNAL_URL_SMALL]);
if ($productImageEntity->isNew() || $productImageEntity->isModified()) {
$productImageEntity->save();
}
return $productImageEntity;
}
/**
* @param \Orm\Zed\ProductImage\Persistence\SpyProductImageSet $imageSetEntity
* @param \Orm\Zed\ProductImage\Persistence\SpyProductImage $productImageEntity
*
* @return void
*/
protected function updateOrCreateImageToImageSetRelation(SpyProductImageSet $imageSetEntity, SpyProductImage $productImageEntity)
{
$productImageSetToProductImageEntity = SpyProductImageSetToProductImageQuery::create()
->filterByFkProductImageSet($imageSetEntity->getIdProductImageSet())
->filterByFkProductImage($productImageEntity->getIdProductImage())
->findOneOrCreate();
$productImageSetToProductImageEntity
->setSortOrder(static::DEFAULT_IMAGE_SORT_ORDER);
if ($productImageSetToProductImageEntity->isNew() || $productImageSetToProductImageEntity->isModified()) {
$productImageSetToProductImageEntity->save();
}
}
/**
* @param \Orm\Zed\ProductImage\Persistence\SpyProductImageSet $productImageSetEntity
*
* @return void
*/
protected function addImagePublishEvents(SpyProductImageSet $productImageSetEntity)
{
if ($productImageSetEntity->getFkProductAbstract()) {
$this->addPublishEvents(ProductImageEvents::PRODUCT_IMAGE_PRODUCT_ABSTRACT_PUBLISH, $productImageSetEntity->getFkProductAbstract());
$this->addPublishEvents(ProductEvents::PRODUCT_ABSTRACT_PUBLISH, $productImageSetEntity->getFkProductAbstract());
} elseif ($productImageSetEntity->getFkProduct()) {
$this->addPublishEvents(ProductImageEvents::PRODUCT_IMAGE_PRODUCT_CONCRETE_PUBLISH, $productImageSetEntity->getFkProduct());
}
}
private function createImagesFromAttributes($dataSet)
{
$imageHost = Config::get(AkeneoPimConstants::HOST);
$smallImagePrefix = $imageHost.'/media/cache/thumbnail_small/';
$fullImagePrefix = $imageHost.'/media/cache/preview/';
$imageKeys= ['image','image2','image3','image4','image5','image6','image7'];
$images = [];
if(!empty($dataSet['attributes'])){
foreach($imageKeys as $imageArrayKey) {
if (!empty($dataSet['attributes'][$imageArrayKey])) {
$images[] = [
static::KEY_EXTERNAL_URL_SMALL => $smallImagePrefix . $dataSet['attributes'][$imageArrayKey],
static::KEY_EXTERNAL_URL_LARGE => $fullImagePrefix . $dataSet['attributes'][$imageArrayKey],
];
}
}
}
return $images;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment