src/Subscriber/User/AccountStatusSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\User;
  3. use App\Dictionary\ContentStatus;
  4. use App\Entity\Content;
  5. use App\Event\Mail\DirectLinkRequestEvent;
  6. use App\Event\User\AccountStatusChangeEvent;
  7. use App\Security\UserChecker;
  8. use App\Service\Content\ContentService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class AccountStatusSubscriber implements EventSubscriberInterface
  11. {
  12.     protected ContentService $contentService;
  13.     protected UserChecker $userChecker;
  14.     public function __construct(ContentService $contentServiceUserChecker $userChecker)
  15.     {
  16.         $this->contentService $contentService;
  17.         $this->userChecker $userChecker;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             AccountStatusChangeEvent::class => [
  23.                 ['onAccountStatusChange'0],
  24.             ],
  25.             DirectLinkRequestEvent::class => [
  26.                 ['onRequestDirectLinkUpdateAccountStatus'1],
  27.             ],
  28.         ];
  29.     }
  30.     /**
  31.      * @throws \Doctrine\ORM\ORMException
  32.      * @throws \Doctrine\ORM\OptimisticLockException
  33.      */
  34.     public function onRequestDirectLinkUpdateAccountStatus(DirectLinkRequestEvent $event): bool
  35.     {
  36.         $account $event->getAccount();
  37.         if ($this->userChecker->isReactivationPossible($account)) {
  38.             return $this->userChecker->reactivateDeletedAccount($account);
  39.         }
  40.         return false;
  41.     }
  42.     /**
  43.      * @throws \Doctrine\ORM\ORMException
  44.      * @throws \Doctrine\ORM\OptimisticLockException
  45.      */
  46.     public function onAccountStatusChange(AccountStatusChangeEvent $event)
  47.     {
  48.         if ($event->gotBlocked()) {
  49.             $this->onAccountBlocked($event);
  50.         }
  51.         if ($event->gotReenabled()) {
  52.             $this->onAccountReenabled($event);
  53.         }
  54.     }
  55.     /**
  56.      * account geblockt oder gelöscht:
  57.      * - aktiven content (6) auf inactive (7) setzen
  58.      * - nicht auf 9 (blocked) setzen, da dann bei reaktivierung nicht mehr
  59.      * zwischen "ursprünglich aktivem" content und gewollt blockiertem
  60.      * content unterschieden werden kann
  61.      *
  62.      * @throws \Doctrine\ORM\ORMException
  63.      * @throws \Doctrine\ORM\OptimisticLockException
  64.      */
  65.     public function onAccountBlocked(AccountStatusChangeEvent $event)
  66.     {
  67.         $member $event->getAccount()->getMember();
  68.         $contentService $this->getContentService();
  69.         // search for active content
  70.         $contents $contentService->getContentsByMember($member, [
  71.             ContentStatus::ACTIVE,
  72.         ]);
  73.         foreach ($contents as $content) {
  74.             $contentService->changeContentStatus($contentContentStatus::INACTIVEfalse);
  75.         }
  76.         if (count($contents)) {
  77.             $contentService->delayedFlush();
  78.         }
  79.     }
  80.     /**
  81.      * Account von "gelöscht" oder "geblockt" wieder auf "aktiv" gesetzt =
  82.      * inaktiven content (7) wieder auf active (6) setzen.
  83.      *
  84.      * @throws \Doctrine\ORM\ORMException
  85.      * @throws \Doctrine\ORM\OptimisticLockException
  86.      */
  87.     public function onAccountReenabled(AccountStatusChangeEvent $event)
  88.     {
  89.         $member $event->getAccount()->getMember();
  90.         $contentService $this->getContentService();
  91.         // search for inactive content
  92.         $contents $contentService->getContentsByMember($member, [
  93.             ContentStatus::INACTIVE,
  94.         ]);
  95.         foreach ($contents as $content) {
  96.             $contentService->changeContentStatus($contentContentStatus::ACTIVEfalse);
  97.         }
  98.         if (count($contents)) {
  99.             $contentService->delayedFlush();
  100.         }
  101.     }
  102.     protected function getContentService(): ContentService
  103.     {
  104.         return $this->contentService;
  105.     }
  106. }