custom/plugins/WbfkExtensions/src/Subscriber/SaveTradeLicenseForCustomer.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  6. use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\Event\DataMappingEvent;
  10. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  11. use Shopware\Storefront\Framework\Media\StorefrontMediaUploader;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class SaveTradeLicenseForCustomer implements EventSubscriberInterface
  16. {
  17.     private ?CustomerEntity $currentCustomer;
  18.     public function __construct(
  19.         private readonly RequestStack $requestStack,
  20.         private readonly StorefrontMediaUploader $storefrontMediaUploader,
  21.         private readonly EntityRepository $customerRepository,
  22.         private readonly EntityRepository $mediaRepository,
  23.     ) {
  24.     }
  25.     public function getCustomerInfo(SalesChannelContextResolvedEvent $event)
  26.     {
  27.         $this->currentCustomer $event->getSalesChannelContext()->getCustomer();
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CustomerRegisterEvent::class => ['saveTradeLicenseIfUploaded'4000],
  33.             GuestCustomerRegisterEvent::class => ['saveTradeLicenseIfUploaded'4000],
  34. //            'frontend.account.profile.save.request'=> 'getCustomerInfo',
  35.             'checkout.customer.sales_channel.profile.update' => 'saveTradeLicenseIfUploadedDuringProfileUpdate',
  36.             SalesChannelContextResolvedEvent::class => 'getCustomerInfo',
  37.         ];
  38.     }
  39.     public function saveTradeLicenseIfUploaded(CustomerRegisterEvent|GuestCustomerRegisterEvent $event): void
  40.     {
  41.         /** @var ?UploadedFile $tradeLicenseFile */
  42.         $tradeLicenseFile $this->requestStack->getCurrentRequest()->files->get('tradeLicense');
  43.         if (!$tradeLicenseFile) {
  44.             return;
  45.         }
  46.         $mediaId $this->storefrontMediaUploader->upload($tradeLicenseFile'user''trade_license'$event->getContext(), true);
  47.         $customer $event->getCustomer();
  48.         $this->customerRepository->update([
  49.             [
  50.                 'id' => $customer->getId(),
  51.                 'customFields' => [
  52.                     'wbfk_customer_trade_license' => $mediaId,
  53.                 ],
  54.             ],
  55.         ], $event->getContext());
  56.         // Add custom field to customer entity so that it can be utilized to send mail to admin
  57.         $customer->setCustomFields([
  58.             'wbfk_customer_trade_license' => $mediaId,
  59.             ...$customer->getCustomFields() ?? [],
  60.         ]);
  61.     }
  62.     public function saveTradeLicenseIfUploadedDuringProfileUpdate(DataMappingEvent $event): void
  63.     {
  64.         $tradeLicenseFile $this->requestStack->getCurrentRequest()->files->get('tradeLicense');
  65.         if (!$tradeLicenseFile) {
  66.             return;
  67.         }
  68.         $this->deletePreviousTradeLicense($event->getContext());
  69.         $mediaId $this->storefrontMediaUploader->upload($tradeLicenseFile'user''trade_license'$event->getContext(), true);
  70.         $customer $event->getOutput();
  71.         if (!isset($customer['customFields'])) {
  72.             $customer['customFields'] = [];
  73.         }
  74.         $customer['customFields']['wbfk_customer_trade_license'] = $mediaId;
  75.         $event->setOutput($customer);
  76.     }
  77.     private function deletePreviousTradeLicense(Context $context): void
  78.     {
  79.         if (!$this->currentCustomer) {
  80.             return;
  81.         }
  82.         $previousTradeLicenseMediaId $this->currentCustomer->getCustomFields()['wbfk_customer_trade_license'] ?? null;
  83.         if (!$previousTradeLicenseMediaId) {
  84.             return;
  85.         }
  86.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($previousTradeLicenseMediaId) {
  87.             $this->mediaRepository->delete([['id' => $previousTradeLicenseMediaId]], $context);
  88.         });
  89.     }
  90. }