<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 25 wrz 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Arrangements\Subscriber;
use Crehler\Arrangements\Message\DeleteArrangementProductMessage;
use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface;
class ProductDeleteSubscriber implements EventSubscriberInterface
{
private MessageBusInterface $messageBus;
private CrehlerContextService $crehlerContext;
public function __construct(MessageBusInterface $messageBus, CrehlerContextService $crehlerContext)
{
$this->messageBus = $messageBus;
$this->crehlerContext = $crehlerContext;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => 'onKernelResponse',
];
}
public function onKernelResponse(ResponseEvent $event)
{
$uri = $event->getRequest()->getRequestUri();
$method = $event->getRequest()->getMethod();
if('DELETE' == $method)
{
$uri = explode("/", $uri);
if (in_array('api', $uri) && in_array('arrangement', $uri) && in_array('arrangement-products', $uri))
{
$arrangement = $uri[4];
$context = $event->getRequest()->attributes->get('sw-context');
$message = new DeleteArrangementProductMessage();
$message->withContext($context);
$message->setArrangementId($arrangement);
$this->messageBus->dispatch($message, [new TransportConfiguration(['topic' => 'crehlerarrangements'])]);
}
}
}
}