src/EventListener/CancellationInFoListener.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Gos\CancellationFoResponse;
  4. use App\Entity\Gos\OrderPart;
  5. use App\Event\CancellationInFoEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CancellationInFoListener implements EventSubscriberInterface
  9. {
  10.     private $em;
  11.     public function __construct(EntityManagerInterface $em)
  12.     {
  13.         $this->em $em;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             CancellationInFoEvent::CANCELLATION_SENT => 'onCancellationSent'
  19.         ];
  20.     }
  21.     public function onCancellationSent(CancellationInFoEvent $event): void
  22.     {
  23.         $orderPart $event->getOrderPart();
  24.         $response $event->getResponse();
  25.         $decodedResponse json_decode($responsetrue);
  26.         if (!$orderPart->getCancellationFoResponses()->isEmpty())
  27.         {
  28.             /** @var CancellationFoResponse $cancellationResponse */
  29.             $cancellationFoResponse $orderPart->getCancellationFoResponses()->first();
  30.         }
  31.         $cancellationFoResponse $cancellationFoResponse ?? new CancellationFoResponse();
  32.         $cancellationFoResponse->setOrderPart($orderPart);
  33.         $cancellationFoResponse->setUser($orderPart->getOrders()->getUser());
  34.         $cancellationFoResponse->setBody($response);
  35.         $cancellationFoResponse->setIsSuccessfully(true);
  36.         if (empty($decodedResponse) || array_key_exists('error'$decodedResponse))
  37.         {
  38.             $this->handleCancellationFailure($event$orderPart$response$cancellationFoResponse);
  39.         }
  40.         else
  41.         {
  42.             $cancellationFoResponse->setFailureNumber(null);
  43.         }
  44.         $this->em->persist($cancellationFoResponse);
  45.         $this->em->flush();
  46.     }
  47.     private function handleCancellationFailure(
  48.         CancellationInFoEvent $event,
  49.         OrderPart $orderPart,
  50.         string $response,
  51.         CancellationFoResponse $cancellationFoResponse
  52.     ): void
  53.     {
  54.         $failureNumber $cancellationFoResponse->getFailureNumber();
  55.         if (++$failureNumber >= 5)
  56.         {
  57.             $event->setEmailMessage($orderPart->getOrdTran() . ' - ' $failureNumber ' nieudana rezygnacja w FO');
  58.         }
  59.         $cancellationFoResponse->setFailureNumber($failureNumber);
  60.         $cancellationFoResponse->setIsSuccessfully(false);
  61.     }
  62. }