Slack notify
This commit is contained in:
52
src/EventSubscriber/SlackNotifySubscriber.php
Normal file
52
src/EventSubscriber/SlackNotifySubscriber.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use ApiPlatform\Symfony\EventListener\EventPriorities;
|
||||
use App\Helper\Slack;
|
||||
use App\Entity\Order;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Event\ViewEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\Notifier\ChatterInterface;
|
||||
|
||||
class SlackNotifySubscriber implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var Slack
|
||||
*/
|
||||
public $slack;
|
||||
|
||||
public function __construct(ChatterInterface $chatter)
|
||||
{
|
||||
$this->slack = new Slack($chatter);
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
KernelEvents::VIEW => ['sendSlack', EventPriorities::POST_WRITE],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ViewEvent $event
|
||||
* @return void
|
||||
*/
|
||||
public function sendSlack(ViewEvent $event): void
|
||||
{
|
||||
$order = $event->getControllerResult();
|
||||
$method = $event->getRequest()->getMethod();
|
||||
|
||||
//wenn es keine Bestellung ist oder es kein POST Request ist, dann return
|
||||
if (!$order instanceof Order || Request::METHOD_POST !== $method) {
|
||||
return;
|
||||
}
|
||||
|
||||
$msg = "Order {$order->getId()} has been created: {$order->getStatus()}";
|
||||
$this->slack->sendMessage($msg);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user