src/EventSubscriber/User/UserSubscriber.php line 219

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\User;
  3. use App\Entity\Logs\EventLog;
  4. use App\Entity\Notifications\Notification;
  5. use App\Entity\Profile\Profile;
  6. use App\Entity\Profile\ProfileAccessLog;
  7. use App\Entity\Profile\ProfileRBAC;
  8. use App\Entity\User\User;
  9. use App\Enums\Constants;
  10. use App\Event\Composition\SongWriter\SongwriterConnectedToProfileEvent;
  11. use App\Event\LogEvent;
  12. use App\Event\User\UserClosedEvent;
  13. use App\Event\User\UserCreatedEvent;
  14. use App\Event\User\UserDeletedEvent;
  15. use App\Event\User\UserSuspendedEvent;
  16. use App\Event\User\UserTerminatedEvent;
  17. use App\Event\User\UserUpdatedEvent;
  18. use App\Repository\Notifications\NotificationRepository;
  19. use App\Repository\Profile\ProfileAccessLogRepository;
  20. use App\Repository\Profile\ProfileRBACRepository;
  21. use App\Repository\User\UserProfileRepository;
  22. use App\Repository\User\UserRepository;
  23. use App\Service\AuthenticationService;
  24. use App\Service\Mailer\UserMailer;
  25. use App\Service\ProfileService;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. class UserSubscriber implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var AuthenticationService
  32.      */
  33.     private $authenticationService;
  34.     /**
  35.      * @var EventDispatcherInterface
  36.      */
  37.     private $eventDispatcher;
  38.     /** @var UserRepository */
  39.     private $userRepository;
  40.     /** @var UserMailer */
  41.     private $userMailer;
  42.     /** @var UserProfileRepository */
  43.     private $userProfileRepository;
  44.     /** @var ProfileAccessLogRepository */
  45.     private $profileAccessLogRepository;
  46.     /** @var ProfileRBACRepository */
  47.     private $profileRBACRepository;
  48.     /** @var NotificationRepository */
  49.     private $notificationRepository;
  50.     /** @var ProfileService */
  51.     private $profileService;
  52.     /**
  53.      * @param AuthenticationService $authenticationService
  54.      * @param EventDispatcherInterface $eventDispatcher
  55.      * @param UserRepository $userRepository
  56.      * @param UserMailer $userMailer
  57.      * @param UserProfileRepository $userProfileRepository
  58.      * @param ProfileAccessLogRepository $profileAccessLogRepository
  59.      * @param ProfileRBACRepository $profileRBACRepository
  60.      * @param NotificationRepository $notificationRepository
  61.      * @Param ProfileService $profileService
  62.      */
  63.     public function __construct(
  64.         AuthenticationService $authenticationService,
  65.         EventDispatcherInterface $eventDispatcher,
  66.         UserRepository $userRepository,
  67.         UserMailer $userMailer,
  68.         UserProfileRepository $userProfileRepository,
  69.         ProfileAccessLogRepository $profileAccessLogRepository,
  70.         ProfileRBACRepository $profileRBACRepository,
  71.         NotificationRepository $notificationRepository,
  72.         ProfileService $profileService
  73.     )
  74.     {
  75.         $this->authenticationService $authenticationService;
  76.         $this->eventDispatcher $eventDispatcher;
  77.         $this->userRepository $userRepository;
  78.         $this->userMailer $userMailer;
  79.         $this->userProfileRepository $userProfileRepository;
  80.         $this->profileAccessLogRepository $profileAccessLogRepository;
  81.         $this->profileRBACRepository $profileRBACRepository;
  82.         $this->notificationRepository $notificationRepository;
  83.         $this->profileService $profileService;
  84.     }
  85.     /**
  86.      * @inheritDoc
  87.      */
  88.     public static function getSubscribedEvents(): array
  89.     {
  90.         return [
  91.             UserCreatedEvent::class     => 'onUserCreatedEvent',
  92.             UserUpdatedEvent::class     => 'onUserUpdatedEvent',
  93.             UserClosedEvent::class      => 'onUserClosedEvent',
  94.             UserSuspendedEvent::class   => 'onUserSuspendedEvent',
  95.             UserTerminatedEvent::class  => 'onUserTerminatedEvent',
  96.             UserDeletedEvent::class     => 'onUserDeletedEvent'
  97.         ];
  98.     }
  99.     /**
  100.      * @param UserCreatedEvent $event
  101.      * @return void
  102.      */
  103.     public function onUserCreatedEvent(UserCreatedEvent $event)
  104.     {
  105.         /** @var User $user */
  106.         $user $event->getUser();
  107.         $userForLog = ($event->getFrom() === Constants::MMPZ_REFERER) ? $this->authenticationService->getUser() : $user;
  108.         $this->eventDispatcher->dispatch(
  109.             new LogEvent(
  110.                 $event->getUser(),
  111.                 $userForLog,
  112.                 EventLog::TYPE_USER_CREATED
  113.             )
  114.         );
  115.     }
  116.     /**
  117.      * @param UserUpdatedEvent $event
  118.      * @return void
  119.      */
  120.     public function onUserUpdatedEvent(UserUpdatedEvent $event)
  121.     {
  122.         /** @var User $user */
  123.         $user $event->getUser();
  124.         $userForLog = ($event->getFrom() === Constants::MMPZ_REFERER) ? $this->authenticationService->getUser() : $user;
  125.         $this->eventDispatcher->dispatch(
  126.             new LogEvent(
  127.                 $event->getUser(),
  128.                 $userForLog,
  129.                 EventLog::TYPE_USER_UPDATED
  130.             )
  131.         );
  132.     }
  133.     /**
  134.      * @param UserUpdatedEvent $event
  135.      * @return void
  136.      */
  137.     public function onUserRevokeEvent(UserUpdatedEvent $event)
  138.     {
  139.         /** @var User $user */
  140.         $user $event->getUser();
  141.         $userForLog = ($event->getFrom() === Constants::MMPZ_REFERER) ? $this->authenticationService->getUser() : $user;
  142.         $this->eventDispatcher->dispatch(
  143.             new LogEvent(
  144.                 $event->getUser(),
  145.                 $userForLog,
  146.                 EventLog::TYPE_USER_REVOKED
  147.             )
  148.         );
  149.     }
  150.     /**
  151.      * @param UserClosedEvent $event
  152.      */
  153.     public function onUserClosedEvent(UserClosedEvent $event): void
  154.     {
  155.         /** @var User $user */
  156.         $user $event->getUser();
  157.         $user->setStatus(Constants::USER_CLOSE);
  158.         $this->userRepository->flush();
  159.         $this->eventDispatcher->dispatch(
  160.             new LogEvent(
  161.                 $event->getUser(),
  162.                 $this->authenticationService->getUser(),
  163.                 EventLog::TYPE_USER_CLOSED
  164.             )
  165.         );
  166.         $this->userMailer->sendUserEmails(
  167.             $user,
  168.             $this->authenticationService->getUser(),
  169.             $this->buildMessage($user$this->authenticationService->getUser(), Constants::USER_CLOSE)
  170.         );
  171.     }
  172.     /**
  173.      * @param UserSuspendedEvent $event
  174.      */
  175.     public function onUserSuspendedEvent(UserSuspendedEvent $event): void
  176.     {
  177.         /** @var User $user */
  178.         $user $event->getUser();
  179.         $user->setStatus(Constants::USER_SUSPEND);
  180.         $this->userRepository->flush();
  181.         $this->eventDispatcher->dispatch(
  182.             new LogEvent(
  183.                 $event->getUser(),
  184.                 $this->authenticationService->getUser(),
  185.                 EventLog::TYPE_USER_SUSPENDED
  186.             )
  187.         );
  188.         $this->userMailer->sendUserEmails(
  189.             $user,
  190.             $this->authenticationService->getUser(),
  191.             $this->buildMessage($user$this->authenticationService->getUser(), Constants::USER_SUSPEND)
  192.         );
  193.     }
  194.     /**
  195.      * @param UserTerminatedEvent $event
  196.      */
  197.     public function onUserTerminatedEvent(UserTerminatedEvent $event): void
  198.     {
  199.         /** @var User $user */
  200.         $user $event->getUser();
  201.         $user->setStatus(Constants::USER_TERMINATE);
  202.         $this->userRepository->flush();
  203.         $this->eventDispatcher->dispatch(
  204.             new LogEvent(
  205.                 $event->getUser(),
  206.                 $this->authenticationService->getUser(),
  207.                 EventLog::TYPE_USER_TERMINATED
  208.             )
  209.         );
  210.         $this->userMailer->sendUserEmails(
  211.             $user,
  212.             $this->authenticationService->getUser(),
  213.             $this->buildMessage($user$this->authenticationService->getUser(), Constants::USER_TERMINATE)
  214.         );
  215.     }
  216.     /**
  217.      * @param UserDeletedEvent $event
  218.      */
  219.     public function onUserDeletedEvent(UserDeletedEvent $event): void
  220.     {
  221.         /** @var User $user */
  222.         $user $event->getUser();
  223.         $userForLog = ($event->getFrom() === Constants::MMPZ_REFERER) ? $this->authenticationService->getUser() : $user;
  224.         $this->eventDispatcher->dispatch(
  225.             new LogEvent(
  226.                 $event->getUser(),
  227.                 $userForLog,
  228.                 EventLog::TYPE_USER_TERMINATED
  229.             )
  230.         );
  231.         $this->userMailer->sendUserEmails(
  232.             $user,
  233.             $userForLog,
  234.             $this->buildMessage($user$userForLogConstants::USER_DELETE)
  235.         );
  236.         $pofileRbacs $this->profileRBACRepository->findBy(['user' => $user]);
  237.         /** @var ProfileRBAC $pofileRbac */
  238.         foreach ($pofileRbacs as $pofileRbac) {
  239.             $this->profileRBACRepository->remove($pofileRbac);
  240.             $this->profileRBACRepository->flush();
  241.         }
  242.         $profileAccessLogs $this->profileAccessLogRepository->findBy(['user' => $user]);
  243.         /** @var ProfileAccessLog $profileAccessLog */
  244.         foreach ($profileAccessLogs as $profileAccessLog){
  245.             $this->profileAccessLogRepository->remove($profileAccessLog);
  246.             $this->profileAccessLogRepository->flush();
  247.         }
  248.         $notifications $this->notificationRepository->findBy(['user' => $user]);
  249.         /** @var Notification $notification */
  250.         foreach ($notifications as $notification){
  251.             $this->notificationRepository->remove($notification);
  252.             $this->notificationRepository->flush();
  253.         }
  254.     }
  255.     /**
  256.      * @param User $user
  257.      * @param User $responsible
  258.      * @param string $event
  259.      * @return string[]
  260.      */
  261.     private function buildMessage(User $userUser $responsiblestring $event)
  262.     {
  263.         if($event !== Constants::USER_DELETE){
  264.             $message = [
  265.                 'title'     => 'User " ' $user->getUserProfile()->getFirstName() ." ".$user->getUserProfile()->getLastName() . '" '$event,
  266.                 'message'   => "Dear ".$user->getUsername().", this user account now is {$event} status"
  267.             ];
  268.         }else{
  269.             $message = [
  270.                 'title'     => 'User " ' $user->getUserProfile()->getFirstName() ." ".$user->getUserProfile()->getLastName() . '" '$event,
  271.                 'message'   => "Dear ".$user->getUsername().", this user account now is deleted"
  272.             ];
  273.         }
  274.         $profile $this->profileService->findDefaultProfileForUser($user);
  275.         if(is_null($profile)){
  276.             $profile $this->profileService->getProfileByUrlName(Constants::PROFILE_SERVICE_URL_NAME);
  277.         }
  278.         $message['profile'] = $profile;
  279.         return $message;
  280.     }
  281. }