tinternet.net/core/EventSuscriber/Site/Page/BlockEventSubscriber.php

61 lines
1.7 KiB
PHP

<?php
namespace App\Core\EventSuscriber\Site\Page;
use App\Core\Entity\EntityInterface;
use App\Core\Entity\Site\Page\Block;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
use App\Core\Form\FileUploadHandler;
use App\Core\Entity\Site\Page\FileBlock;
use App\Core\Entity\Site\Page\Page;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* class BlockEventSubscriber.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class BlockEventSubscriber extends EntityManagerEventSubscriber
{
protected FileUploadHandler $fileUpload;
public function __construct(FileUploadHandler $fileUpload)
{
$this->fileUpload = $fileUpload;
}
public function support(EntityInterface $entity)
{
return $entity instanceof Page;
}
public function onPreUpdate(EntityManagerEvent $event)
{
if (!$this->support($event->getEntity())) {
return;
}
foreach ($event->getEntity()->getBlocks() as $block) {
if ($block instanceof FileBlock) {
if ($block->getValue() instanceof UploadedFile) {
$directory = 'uploads/page/block';
$this->fileUpload->handleForm(
$block->getValue(),
$directory,
function ($filename) use ($block, $directory) {
$block->setValue($directory.'/'.$filename);
}
);
}
}
}
}
public function onPreCreate(EntityManagerEvent $event)
{
return $this->onPreUpdate($event);
}
}