src/UniSport/Bundle/AppBundle/EventListener/UsizySubscriber.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace UniSport\Bundle\AppBundle\EventListener;
  14. use Psr\Container\ContainerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use WellCommerce\Bundle\AppBundle\Entity\AddressInterface;
  17. use WellCommerce\Bundle\AppBundle\Entity\ClientBillingAddress;
  18. use WellCommerce\Bundle\AppBundle\Entity\ClientContactDetails;
  19. use WellCommerce\Bundle\AppBundle\Entity\Media;
  20. use WellCommerce\Bundle\AppBundle\Entity\Shop;
  21. use WellCommerce\Bundle\CatalogBundle\Entity\Producer;
  22. use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
  23. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  24. use WellCommerce\Bundle\OrderBundle\DataSet\Admin\OrderStatusDataSet;
  25. use WellCommerce\Bundle\OrderBundle\Entity\Order;
  26. use WellCommerce\Bundle\OrderBundle\Entity\OrderProduct;
  27. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
  28. use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
  29. use WellCommerce\Component\Form\Event\FormEvent;
  30. /**
  31.  * Class Usizy Subscriber
  32.  *
  33.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  34.  */
  35. class UsizySubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
  36. {
  37.     protected const FEATURE_NAME_SIZE 'Rozmiar';
  38.     protected const USIZY_API_ENDPOINT 'https://usizy.com/external/event';
  39.     protected OrderStatusDataSet $dataSet;
  40.     public function __construct(ContainerInterface $locatorOrderStatusDataSet $dataSet)
  41.     {
  42.         parent::__construct($locator);
  43.         $this->dataSet $dataSet;
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             'admin.shop.pre_form_init'         => ['onShopFormAdminInit'],
  49.             'order_status_history.post_create' => ['onOrderStatusHistoryPostCreate'0],
  50.         ];
  51.     }
  52.     public function onShopFormAdminInit(FormEvent $event)
  53.     {
  54.         $resource $event->getResource();
  55.         if ($resource instanceof Shop) {
  56.             $form    $event->getForm();
  57.             $builder $event->getFormBuilder();
  58.             $usizyData $form->addChild($builder->getElement('nested_fieldset', [
  59.                 'name'  => 'usizy_data',
  60.                 'label' => 'usizy.fieldset.settings',
  61.             ]));
  62.             $usizyData->addChild($builder->getElement('text_field', [
  63.                 'name'  => 'usizyApiEcommerce',
  64.                 'label' => 'usizy.label.ecommerce',
  65.             ]));
  66.             
  67.             $usizyData->addChild($builder->getElement('checkbox', [
  68.                 'name'  => 'usizyApiEnabled',
  69.                 'label' => 'usizy.label.enabled',
  70.             ]));
  71.             $orderStatuses $this->dataSet->getResult('select', [], ['default_option' => '---']);
  72.             
  73.             $usizyData->addChild($builder->getElement('select', [
  74.                 'name'        => 'usizyStatusOrderCancelled',
  75.                 'label'       => 'usizy.label.order_status_cancelled',
  76.                 'options'     => $orderStatuses,
  77.                 'transformer' => $builder->getRepositoryTransformer('entity'OrderStatus::class),
  78.             ]));
  79.             $usizyData->addChild($builder->getElement('select', [
  80.                 'name'        => 'usizyStatusOrderReturned',
  81.                 'label'       => 'usizy.label.order_status_returned',
  82.                 'options'     => $orderStatuses,
  83.                 'transformer' => $builder->getRepositoryTransformer('entity'OrderStatus::class),
  84.             ]));
  85.         }
  86.     }
  87.     public function onOrderStatusHistoryPostCreate(EntityEvent $event)
  88.     {
  89.         $history $event->getEntity();
  90.         if ($history instanceof OrderStatusHistory) {
  91.             $order  $history->getOrder();
  92.             $shop   $order->getShop();
  93.             $status $history->getOrderStatus();
  94.             if ($shop->isUsizyApiEnabled()) {
  95.                 $usizyStatusCancelled $shop->getUsizyStatusOrderCancelled();
  96.                 if ($usizyStatusCancelled instanceof OrderStatus) {
  97.                     if ($status->getId() === $usizyStatusCancelled->getId()) {
  98.                         $this->usizySendStatusCancelled($shop$order);
  99.                     }
  100.                 }
  101.                 $usizyStatusReturned $shop->getUsizyStatusOrderReturned();
  102.                 if ($usizyStatusReturned instanceof OrderStatus) {
  103.                     if ($status->getId() === $usizyStatusReturned->getId()) {
  104.                         $this->usizySendStatusReturned($shop$order);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     protected function sendJson($url$query)
  111.     {
  112.         $ch curl_init$url );
  113.         $payload json_encode($query);
  114.         curl_setopt$chCURLOPT_POSTFIELDS$payload );
  115.         curl_setopt$chCURLOPT_HTTPHEADER, ['Content-Type:application/json']);
  116.         curl_setopt$chCURLOPT_RETURNTRANSFERtrue );
  117.         $result curl_exec($ch);
  118.         curl_close($ch);
  119.         return $result;
  120.     }
  121.     private function usizySendStatusCancelled(Shop $shopOrder $order)
  122.     {
  123.         $products = [];
  124.         $sizes    = [];
  125.         $orderProducts $order->getProducts();
  126.         if (count($orderProducts) > 0) {
  127.             foreach ($order->getProducts() as $orderProduct) {
  128.                 $product $orderProduct->getProduct();
  129.                 $size $product->getFeatures()->get(self::FEATURE_NAME_SIZE);
  130.                 if (null !== $size) {
  131.                     $products[] = $product->translate()->getName();
  132.                     $sizes[]    = $size;
  133.                 }
  134.             }
  135.             $query = [
  136.                 'event' => 'CANCEL',
  137.                 'ecommerce' => $shop->getUsizyApiEcommerce(),
  138.                 'order' => $order->getId(),
  139.                 'order_date' => $order->getCreatedAt()->format(\DateTimeInterface::ISO8601),
  140.                 'product' => $products,
  141.                 'date' => (new \DateTime())->format(\DateTimeInterface::ISO8601),
  142.                 'ecomm_size' => $sizes
  143.             ];
  144.             $res $this->sendJson(self::USIZY_API_ENDPOINT$query);
  145.         }
  146.     }
  147.     private function usizySendStatusReturned(Shop $shopOrder $order)
  148.     {
  149.         $products = [];
  150.         $sizes    = [];
  151.         $orderProducts $order->getProducts();
  152.         if (count($orderProducts) > 0) {
  153.             foreach ($order->getProducts() as $orderProduct) {
  154.                 $product $orderProduct->getProduct();
  155.                 $size $product->getFeatures()->get(self::FEATURE_NAME_SIZE);
  156.                 if (null !== $size) {
  157.                     $products[] = $product->translate()->getName();
  158.                     $sizes[]    = $size;
  159.                 }
  160.             }
  161.             $query = [
  162.                 'event' => 'RETURN',
  163.                 'ecommerce' => $shop->getUsizyApiEcommerce(),
  164.                 'order' => $order->getId(),
  165.                 'order_date' => $order->getCreatedAt()->format(\DateTimeInterface::ISO8601),
  166.                 'product' => $products,
  167.                 'date' => (new \DateTime())->format(\DateTimeInterface::ISO8601),
  168.                 'ecomm_size' => $sizes
  169.             ];
  170.             $res $this->sendJson(self::USIZY_API_ENDPOINT$query);
  171.         }
  172.     }
  173. }