src/Subscriber/User/AmateurUpgrade/WebmasterSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\User\AmateurUpgrade;
  3. use App\Event\User\AmateurUpgradeCompleteEvent;
  4. use App\Service\User\WebmasterService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class WebmasterSubscriber implements EventSubscriberInterface
  7. {
  8.     protected WebmasterService $service;
  9.     public function __construct(WebmasterService $service)
  10.     {
  11.         $this->service $service;
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             AmateurUpgradeCompleteEvent::class => [
  17.                 ['createWebmasterAfterSuccessfulUpgrade'0],
  18.             ],
  19.         ];
  20.     }
  21.     /**
  22.      * @throws \Doctrine\ORM\ORMException
  23.      * @throws \Doctrine\ORM\OptimisticLockException
  24.      */
  25.     public function createWebmasterAfterSuccessfulUpgrade(AmateurUpgradeCompleteEvent $event)
  26.     {
  27.         if ($event->isRejected()) {
  28.             return;
  29.         }
  30.         $account $event->getAmateurUpgrade()->getAccount();
  31.         if ($account->getWebmaster()) {
  32.             // the new amateur has already been a webmaster
  33.             return;
  34.         }
  35.         // amateurs should automatically also get webmaster access
  36.         $wm $this->service->createWebmasterForAccount($account);
  37.         // if the amateur has a person assigned, use this person data for the new WM aswell
  38.         if ($person $account->getMember()->getPerson()) {
  39.             // probably rare
  40.             $wm->setPerson($person);
  41.             $this->service->storeEntity($wm);
  42.         } else {
  43.             // there probably will be a person entity related with the new amateurupgrade.
  44.             // use this for the WM aswell, IF the member did not have a person yet
  45.             // this will be the case most of the time.
  46.             // discussed on feb 23rd 2023
  47.             if ($person $event->getAmateurUpgrade()->getPerson()) {
  48.                 $wm->setPerson($person);
  49.                 $this->service->storeEntity($wm);
  50.             }
  51.         }
  52.         $account->setWebmaster($wm);
  53.         $this->service->storeEntity($account);
  54.     }
  55. }