src/EventSubscriber/Profile/FeedSubscriber.php line 34

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\Profile;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use App\Events\Profile\FeedAddEvent;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use App\Entity\Profile\Feed;
  14. class FeedSubscriber implements EventSubscriberInterface
  15. {
  16.     private $em;
  17.     public function __construct(
  18.         EntityManagerInterface $em
  19.     ) {
  20.         $this->em $em;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             FeedAddEvent::NAME => 'onFeedAddEvent',
  26.         ];
  27.     }
  28.     public function onFeedAddEvent(FeedAddEvent $event): void
  29.     {
  30.         // first we check if feed dont exist
  31.         $feed $this->em->getRepository(Feed::class)->findOneBy([
  32.             'profile' => $event->getProfile(),
  33.             'type' => $event->getType(),
  34.             'entityId' => $event->getEntity(),
  35.         ]);
  36.         if (!$feed) {
  37.             $feed = new Feed();
  38.             $feed->setProfile($event->getProfile());
  39.             $feed->setType($event->getType());
  40.             $feed->setEntityId($event->getEntity());
  41.             $feed->setPrivacy($event->getPrivacy());
  42.             $this->em->persist($feed);
  43.             $this->em->flush();
  44.         }
  45.     }
  46. }