<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace UniSport\Bundle\AppBundle\EventListener;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WellCommerce\Bundle\AppBundle\Entity\AddressInterface;
use WellCommerce\Bundle\AppBundle\Entity\ClientBillingAddress;
use WellCommerce\Bundle\AppBundle\Entity\ClientContactDetails;
use WellCommerce\Bundle\AppBundle\Entity\Media;
use WellCommerce\Bundle\AppBundle\Entity\Shop;
use WellCommerce\Bundle\CatalogBundle\Entity\Producer;
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractServiceSubscriber;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\OrderBundle\DataSet\Admin\OrderStatusDataSet;
use WellCommerce\Bundle\OrderBundle\Entity\Order;
use WellCommerce\Bundle\OrderBundle\Entity\OrderProduct;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class Usizy Subscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class UsizySubscriber extends AbstractServiceSubscriber implements EventSubscriberInterface
{
protected const FEATURE_NAME_SIZE = 'Rozmiar';
protected const USIZY_API_ENDPOINT = 'https://usizy.com/external/event';
protected OrderStatusDataSet $dataSet;
public function __construct(ContainerInterface $locator, OrderStatusDataSet $dataSet)
{
parent::__construct($locator);
$this->dataSet = $dataSet;
}
public static function getSubscribedEvents()
{
return [
'admin.shop.pre_form_init' => ['onShopFormAdminInit'],
'order_status_history.post_create' => ['onOrderStatusHistoryPostCreate', 0],
];
}
public function onShopFormAdminInit(FormEvent $event)
{
$resource = $event->getResource();
if ($resource instanceof Shop) {
$form = $event->getForm();
$builder = $event->getFormBuilder();
$usizyData = $form->addChild($builder->getElement('nested_fieldset', [
'name' => 'usizy_data',
'label' => 'usizy.fieldset.settings',
]));
$usizyData->addChild($builder->getElement('text_field', [
'name' => 'usizyApiEcommerce',
'label' => 'usizy.label.ecommerce',
]));
$usizyData->addChild($builder->getElement('checkbox', [
'name' => 'usizyApiEnabled',
'label' => 'usizy.label.enabled',
]));
$orderStatuses = $this->dataSet->getResult('select', [], ['default_option' => '---']);
$usizyData->addChild($builder->getElement('select', [
'name' => 'usizyStatusOrderCancelled',
'label' => 'usizy.label.order_status_cancelled',
'options' => $orderStatuses,
'transformer' => $builder->getRepositoryTransformer('entity', OrderStatus::class),
]));
$usizyData->addChild($builder->getElement('select', [
'name' => 'usizyStatusOrderReturned',
'label' => 'usizy.label.order_status_returned',
'options' => $orderStatuses,
'transformer' => $builder->getRepositoryTransformer('entity', OrderStatus::class),
]));
}
}
public function onOrderStatusHistoryPostCreate(EntityEvent $event)
{
$history = $event->getEntity();
if ($history instanceof OrderStatusHistory) {
$order = $history->getOrder();
$shop = $order->getShop();
$status = $history->getOrderStatus();
if ($shop->isUsizyApiEnabled()) {
$usizyStatusCancelled = $shop->getUsizyStatusOrderCancelled();
if ($usizyStatusCancelled instanceof OrderStatus) {
if ($status->getId() === $usizyStatusCancelled->getId()) {
$this->usizySendStatusCancelled($shop, $order);
}
}
$usizyStatusReturned = $shop->getUsizyStatusOrderReturned();
if ($usizyStatusReturned instanceof OrderStatus) {
if ($status->getId() === $usizyStatusReturned->getId()) {
$this->usizySendStatusReturned($shop, $order);
}
}
}
}
}
protected function sendJson($url, $query)
{
$ch = curl_init( $url );
$payload = json_encode($query);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
private function usizySendStatusCancelled(Shop $shop, Order $order)
{
$products = [];
$sizes = [];
$orderProducts = $order->getProducts();
if (count($orderProducts) > 0) {
foreach ($order->getProducts() as $orderProduct) {
$product = $orderProduct->getProduct();
$size = $product->getFeatures()->get(self::FEATURE_NAME_SIZE);
if (null !== $size) {
$products[] = $product->translate()->getName();
$sizes[] = $size;
}
}
$query = [
'event' => 'CANCEL',
'ecommerce' => $shop->getUsizyApiEcommerce(),
'order' => $order->getId(),
'order_date' => $order->getCreatedAt()->format(\DateTimeInterface::ISO8601),
'product' => $products,
'date' => (new \DateTime())->format(\DateTimeInterface::ISO8601),
'ecomm_size' => $sizes
];
$res = $this->sendJson(self::USIZY_API_ENDPOINT, $query);
}
}
private function usizySendStatusReturned(Shop $shop, Order $order)
{
$products = [];
$sizes = [];
$orderProducts = $order->getProducts();
if (count($orderProducts) > 0) {
foreach ($order->getProducts() as $orderProduct) {
$product = $orderProduct->getProduct();
$size = $product->getFeatures()->get(self::FEATURE_NAME_SIZE);
if (null !== $size) {
$products[] = $product->translate()->getName();
$sizes[] = $size;
}
}
$query = [
'event' => 'RETURN',
'ecommerce' => $shop->getUsizyApiEcommerce(),
'order' => $order->getId(),
'order_date' => $order->getCreatedAt()->format(\DateTimeInterface::ISO8601),
'product' => $products,
'date' => (new \DateTime())->format(\DateTimeInterface::ISO8601),
'ecomm_size' => $sizes
];
$res = $this->sendJson(self::USIZY_API_ENDPOINT, $query);
}
}
}