<?php
namespace K3n\K3nAgrilityConnector\Subscriber;
use K3n\K3nAgrilityConnector\Services\AgrilityApiHandler;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private $orderRepository;
/**
* @var EntityRepositoryInterface
*/
private $categoryRepository;
/**
* @var AgrilityApiHandler
*/
private $agrilityApiHandler;
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(
EntityRepositoryInterface $orderRepository,
EntityRepositoryInterface $categoryRepository,
AgrilityApiHandler $agrilityApiHandler,
SystemConfigService $systemConfigService
) {
$this->orderRepository = $orderRepository;
$this->categoryRepository = $categoryRepository;
$this->agrilityApiHandler = $agrilityApiHandler;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
];
}
public function onOrderWritten(EntityWrittenEvent $event)
{
$writeResults = $event->getWriteResults();
foreach ($writeResults as $writeResult) {
$entityName = $writeResult->getEntityName();
$operation = $writeResult->getOperation();
if ($entityName == OrderDefinition::ENTITY_NAME && $operation == EntityWriteResult::OPERATION_INSERT) {
$orderId = $writeResult->getPayload()['id'] ?? null;
if ($orderId) {
$context = $event->getContext();
$order = $this->getOrderById($orderId, $context);
if ($order) {
if ($order->getVersionId() == Defaults::LIVE_VERSION) {
$agrilityProduct = $this->systemConfigService->get( 'K3nAgrilityConnector.config.agrilityProduct');
$lineItems = $order->getLineItems()->getElements();
foreach ($lineItems as $lineItem) {
if($lineItem->getType() == LineItem::PRODUCT_LINE_ITEM_TYPE) {
$product = $lineItem->getProduct();
if($agrilityProduct == $product->getId()) {
$customer = $order->getOrderCustomer()->getCustomer();
$this->agrilityApiHandler->createUpdateFarmer($customer, $context);
break;
}
}
}
}
}
}
}
}
}
private function getOrderById(string $orderId, Context $context): ?OrderEntity
{
$criteria = new Criteria([$orderId]);
$criteria->addAssociation('lineItems');
$criteria->addAssociation('lineItems.product');
$criteria->addAssociation('orderCustomer.customer');
$criteria->addAssociation('orderCustomer.customer.defaultBillingAddress');
$criteria->addAssociation('orderCustomer.customer.defaultBillingAddress.company');
$criteria->addAssociation('orderCustomer.customer.defaultBillingAddress.country');
$criteria->setLimit(1);
return $this->orderRepository->search($criteria, $context)->first();
}
private function getAgrilityCategory(Context $context): ?CategoryEntity
{
$criteria = new Criteria();
$criteria->addFilter(new OrFilter([
new EqualsFilter('name', 'agrility'),
new EqualsFilter('name', 'Agrility'),
new EqualsFilter('name', 'AGRILITY'),
]));
$criteria->setLimit(1);
return $this->categoryRepository->search($criteria, $context)->first();
}
}