<?php declare(strict_types=1);
namespace FourtwosixSeparateStreetAndNumber\Storefront\Subscriber;
use FourtwosixSeparateStreetAndNumber\Components\Constants\Constants;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Framework\Validation\DataValidationDefinition;
use Shopware\Core\Framework\Validation\DataValidator;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
class AddressSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly DataValidator $dataValidator,
private readonly SystemConfigService $systemConfigService,
private readonly RequestStack $requestStack)
{
}
public static function getSubscribedEvents()
{
return [
CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => Constants::DATA_MAPPING_EVENT,
CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => Constants::DATA_MAPPING_EVENT,
CustomerEvents::MAPPING_ADDRESS_CREATE => Constants::DATA_MAPPING_EVENT
];
}
public function onDataMappingEvent(DataMappingEvent $event): void
{
$this->setAddressOutput($event);
}
private function setAddressOutput(DataMappingEvent $event): void
{
$output = $event->getOutput();
$houseNumber = $event->getInput()->get(Constants::ADDRESS_HOUSE_NUMBER_KEY);
$salesChannelId = $event->getContext()->hasExtension(Constants::SALES_CHANNEL_ID) ? $event->getContext()->getExtension(Constants::SALES_CHANNEL_ID)->get(Constants::ID_KEY) : null;
$active = $this->systemConfigService->get(Constants::PLUGIN_CONFIG.Constants::PLUGIN_ACTIVATION, $salesChannelId);
if ($active) {
if (empty($salesChannelId) && !empty($this->requestStack) && !empty($request = $this->requestStack->getCurrentRequest())) {
$salesChannelId = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
}
if (!empty($salesChannelId) && $this->systemConfigService->get(Constants::PLUGIN_CONFIG.Constants::PLUGIN_CONFIG_ACTIVATION, $salesChannelId)) {
$definition = new DataValidationDefinition();
$definition->add(Constants::ADDRESS_HOUSE_NUMBER_KEY, new NotBlank(), new Type(Constants::STRING_TYPE));
$this->dataValidator->validate($event->getInput()->all(), $definition);
}
if (!$houseNumber) return;
$output[Constants::ADDRESS_STREET_KEY] = trim($output[Constants::ADDRESS_STREET_KEY]).Constants::EMPTY_SPACE.trim($houseNumber);
if (!array_key_exists(Constants::ADDRESS_CUSTOM_FIELD_KEY, $output)) {
$output[Constants::ADDRESS_CUSTOM_FIELD_KEY] = [
Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY => trim($event->getInput()->get(Constants::ADDRESS_STREET_KEY)),
Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY => trim($event->getInput()->get(Constants::ADDRESS_HOUSE_NUMBER_KEY))
];
} else {
$output[Constants::ADDRESS_CUSTOM_FIELD_KEY][Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY] = trim($event->getInput()->get(Constants::ADDRESS_STREET_KEY));
$output[Constants::ADDRESS_CUSTOM_FIELD_KEY][Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY] = trim($event->getInput()->get(Constants::ADDRESS_HOUSE_NUMBER_KEY));
}
$event->setOutput($output);
}
}
}