<?php
namespace App\Listener;
use App\Event\NewDocumentEvent;
use App\Manager\NotificationManager;
use App\Manager\UserRightsManagerInterface;
use App\Model\UserRequest\RequestModel;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AttachmentPostedSubscriber implements EventSubscriberInterface
{
public const ATTACHMENT_POSTED_DONE = 'attachement.posted.done';
public const STATUS_BLOCKING_NOTIF = [
RequestModel::STATUS_DRAFT,
RequestModel::STATUS_TO_QUALIFY,
RequestModel::STATUS_TO_SPECIFY,
];
/**
* @var NotificationManager
*/
protected $notificationManager;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* AttachmentPostedSubscriber constructor.
*
* @param NotificationManager $notificationManager
* @param LoggerInterface $logger
*/
public function __construct(NotificationManager $notificationManager, LoggerInterface $logger)
{
$this->notificationManager = $notificationManager;
$this->logger = $logger;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
static::ATTACHMENT_POSTED_DONE => 'onAttachmentPostedDone',
];
}
/**
* Bootstraps status change hooks.
*
* @param NewDocumentEvent $event
*
* @return void
*/
public function onAttachmentPostedDone(NewDocumentEvent $event): void
{
$request = $event->getSubject();
$targetStatus = $event->getArgument('targetStatus');
if (!in_array($targetStatus, static::STATUS_BLOCKING_NOTIF, true)) {
$notification = $this->notificationManager->getNewDocumentNotification($request);
$this->notificationManager->notify($notification);
// if the user isn't from Suez organisation, a notification should be sent to Suez to let them know the action took place
$userRoles = $event->getArgument('userRoles');
if (!in_array(UserRightsManagerInterface::ROLE_SUEZ, $userRoles, true)) {
$this->notifySuezNewStatus($event);
}
}
}
public function notifySuezNewStatus(NewDocumentEvent $event): void
{
$request = $event->getSubject();
$notification = $this->notificationManager->getNewDocumentNotification($request);
$this->logger->info('New attachment from client user. Pushing notification to Asteo');
// Original email will be replaced by the consumer dedicated to pushing notifications to Suez.
// and we push
$this->notificationManager->notifyPce($notification);
}
}