src/Suez/NotificationBundle/Event/UserModificationEventSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT.
  8. */
  9. namespace Suez\NotificationBundle\Event;
  10. use Suez\NotificationBundle\Manager\AlertConfigurationManagerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\EventDispatcher\GenericEvent;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use TSMS\CoreBundle\Entity\Contract;
  15. use TSMS\CoreBundle\Entity\Eservice;
  16. class UserModificationEventSubscriber implements EventSubscriberInterface
  17. {
  18.     const USER_CONTRACT_DISABLED 'user.contract.disabled';
  19.     const USER_SERVICE_DISABLED  'user.service.disabled';
  20.     protected $requestAlertManager;
  21.     protected $documentAlertManager;
  22.     public function __construct(
  23.         AlertConfigurationManagerInterface $requestAlertManager,
  24.         AlertConfigurationManagerInterface $documentAlertManager
  25.     ) {
  26.         /*
  27.          * @todo: In SF >= 2.7, use a compiler pass to inject all managers.
  28.          */
  29.         $this->requestAlertManager  $requestAlertManager;
  30.         $this->documentAlertManager $documentAlertManager;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             static::USER_CONTRACT_DISABLED => 'onUserContractDisabled',
  39.             static::USER_SERVICE_DISABLED  => 'onUserServiceDisabled',
  40.         ];
  41.     }
  42.     /**
  43.      * When a contract is removed from a user, all types alerts on this contract are disabled.
  44.      *
  45.      * @param GenericEvent $event event
  46.      */
  47.     public function onUserContractDisabled(GenericEvent $event)
  48.     {
  49.         $subject  $event->getSubject();
  50.         if (empty($subject['contract']) || !($subject['contract'] instanceof Contract)) {
  51.             throw new \InvalidArgumentException('Contract instance expected');
  52.         }
  53.         if (empty($subject['user']) || !($subject['user'] instanceof UserInterface)) {
  54.             throw new \InvalidArgumentException('UserInterface expected');
  55.         }
  56.         $filter = [
  57.             'contract' => $subject['contract'],
  58.             'user'     => $subject['user'],
  59.         ];
  60.         $this->requestAlertManager->disableAllAlertsByFilter($filter);
  61.         $this->documentAlertManager->disableAllAlertsByFilter($filter);
  62.     }
  63.     /**
  64.      * When a service is removed from a user on a contract, alerts from this service for this contract are disabled.
  65.      *
  66.      * @param GenericEvent $event event
  67.      */
  68.     public function onUserServiceDisabled(GenericEvent $event)
  69.     {
  70.         $subject  $event->getSubject();
  71.         if (empty($subject['contract']) || !($subject['contract'] instanceof Contract)) {
  72.             throw new \InvalidArgumentException('Contract instance expected');
  73.         }
  74.         if (empty($subject['user']) || !($subject['user'] instanceof UserInterface)) {
  75.             throw new \InvalidArgumentException('UserInterface expected');
  76.         }
  77.         if (empty($subject['service']) || !($subject['service'] instanceof Eservice)) {
  78.             throw new \InvalidArgumentException('Eservice expected');
  79.         }
  80.         $filter = [
  81.             'contract' => $subject['contract'],
  82.             'user'     => $subject['user'],
  83.         ];
  84.         switch ($subject['service']->getMnemonic()) {
  85.             case Eservice::OUR_DOCUMENTS_READ_MNEMONIC:
  86.             case Eservice::OUR_DOCUMENTS_WRITE_MNEMONIC:
  87.                 $this->documentAlertManager->disableAllAlertsByFilter($filter);
  88.                 break;
  89.             case Eservice::REQUEST_COLLECTIVITY:
  90.                 $this->requestAlertManager->disableAllAlertsByFilter($filter);
  91.                 break;
  92.         }
  93.     }
  94. }