forked from elefan-grenoble/gestion-compte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Créneaux : Alertes : refactoring de l'envoi via l'EmailingEventListen…
…er & MattermostEventListener (elefan-grenoble#1057) * Create MattermostEventListener & ShiftAlertEvent * Seperate ShiftAlertsEvent & ShiftAlertsMattermostEvent
- Loading branch information
1 parent
a8a4147
commit 92a36d8
Showing
6 changed files
with
236 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace AppBundle\Event; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class ShiftAlertsEvent extends Event | ||
{ | ||
const NAME = 'shift.alerts'; | ||
|
||
private $alerts; | ||
private $date; | ||
private $template; | ||
private $recipients; | ||
|
||
public function __construct(array $alerts, \DateTime $date, $template = null, $recipients = null) | ||
{ | ||
$this->alerts = $alerts; | ||
$this->date = $date; | ||
$this->template = $template; | ||
$this->recipients = $recipients; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAlerts() | ||
{ | ||
return $this->alerts; | ||
} | ||
|
||
/** | ||
* @return \DateTime | ||
*/ | ||
public function getDate() | ||
{ | ||
return $this->date; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getTemplate() | ||
{ | ||
return $this->template; | ||
} | ||
|
||
/** | ||
* @return array|null | ||
*/ | ||
public function getRecipients() | ||
{ | ||
return $this->recipients; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace AppBundle\Event; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class ShiftAlertsMattermostEvent extends Event | ||
{ | ||
const NAME = 'shift.alerts.mattermost'; | ||
|
||
private $alerts; | ||
private $date; | ||
private $template; | ||
private $mattermost_hook_url; | ||
|
||
public function __construct(array $alerts, \DateTime $date, $template = null, $mattermost_hook_url = null) | ||
{ | ||
$this->alerts = $alerts; | ||
$this->date = $date; | ||
$this->template = $template; | ||
$this->mattermost_hook_url = $mattermost_hook_url; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAlerts() | ||
{ | ||
return $this->alerts; | ||
} | ||
|
||
/** | ||
* @return \DateTime | ||
*/ | ||
public function getDate() | ||
{ | ||
return $this->date; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getTemplate() | ||
{ | ||
return $this->template; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getMattermostHookUrl() | ||
{ | ||
return $this->mattermost_hook_url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace AppBundle\EventListener; | ||
|
||
use AppBundle\Event\ShiftAlertsMattermostEvent; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Monolog\Logger; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
class MattermostEventListener | ||
{ | ||
protected $em; | ||
protected $logger; | ||
protected $container; | ||
|
||
public function __construct(EntityManagerInterface $entityManager, Logger $logger, Container $container) | ||
{ | ||
$this->em = $entityManager; | ||
$this->logger = $logger; | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* @param ShiftAlertsMattermostEvent $event | ||
* @throws \Exception | ||
*/ | ||
public function onShiftAlerts(ShiftAlertsMattermostEvent $event) | ||
{ | ||
$this->logger->info("Mattermost Listener: onShiftAlerts"); | ||
|
||
$alerts = $event->getAlerts(); | ||
$date = $event->getDate(); | ||
|
||
if ($alerts && $event->getMattermostHookUrl()) { | ||
$template = null; | ||
$dynamicContent = $this->em->getRepository('AppBundle:DynamicContent')->findOneByCode($event->getTemplate()); | ||
if ($dynamicContent) { | ||
$template = $this->container->get('twig')->createTemplate($dynamicContent->getContent()); | ||
} else { | ||
$template = 'markdown/shift_alerts_default.md.twig'; | ||
} | ||
$content = $this->container->get('twig')->render( | ||
$template, | ||
array('alerts' => $alerts, 'date' => $date) | ||
); | ||
|
||
$client = HttpClient::create(); | ||
$response = $client->request('POST', $event->getMattermostHookUrl(), [ | ||
'json' => ['text' => $content] | ||
]); | ||
} | ||
} | ||
} |