<?php
namespace App\Subscriber\User\AmateurUpgrade;
use App\Event\User\AmateurUpgradeCompleteEvent;
use App\Service\User\WebmasterService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class WebmasterSubscriber implements EventSubscriberInterface
{
protected WebmasterService $service;
public function __construct(WebmasterService $service)
{
$this->service = $service;
}
public static function getSubscribedEvents(): array
{
return [
AmateurUpgradeCompleteEvent::class => [
['createWebmasterAfterSuccessfulUpgrade', 0],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function createWebmasterAfterSuccessfulUpgrade(AmateurUpgradeCompleteEvent $event)
{
if ($event->isRejected()) {
return;
}
$account = $event->getAmateurUpgrade()->getAccount();
if ($account->getWebmaster()) {
// the new amateur has already been a webmaster
return;
}
// amateurs should automatically also get webmaster access
$wm = $this->service->createWebmasterForAccount($account);
// if the amateur has a person assigned, use this person data for the new WM aswell
if ($person = $account->getMember()->getPerson()) {
// probably rare
$wm->setPerson($person);
$this->service->storeEntity($wm);
} else {
// there probably will be a person entity related with the new amateurupgrade.
// use this for the WM aswell, IF the member did not have a person yet
// this will be the case most of the time.
// discussed on feb 23rd 2023
if ($person = $event->getAmateurUpgrade()->getPerson()) {
$wm->setPerson($person);
$this->service->storeEntity($wm);
}
}
$account->setWebmaster($wm);
$this->service->storeEntity($account);
}
}