custom/plugins/WbfkVatIdValidation/src/Subscriber/CustomerWrittenSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\VatIdValidation\Subscriber;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  7. use Wbfk\VatIdValidation\Service\VatIdCheckService;
  8. use Wbfk\VatIdValidation\Service\VatIdTrimmer;
  9. class CustomerWrittenSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             'customer.written' => 'onCustomerWritten',
  15.         ];
  16.     }
  17.     public function __construct(
  18.         protected readonly VatIdCheckService $vatIdCheckService,
  19.         protected readonly VatIdTrimmer $vatTrimmer,
  20.         protected readonly EventDispatcherInterface $eventDispatcher
  21.     ) {
  22.     }
  23.     public function onCustomerWritten(EntityWrittenEvent $entityWrittenEvent): void
  24.     {
  25.         $payloads $entityWrittenEvent->getPayloads();
  26.         foreach ($payloads as $payload) {
  27.             if (array_key_exists('vatIds'$payload) && $payload['vatIds'] !== null) {
  28.                 $this->vatTrimmer->fixVatId($payload['id'], $payload['vatIds']);
  29.                 $vatId array_shift($payload['vatIds']);
  30.                 if ($vatId === null) {
  31.                     return;
  32.                 }
  33.                 $customerId $payload['id'];
  34.                 // ToDo: Async event to evaluate VatId
  35.             }
  36.         }
  37.     }
  38. }