<?php
namespace App\Listener;
use App\Event\NewCommentEvent;
use App\Manager\NotificationManager;
use App\Manager\UserRightsManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class NewCommentSubscriber
*/
class NewCommentSubscriber implements EventSubscriberInterface
{
public const COMMENT_POSTED_DONE = 'comment.posted.done';
/**
* @var NotificationManager
*/
protected $notificationManager;
/**
* @var LoggerInterface
*/
protected $logger;
public function __construct(
NotificationManager $notificationManager,
LoggerInterface $logger
) {
$this->notificationManager = $notificationManager;
$this->logger = $logger;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
static::COMMENT_POSTED_DONE => 'onCommentPostedDone',
];
}
/**
* Bootstraps status change hooks.
*
* @param NewCommentEvent $event
*
* @return void
*/
public function onCommentPostedDone(NewCommentEvent $event): void
{
$request = $event->getSubject();
$commentRole = $request->getCurrentStatus()->getCommentRole();
if (empty($commentRole)) {
$notification = $this->notificationManager->getNewCommentNotification($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)) {
$this->notifySuezNewStatus($event);
}
}
public function notifySuezNewStatus(NewCommentEvent $event): void
{
$request = $event->getSubject();
$notification = $this->notificationManager->getNewCommentNotification($request);
$this->logger->info('New comment 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);
}
}