<?php
namespace App\Subscriber\User;
use App\Dictionary\ContentStatus;
use App\Entity\Content;
use App\Event\Mail\DirectLinkRequestEvent;
use App\Event\User\AccountStatusChangeEvent;
use App\Security\UserChecker;
use App\Service\Content\ContentService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AccountStatusSubscriber implements EventSubscriberInterface
{
protected ContentService $contentService;
protected UserChecker $userChecker;
public function __construct(ContentService $contentService, UserChecker $userChecker)
{
$this->contentService = $contentService;
$this->userChecker = $userChecker;
}
public static function getSubscribedEvents(): array
{
return [
AccountStatusChangeEvent::class => [
['onAccountStatusChange', 0],
],
DirectLinkRequestEvent::class => [
['onRequestDirectLinkUpdateAccountStatus', 1],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onRequestDirectLinkUpdateAccountStatus(DirectLinkRequestEvent $event): bool
{
$account = $event->getAccount();
if ($this->userChecker->isReactivationPossible($account)) {
return $this->userChecker->reactivateDeletedAccount($account);
}
return false;
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onAccountStatusChange(AccountStatusChangeEvent $event)
{
if ($event->gotBlocked()) {
$this->onAccountBlocked($event);
}
if ($event->gotReenabled()) {
$this->onAccountReenabled($event);
}
}
/**
* account geblockt oder gelöscht:
* - aktiven content (6) auf inactive (7) setzen
* - nicht auf 9 (blocked) setzen, da dann bei reaktivierung nicht mehr
* zwischen "ursprünglich aktivem" content und gewollt blockiertem
* content unterschieden werden kann
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onAccountBlocked(AccountStatusChangeEvent $event)
{
$member = $event->getAccount()->getMember();
$contentService = $this->getContentService();
// search for active content
$contents = $contentService->getContentsByMember($member, [
ContentStatus::ACTIVE,
]);
foreach ($contents as $content) {
$contentService->changeContentStatus($content, ContentStatus::INACTIVE, false);
}
if (count($contents)) {
$contentService->delayedFlush();
}
}
/**
* Account von "gelöscht" oder "geblockt" wieder auf "aktiv" gesetzt =
* inaktiven content (7) wieder auf active (6) setzen.
*
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onAccountReenabled(AccountStatusChangeEvent $event)
{
$member = $event->getAccount()->getMember();
$contentService = $this->getContentService();
// search for inactive content
$contents = $contentService->getContentsByMember($member, [
ContentStatus::INACTIVE,
]);
foreach ($contents as $content) {
$contentService->changeContentStatus($content, ContentStatus::ACTIVE, false);
}
if (count($contents)) {
$contentService->delayedFlush();
}
}
protected function getContentService(): ContentService
{
return $this->contentService;
}
}