<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
use NetInventors\NetiNextEasyCoupon\Events\CheckoutVoucherCreatedEvent;
use NetInventors\NetiNextEasyCouponDesigns\Service\Task;
use NetInventors\NetiNextEasyCouponDesigns\Struct\LineItemStruct;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Checkout implements EventSubscriberInterface
{
private Task $taskHandler;
public function __construct(Task $taskHandler)
{
$this->taskHandler = $taskHandler;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutVoucherCreatedEvent::class => 'onCheckoutVoucherCreated',
];
}
public function onCheckoutVoucherCreated(CheckoutVoucherCreatedEvent $event): void
{
foreach ($event->getVoucherData() as $voucher) {
/** @var string $orderLineItemId */
$orderLineItemId = $voucher['transactions'][0]['orderLineItemId'];
/** @var OrderLineItemEntity $orderLineItem */
$orderLineItem = $event->getOrderLineItems()->get($orderLineItemId);
$payload = $orderLineItem->getPayload();
if (
!isset($payload[LineItemStruct::PAYLOAD_NAME]['selectedDesign'])
|| !\is_string($payload[LineItemStruct::PAYLOAD_NAME]['selectedDesign'])
|| '' === $payload[LineItemStruct::PAYLOAD_NAME]['selectedDesign']
) {
continue;
}
$this->taskHandler->createTask($voucher, $orderLineItem, $event->getContext());
}
}
}