<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\EventSubscriber\Profile;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Events\Profile\FeedAddEvent;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Profile\Feed;
class FeedSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(
EntityManagerInterface $em
) {
$this->em = $em;
}
public static function getSubscribedEvents(): array
{
return [
FeedAddEvent::NAME => 'onFeedAddEvent',
];
}
public function onFeedAddEvent(FeedAddEvent $event): void
{
// first we check if feed dont exist
$feed = $this->em->getRepository(Feed::class)->findOneBy([
'profile' => $event->getProfile(),
'type' => $event->getType(),
'entityId' => $event->getEntity(),
]);
if (!$feed) {
$feed = new Feed();
$feed->setProfile($event->getProfile());
$feed->setType($event->getType());
$feed->setEntityId($event->getEntity());
$feed->setPrivacy($event->getPrivacy());
$this->em->persist($feed);
$this->em->flush();
}
}
}