<?php
declare(strict_types=1);
namespace Wbfk\VatIdValidation\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Wbfk\VatIdValidation\Service\VatIdCheckService;
use Wbfk\VatIdValidation\Service\VatIdTrimmer;
class CustomerWrittenSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'customer.written' => 'onCustomerWritten',
];
}
public function __construct(
protected readonly VatIdCheckService $vatIdCheckService,
protected readonly VatIdTrimmer $vatTrimmer,
protected readonly EventDispatcherInterface $eventDispatcher
) {
}
public function onCustomerWritten(EntityWrittenEvent $entityWrittenEvent): void
{
$payloads = $entityWrittenEvent->getPayloads();
foreach ($payloads as $payload) {
if (array_key_exists('vatIds', $payload) && $payload['vatIds'] !== null) {
$this->vatTrimmer->fixVatId($payload['id'], $payload['vatIds']);
$vatId = array_shift($payload['vatIds']);
if ($vatId === null) {
return;
}
$customerId = $payload['id'];
// ToDo: Async event to evaluate VatId
}
}
}
}