<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Shopware\Storefront\Framework\Media\StorefrontMediaUploader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RequestStack;
class SaveTradeLicenseForCustomer implements EventSubscriberInterface
{
private ?CustomerEntity $currentCustomer;
public function __construct(
private readonly RequestStack $requestStack,
private readonly StorefrontMediaUploader $storefrontMediaUploader,
private readonly EntityRepository $customerRepository,
private readonly EntityRepository $mediaRepository,
) {
}
public function getCustomerInfo(SalesChannelContextResolvedEvent $event)
{
$this->currentCustomer = $event->getSalesChannelContext()->getCustomer();
}
public static function getSubscribedEvents(): array
{
return [
CustomerRegisterEvent::class => ['saveTradeLicenseIfUploaded', 4000],
GuestCustomerRegisterEvent::class => ['saveTradeLicenseIfUploaded', 4000],
// 'frontend.account.profile.save.request'=> 'getCustomerInfo',
'checkout.customer.sales_channel.profile.update' => 'saveTradeLicenseIfUploadedDuringProfileUpdate',
SalesChannelContextResolvedEvent::class => 'getCustomerInfo',
];
}
public function saveTradeLicenseIfUploaded(CustomerRegisterEvent|GuestCustomerRegisterEvent $event): void
{
/** @var ?UploadedFile $tradeLicenseFile */
$tradeLicenseFile = $this->requestStack->getCurrentRequest()->files->get('tradeLicense');
if (!$tradeLicenseFile) {
return;
}
$mediaId = $this->storefrontMediaUploader->upload($tradeLicenseFile, 'user', 'trade_license', $event->getContext(), true);
$customer = $event->getCustomer();
$this->customerRepository->update([
[
'id' => $customer->getId(),
'customFields' => [
'wbfk_customer_trade_license' => $mediaId,
],
],
], $event->getContext());
// Add custom field to customer entity so that it can be utilized to send mail to admin
$customer->setCustomFields([
'wbfk_customer_trade_license' => $mediaId,
...$customer->getCustomFields() ?? [],
]);
}
public function saveTradeLicenseIfUploadedDuringProfileUpdate(DataMappingEvent $event): void
{
$tradeLicenseFile = $this->requestStack->getCurrentRequest()->files->get('tradeLicense');
if (!$tradeLicenseFile) {
return;
}
$this->deletePreviousTradeLicense($event->getContext());
$mediaId = $this->storefrontMediaUploader->upload($tradeLicenseFile, 'user', 'trade_license', $event->getContext(), true);
$customer = $event->getOutput();
if (!isset($customer['customFields'])) {
$customer['customFields'] = [];
}
$customer['customFields']['wbfk_customer_trade_license'] = $mediaId;
$event->setOutput($customer);
}
private function deletePreviousTradeLicense(Context $context): void
{
if (!$this->currentCustomer) {
return;
}
$previousTradeLicenseMediaId = $this->currentCustomer->getCustomFields()['wbfk_customer_trade_license'] ?? null;
if (!$previousTradeLicenseMediaId) {
return;
}
$context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($previousTradeLicenseMediaId) {
$this->mediaRepository->delete([['id' => $previousTradeLicenseMediaId]], $context);
});
}
}