<?php
/**
* Copyright (C) SUEZ Smart Solutions - All Rights Reserved
* On’Connect Gateway Management, 2018
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* For the full copyright and license information, please report to the LICENSE CONTRACT.
*/
namespace Suez\NotificationBundle\Event;
use Suez\NotificationBundle\Manager\AlertConfigurationManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\User\UserInterface;
use TSMS\CoreBundle\Entity\Contract;
use TSMS\CoreBundle\Entity\Eservice;
class UserModificationEventSubscriber implements EventSubscriberInterface
{
const USER_CONTRACT_DISABLED = 'user.contract.disabled';
const USER_SERVICE_DISABLED = 'user.service.disabled';
protected $requestAlertManager;
protected $documentAlertManager;
public function __construct(
AlertConfigurationManagerInterface $requestAlertManager,
AlertConfigurationManagerInterface $documentAlertManager
) {
/*
* @todo: In SF >= 2.7, use a compiler pass to inject all managers.
*/
$this->requestAlertManager = $requestAlertManager;
$this->documentAlertManager = $documentAlertManager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
static::USER_CONTRACT_DISABLED => 'onUserContractDisabled',
static::USER_SERVICE_DISABLED => 'onUserServiceDisabled',
];
}
/**
* When a contract is removed from a user, all types alerts on this contract are disabled.
*
* @param GenericEvent $event event
*/
public function onUserContractDisabled(GenericEvent $event)
{
$subject = $event->getSubject();
if (empty($subject['contract']) || !($subject['contract'] instanceof Contract)) {
throw new \InvalidArgumentException('Contract instance expected');
}
if (empty($subject['user']) || !($subject['user'] instanceof UserInterface)) {
throw new \InvalidArgumentException('UserInterface expected');
}
$filter = [
'contract' => $subject['contract'],
'user' => $subject['user'],
];
$this->requestAlertManager->disableAllAlertsByFilter($filter);
$this->documentAlertManager->disableAllAlertsByFilter($filter);
}
/**
* When a service is removed from a user on a contract, alerts from this service for this contract are disabled.
*
* @param GenericEvent $event event
*/
public function onUserServiceDisabled(GenericEvent $event)
{
$subject = $event->getSubject();
if (empty($subject['contract']) || !($subject['contract'] instanceof Contract)) {
throw new \InvalidArgumentException('Contract instance expected');
}
if (empty($subject['user']) || !($subject['user'] instanceof UserInterface)) {
throw new \InvalidArgumentException('UserInterface expected');
}
if (empty($subject['service']) || !($subject['service'] instanceof Eservice)) {
throw new \InvalidArgumentException('Eservice expected');
}
$filter = [
'contract' => $subject['contract'],
'user' => $subject['user'],
];
switch ($subject['service']->getMnemonic()) {
case Eservice::OUR_DOCUMENTS_READ_MNEMONIC:
case Eservice::OUR_DOCUMENTS_WRITE_MNEMONIC:
$this->documentAlertManager->disableAllAlertsByFilter($filter);
break;
case Eservice::REQUEST_COLLECTIVITY:
$this->requestAlertManager->disableAllAlertsByFilter($filter);
break;
}
}
}