<?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\Media;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Events\User\ProfileCreatedEvent;
use App\Events\User\ProfileBeforeDeleteEvent;
use App\Entity\Media\Album;
use App\Entity\Media\Credit;
class ProfileSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(
EntityManagerInterface $em
) {
$this->em = $em;
}
public static function getSubscribedEvents(): array
{
return [
ProfileCreatedEvent::NAME => 'onProfileCreatedEvent',
ProfileBeforeDeleteEvent::NAME => 'onProfileBeforeDeleteEvent',
];
}
public function onProfileBeforeDeleteEvent(ProfileBeforeDeleteEvent $event): void
{
if (!$event->getProfile()) {
return;
}
$credits = $this->em->getRepository(Credit::class)->findBy([
'profile' => $event->getProfile()
]);
if ($credits) {
foreach ($credits as $credit) {
$credit->setProfile(null);
}
$this->em->flush();
}
}
public function onProfileCreatedEvent(ProfileCreatedEvent $event): void
{
if (!$event->getProfile()) {
return;
}
$album = new Album();
$album->setTitle('Portfolio')
->setProfile($event->getProfile())
->setPortfolio(true)
;
$this->em->persist($album);
$this->em->flush();
}
}