src/EventSubscriber/EasyAdminPreUpdateSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\MGC\MGCOrder;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Exception\BaseException;
  6. use EasyCorp\Bundle\EasyAdminBundle\Exception\ExceptionContext;
  7. use JMS\Serializer\SerializerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\EventDispatcher\GenericEvent;
  10. use Symfony\Component\HttpClient\HttpClient;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class EasyAdminPreUpdateSubscriber implements EventSubscriberInterface
  13. {
  14.     private $mgcUrl;
  15.     /**
  16.      * @var SerializerInterface
  17.      */
  18.     private $serializer;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $em;
  23.     private $mgcSecret;
  24.     /**
  25.      * EasyAdminPreUpdateSubscriber constructor.
  26.      *
  27.      * @param $mgcUrl
  28.      * @param $mgcSecret
  29.      * @param SerializerInterface    $serializer
  30.      * @param EntityManagerInterface $em
  31.      */
  32.     public function __construct($mgcUrl$mgcSecretSerializerInterface $serializerEntityManagerInterface $em)
  33.     {
  34.         $this->mgcUrl $mgcUrl;
  35.         $this->serializer $serializer;
  36.         $this->em $em;
  37.         $this->mgcSecret $mgcSecret;
  38.     }
  39.     public function onEasyAdminPreUpdate($event)
  40.     {
  41.         /** @var GenericEvent $event */
  42.         $subject $event->getSubject();
  43.         if ($subject instanceof MGCOrder) {
  44.             $client HttpClient::create();
  45.             /** @var MGCOrder $subject */
  46.             $response $client->request(
  47.                 'PATCH',
  48.                 "$this->mgcUrl/orders/{$subject->getInternalId()}/status?secret={$this->mgcSecret}",
  49.                 [
  50.                     'json' => [
  51.                         'status' => $subject->getStatus(),
  52.                     ],
  53.                 ]
  54.             );
  55.             if (Response::HTTP_OK !== $response->getStatusCode()) {
  56.                 throw new BaseException((new ExceptionContext('При обновлении статус возникла ошибка. Попробуйте позже''', [], $response->getStatusCode())));
  57.             }
  58.         }
  59.     }
  60.     public static function getSubscribedEvents()
  61.     {
  62.         return [
  63.             'easy_admin.pre_update' => 'onEasyAdminPreUpdate',
  64.         ];
  65.     }
  66. }