<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2021
*
***/
use Samson\CustomFieldSet\Constants\CustomerAddressCustomFieldConstants;
use Samson\Service\AddressServiceInterface;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddressSubscriber implements EventSubscriberInterface
{
private AddressServiceInterface $customerAddressService;
public function __construct(AddressServiceInterface $customerAddressService)
{
$this->customerAddressService = $customerAddressService;
}
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'setAddressOutput',
CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'setAddressOutput',
CustomerEvents::MAPPING_ADDRESS_CREATE => 'setAddressOutput',
CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => 'onCustomAddressLoaded'
];
}
public function onCustomAddressLoaded(EntityLoadedEvent $event): void
{
/** @var CustomerAddressEntity $address */
foreach ($event->getEntities() as $address) {
$customFields = $address->getCustomFields();
if (!isset($customFields[CustomerAddressCustomFieldConstants::CUSTOM_FIELD_HOUSE_NUMBER])) {
$street = $address->getStreet();
$houseNumberAndStreet = $this->customerAddressService->separateHouseNumberFromStreet($street);
if (!array_key_exists('houseNumber', $houseNumberAndStreet)) {
return;
}
$street = $houseNumberAndStreet['street'];
$houseNumber = $houseNumberAndStreet['houseNumber'];
$customFields = [
CustomerAddressCustomFieldConstants::CUSTOM_FIELD_STREET => $street,
CustomerAddressCustomFieldConstants::CUSTOM_FIELD_HOUSE_NUMBER => $houseNumber
];
$address->setCustomFields($customFields);
}
}
}
public function setAddressOutput(DataMappingEvent $event): void
{
$output = $event->getOutput();
$input = $event->getInput();
$houseNumber = trim($input->get('houseNumber', ''));
$street = trim($input->get('street', ''));
$houseNumberAndStreet = $this->customerAddressService->separateHouseNumberFromStreet($street);
if (!array_key_exists('houseNumber', $houseNumberAndStreet) && empty($houseNumber)) {
return;
}
if (array_key_exists('houseNumber', $houseNumberAndStreet)) {
$houseNumber = $houseNumber === $houseNumberAndStreet['houseNumber']
? $houseNumber
: $houseNumberAndStreet['houseNumber'];
}
$street = $houseNumberAndStreet['street'];
$output['street'] = $street . " " . $houseNumber;
$output['customFields'] = [
CustomerAddressCustomFieldConstants::CUSTOM_FIELD_STREET => $street,
CustomerAddressCustomFieldConstants::CUSTOM_FIELD_HOUSE_NUMBER => $houseNumber
];
$event->setOutput($output);
}
}