src/EventSubscriber/Media/ProfileSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * @since 1.0.0
  4.  * @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
  5.  * @website http://artmedia.biz.pl
  6.  * @author Arkadiusz Tobiasz
  7.  * @email kontakt@artmedia.biz.pl
  8.  */
  9. namespace App\EventSubscriber\Media;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use App\Events\User\ProfileCreatedEvent;
  13. use App\Events\User\ProfileBeforeDeleteEvent;
  14. use App\Entity\Media\Album;
  15. use App\Entity\Media\Credit;
  16. class ProfileSubscriber implements EventSubscriberInterface
  17. {
  18.     private $em;
  19.     public function __construct(
  20.         EntityManagerInterface $em
  21.     ) {
  22.         $this->em $em;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProfileCreatedEvent::NAME => 'onProfileCreatedEvent',
  28.             ProfileBeforeDeleteEvent::NAME => 'onProfileBeforeDeleteEvent',
  29.         ];
  30.     }
  31.     public function onProfileBeforeDeleteEvent(ProfileBeforeDeleteEvent $event): void
  32.     {
  33.         if (!$event->getProfile()) {
  34.             return;
  35.         }
  36.         $credits $this->em->getRepository(Credit::class)->findBy([
  37.             'profile' => $event->getProfile()
  38.         ]);
  39.         if ($credits) {
  40.             foreach ($credits as $credit) {
  41.                 $credit->setProfile(null);
  42.             }
  43.             $this->em->flush();
  44.         }
  45.     }
  46.     public function onProfileCreatedEvent(ProfileCreatedEvent $event): void
  47.     {
  48.         if (!$event->getProfile()) {
  49.             return;
  50.         }
  51.         $album = new Album();
  52.         $album->setTitle('Portfolio')
  53.             ->setProfile($event->getProfile())
  54.             ->setPortfolio(true)
  55.         ;
  56.         $this->em->persist($album);
  57.         $this->em->flush();
  58.     }
  59. }