<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/
declare(strict_types=1);
namespace BitBag\SyliusCmsPlugin\EventListener;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Webmozart\Assert\Assert;
final class PageImageUploadListener
{
/** @var ImageUploaderInterface */
private $uploader;
public function __construct(ImageUploaderInterface $uploader)
{
$this->uploader = $uploader;
}
public function uploadImage(ResourceControllerEvent $event): void
{
$page = $event->getSubject();
Assert::isInstanceOf($page, PageInterface::class);
/** @var PageTranslationInterface $translation */
foreach ($page->getTranslations() as $translation) {
$image = $translation->getImage();
if (null !== $image && true === $image->hasFile()) {
$this->uploader->upload($image);
}
}
}
}