<?php
namespace App\EventSubscriber;
use App\Event\Page\PageCreatedEvent;
use App\Repository\RolesRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PageSubscriber implements EventSubscriberInterface
{
/**
* @var RolesRepository
*/
private $rolesRepository;
public function __construct(RolesRepository $rolesRepository)
{
$this->rolesRepository = $rolesRepository;
}
public static function getSubscribedEvents(): array
{
return [
PageCreatedEvent::class => ['onPageCreated', 100],
];
}
public function onPageCreated(PageCreatedEvent $event)
{
// On ajoute automatiquement la page à l'admin
$page = $event->getPage();
$profilAdmin = $this->rolesRepository->findOneBy(['identifiant' => 'ROLE_ADMIN']);
$page->addRole($profilAdmin);
}
}