custom/plugins/LinkMobilityShopwareSmsApiPlugin/src/Subscriber/CustomerWrittenSubscriber.php line 119

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace LinkMobility\ShopwareSmsApiPlugin\Subscriber;
  4. use LinkMobility\ShopwareSmsApiPlugin\Exception\SmsApiRequestException;
  5. use LinkMobility\ShopwareSmsApiPlugin\Service\ContactService;
  6. use Shopware\Core\Checkout\Customer\CustomerDefinition;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Checkout\Customer\CustomerEvents;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  15. use Shopware\Core\Framework\Event\DataMappingEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class CustomerWrittenSubscriber implements EventSubscriberInterface
  21. {
  22.     public function __construct(
  23.         private EntityRepository $customerRepository,
  24.         private ContactService $contactService,
  25.         private RequestStack $requestStack,
  26.         private TranslatorInterface $translator
  27.     ) {
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'onProfileSaved',
  33.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  34.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onProfileSaved',
  35.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onCustomerDeleted',
  36.             PreWriteValidationEvent::class => 'triggerChangeSet',
  37.         ];
  38.     }
  39.     public function onProfileSaved(DataMappingEvent $event): void
  40.     {
  41.         $data $event->getInput();
  42.         $customer $event->getOutput();
  43.         $consent 'on' === $data->get('link_mobility_shopware_sms_api_plugin_consent');
  44.         $customer['customFields']['link_mobility_shopware_sms_api_plugin_consent'] = $consent;
  45.         $event->setOutput($customer);
  46.     }
  47.     public function onCustomerWritten(EntityWrittenEvent $event): void
  48.     {
  49.         $ids $event->getIds();
  50.         if (=== \count($ids)) {
  51.             return;
  52.         }
  53.         $criteria = new Criteria($ids);
  54.         $criteria->addAssociation('defaultBillingAddress');
  55.         $context $event->getContext();
  56.         $customers $this->customerRepository->search($criteria$context)->getEntities();
  57.         /** @var CustomerEntity $customer */
  58.         foreach ($customers as $customer) {
  59.             $customFields $customer->getCustomFields();
  60.             $customerSmsApiId $customFields['link_mobility_shopware_sms_api_plugin_customer_id'] ?? null;
  61.             $consent $customFields['link_mobility_shopware_sms_api_plugin_consent'] ?? true;
  62.             if (!$consent) {
  63.                 if (null !== $customerSmsApiId) {
  64.                     $this->contactService->delete($customer$customerSmsApiId$context);
  65.                 }
  66.                 continue;
  67.             }
  68.             if (null !== $customerSmsApiId) {
  69.                 try {
  70.                     $this->contactService->edit($customer$customerSmsApiId$context);
  71.                 } catch (SmsApiRequestException $exception) {
  72.                     /** @var Session $session */
  73.                     $session $this->requestStack->getSession();
  74.                     $session->getFlashBag()->add('danger'$this->translator->trans($exception->getMessage()));
  75.                 }
  76.                 continue;
  77.             }
  78.             try {
  79.                 $this->contactService->create($customer$context);
  80.             } catch (SmsApiRequestException $exception) {
  81.                 /** @var Session $session */
  82.                 $session $this->requestStack->getSession();
  83.                 $alreadyFlashed false;
  84.                 $translated $this->translator->trans($exception->getMessage());
  85.                 $flashes $session->getFlashBag()->peek('danger');
  86.                 foreach ($flashes as $message) {
  87.                     if ($message === $translated) {
  88.                         $alreadyFlashed true;
  89.                     }
  90.                 }
  91.                 if (!$alreadyFlashed) {
  92.                     $session->getFlashBag()->add('danger'$translated);
  93.                 }
  94.             }
  95.         }
  96.     }
  97.     public function onCustomerDeleted(EntityDeletedEvent $event)
  98.     {
  99.         foreach ($event->getWriteResults() as $item) {
  100.             /** @var string|null $customFields */
  101.             $customFields $item->getChangeSet()?->getBefore('custom_fields');
  102.             if (null === $customFields) {
  103.                 continue;
  104.             }
  105.             $customFieldsDecode json_decode($customFieldstrue);
  106.             $customerSmsApiId $customFieldsDecode['link_mobility_shopware_sms_api_plugin_customer_id'] ?? null;
  107.             if (null === $customerSmsApiId) {
  108.                 continue;
  109.             }
  110.             $this->contactService->deleteContact($customerSmsApiId);
  111.         }
  112.     }
  113.     public function triggerChangeSet(PreWriteValidationEvent $event): void
  114.     {
  115.         foreach ($event->getCommands() as $command) {
  116.             if (!$command instanceof ChangeSetAware ||
  117.                 CustomerDefinition::ENTITY_NAME !== $command->getDefinition()->getEntityName()) {
  118.                 continue;
  119.             }
  120.             $command->requestChangeSet();
  121.         }
  122.     }
  123. }