<?php
namespace App\EventSubscriber;
use App\Entity\MGC\MGCOrder;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Exception\BaseException;
use EasyCorp\Bundle\EasyAdminBundle\Exception\ExceptionContext;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Response;
class EasyAdminPreUpdateSubscriber implements EventSubscriberInterface
{
private $mgcUrl;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var EntityManagerInterface
*/
private $em;
private $mgcSecret;
/**
* EasyAdminPreUpdateSubscriber constructor.
*
* @param $mgcUrl
* @param $mgcSecret
* @param SerializerInterface $serializer
* @param EntityManagerInterface $em
*/
public function __construct($mgcUrl, $mgcSecret, SerializerInterface $serializer, EntityManagerInterface $em)
{
$this->mgcUrl = $mgcUrl;
$this->serializer = $serializer;
$this->em = $em;
$this->mgcSecret = $mgcSecret;
}
public function onEasyAdminPreUpdate($event)
{
/** @var GenericEvent $event */
$subject = $event->getSubject();
if ($subject instanceof MGCOrder) {
$client = HttpClient::create();
/** @var MGCOrder $subject */
$response = $client->request(
'PATCH',
"$this->mgcUrl/orders/{$subject->getInternalId()}/status?secret={$this->mgcSecret}",
[
'json' => [
'status' => $subject->getStatus(),
],
]
);
if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new BaseException((new ExceptionContext('При обновлении статус возникла ошибка. Попробуйте позже', '', [], $response->getStatusCode())));
}
}
}
public static function getSubscribedEvents()
{
return [
'easy_admin.pre_update' => 'onEasyAdminPreUpdate',
];
}
}