← Back
Editing: EasyAdminSubscriber.php
<?php namespace App\EventSubscriber; use App\Entity\Item; use App\Entity\SettlementTemplate; use App\Entity\User; use App\Repository\AdresseRepository; use App\Service\UserService; use Doctrine\ORM\EntityManagerInterface; use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent; use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent; use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent; use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class EasyAdminSubscriber implements EventSubscriberInterface { public function __construct( private readonly AdresseRepository $adresseRepository, private readonly UserService $userService, private readonly EntityManagerInterface $em, private readonly string $projectDir, private readonly LoggerInterface $logger ) { } public static function getSubscribedEvents(): array { return [ BeforeEntityPersistedEvent::class => ['deleteBackground'], BeforeEntityUpdatedEvent::class => ['deleteBackground'], AfterEntityDeletedEvent::class => ['deleteBackground'], AfterEntityPersistedEvent::class => [['assignExistingAddresses'], ['sendUserCredentials']], ]; } public function sendUserCredentials(AfterEntityPersistedEvent $event): void { $entity = $event->getEntityInstance(); if (!($entity instanceof User)) { return; } $this->userService->sendNewAccountEmail($entity); } public function deleteBackground($event): void { $entity = $event->getEntityInstance(); if (!($entity instanceof SettlementTemplate)) { return; } if ($entity->isEraseBackground() && $entity->getBackground()) { $filePath = $this->projectDir.'uploads/'.$entity->getBackground(); $entity->setBackground(null); try { unlink($filePath); } catch (\Exception) { $this->logger->error('Erase of '.$filePath.' failed'); } } } public function assignExistingAddresses(AfterEntityPersistedEvent $event): void { $entity = $event->getEntityInstance(); if (!($entity instanceof Item)) { return; } if ($entity->isAddToExistingAddresses()) { $addresses = $this->adresseRepository->findAll(); foreach ($addresses as $address) { $address->addItem($entity); } $this->em->persist($address); } $this->em->flush(); } }
Save File
Cancel