<?php
declare(strict_types=1);
namespace Kzph\Simplenote\Subscriber;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderSubscriber implements EventSubscriberInterface
{
private $kzphSimplenoteRepository;
private $requestStack;
private $systemConfigService;
private $config;
public function __construct(
EntityRepository $kzphSimplenoteRepository,
RequestStack $requestStack,
?SystemConfigService $systemConfigService = null
) {
$this->kzphSimplenoteRepository = $kzphSimplenoteRepository;
$this->requestStack = $requestStack;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
];
}
public function onOrderWritten(EntityWrittenEvent $entityWrittenEvent): void
{
try {
$config = $this->systemConfigService->get('KzphSimplenote.config');
$this->config = $config;
if (($this->config['showNotefieldInCheckout'] ?? false) === false)
return;
$writeResults = $entityWrittenEvent->getWriteResults();
$request = $this->requestStack->getCurrentRequest();
foreach ($writeResults as $writeResult) {
$payload = $writeResult->getPayload();
if (@$payload['id']) {
if (($request->get('kzphSimpleNote') ?? false)) {
$this->kzphSimplenoteRepository->upsert([
[
'id' => $payload['id'],
'entityId' => $payload['id'],
'entityType' => 'order',
'username' => 'System',
'note' => $request->get('kzphSimpleNote'),
'showDesktop' => 1,
'showMessage' => 0
],
], $entityWrittenEvent->getContext());
return;
}
}
}
} catch (\Exception $e) {
}
}
}