src/EventSubscriber/Shop/OrderExtension/Bonus/WybieramyDomBonusSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\Shop\OrderExtension\Bonus;
  4. use App\Entity\Shop\Payment;
  5. use App\Event\Shop\OrderFormCreateAfterEvent;
  6. use App\Event\Shop\OrderPlaceAfterEvent;
  7. use App\Event\Shop\PaymentStatusChangeAfterEvent;
  8. use App\Exception\RuntimeException;
  9. use App\Service\UkSlaveApi;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Exception;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  15. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  16. use Symfony\Component\Serializer\Serializer;
  17. class WybieramyDomBonusSubscriber implements EventSubscriberInterface
  18. {
  19.     protected const UK_SLAVE_ID 'b1afc8b37b4ca097a3df2c445e4fc6ea';
  20.     protected UkSlaveApi $api;
  21.     protected EntityManagerInterface $em;
  22.     public function __construct(UkSlaveApi $apiEntityManagerInterface $em)
  23.     {
  24.         $this->api $api;
  25.         $this->em $em;
  26.     }
  27.     public function addBonusField(OrderFormCreateAfterEvent $event): void
  28.     {
  29.         $form $event->getForm();
  30.         $form->add('bonus'ChoiceType::class, [
  31.             'choices' => [
  32.                 'Wersja Elektroniczna (BEZPŁATNIE)' => 1,
  33.                 'Wersja Drukowana (BEZPŁATNIE)' => 2,
  34.                 'Nie, dziękuje' => 0,
  35.             ],
  36.             'expanded' => true,
  37.             'multiple' => false,
  38.         ]);
  39.     }
  40.     public function addBonusInfoToOrder(OrderPlaceAfterEvent $event): void
  41.     {
  42.         if (null !== $bonus $event->getExtraFormData()['bonus'] ?? null) {
  43.             // Add a bonus info to an order
  44.             $order $event->getOrder();
  45.             $order->setParam('bonus'$bonus);
  46.             $order->setParam('bonus_sent'false);
  47.             try {
  48.                 $this->em->persist($order);
  49.                 $this->em->flush();
  50.             } catch (Exception $e) {
  51.                 // TODO: Log
  52.             }
  53.         }
  54.     }
  55.     public function processBonus(PaymentStatusChangeAfterEvent $event): void
  56.     {
  57.         // Process only when a payment was successfull
  58.         if ($event->getStatus() !== Payment::STATUS_SUCCESS)
  59.             return;
  60.         $order $event->getPayment()->getShopOrder();
  61.         $bonusId $order->getParam('bonus');
  62.         if (in_array($bonusId, [12], true)) {
  63.             try {
  64.                 $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
  65.                 $data = [
  66.                     'json' => $serializer->serialize([
  67.                         'order' => [
  68.                             'id' => $order->getId(),
  69.                             'addressDelivery' => $order->getAddressDelivery(),
  70.                             'addressInvoice' => $order->getAddressInvoice(),
  71.                             'comment' => $order->getComment(),
  72.                             'acceptance' => $order->getAcceptance(),
  73.                             'acceptanceMarketing' => $order->getAcceptanceMarketing(),
  74.                         ]
  75.                     ], 'json'),
  76.                 ];
  77.                 $response $this->api->request(self::UK_SLAVE_ID$data);
  78.                 // Validate a response
  79.                 if (array_key_exists('error'$response) || (array_key_exists('data'$response) && empty($response['data'])))
  80.                     throw new RuntimeException('UK Slave Error: ' . ($response['error'] ?? 'empty body'));
  81.                 // Mark a bonus as sent
  82.                 $order->setParam('bonus_sent'true);
  83.                 $this->em->persist($order);
  84.                 $this->em->flush();
  85.             } catch (Exception $e) {
  86.                 dump($e);
  87.                 // TODO: log
  88.             }
  89.         }
  90.     }
  91.     public static function getSubscribedEvents(): array
  92.     {
  93.         return [
  94.             OrderFormCreateAfterEvent::NAME => [
  95.                 ['addBonusField'1000],
  96.             ],
  97.             OrderPlaceAfterEvent::NAME => [
  98.                 ['addBonusInfoToOrder'1000],
  99.             ],
  100.             PaymentStatusChangeAfterEvent::NAME => [
  101.                 ['processBonus'1000],
  102.             ],
  103.         ];
  104.     }
  105. }