<?php
namespace App\EventSubscriber;
use App\Entity\BlogArticle;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EasyAdminSubscriberBlogPosted implements EventSubscriberInterface
{
/**
* @return string[][]
*/
public static function getSubscribedEvents(): array
{
return [
BeforeEntityPersistedEvent::class => ['setBlogPostedAt'],
];
}
/**
* @param BeforeEntityPersistedEvent $event
* @return void
*/
public function setBlogPostedAt(BeforeEntityPersistedEvent $event): void
{
$blog = $event->getEntityInstance();
if (!($blog instanceof BlogArticle)) {
return;
}
$blog->setCreatedAt(new \DateTime());
}
}