This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from pamil/images-support
Synchronize images paths
- Loading branch information
Showing
6 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylake\SyliusConsumerPlugin\Attribute; | ||
|
||
use Sylake\SyliusConsumerPlugin\Model\Attribute; | ||
use Sylius\Component\Core\Model\ProductImageInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
|
||
final class ImageAttributeProcessor implements AttributeProcessorInterface | ||
{ | ||
/** @var FactoryInterface */ | ||
private $productImageFactory; | ||
|
||
/** @var RepositoryInterface */ | ||
private $productImageRepository; | ||
|
||
/** @var string */ | ||
private $imageAttribute; | ||
|
||
public function __construct( | ||
FactoryInterface $productImageFactory, | ||
RepositoryInterface $productImageRepository, | ||
string $imageAttribute | ||
) { | ||
$this->productImageFactory = $productImageFactory; | ||
$this->productImageRepository = $productImageRepository; | ||
$this->imageAttribute = $imageAttribute; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function process(ProductInterface $product, Attribute $attribute): void | ||
{ | ||
if (!$this->supports($attribute)) { | ||
return; | ||
} | ||
|
||
foreach ($product->getImagesByType('akeneo') as $productImage) { | ||
$product->removeImage($productImage); | ||
} | ||
|
||
if (null === $attribute->data()) { | ||
return; | ||
} | ||
|
||
/** @var ProductImageInterface|null $image */ | ||
$productImage = $this->productImageRepository->findOneBy([ | ||
'owner' => $product, | ||
'type' => 'akeneo', | ||
'path' => $attribute->data(), | ||
]); | ||
|
||
if (null === $productImage) { | ||
/** @var ProductImageInterface $productImage */ | ||
$productImage = $this->productImageFactory->createNew(); | ||
$productImage->setType('akeneo'); | ||
$productImage->setPath($attribute->data()); | ||
} | ||
|
||
$product->addImage($productImage); | ||
} | ||
|
||
private function supports(Attribute $attribute): bool | ||
{ | ||
return $this->imageAttribute === $attribute->attribute() && (null === $attribute->data() || is_string($attribute->data())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters