<?php declare(strict_types=1);
namespace Acris\SeparateStreet\Storefront\Subscriber;
use Acris\SeparateStreet\Components\Service\SeparateStreetService;
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
{
/**
* @var DataValidator
*/
private $dataValidator;
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var SeparateStreetService
*/
private $separateStreetService;
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
DataValidator $dataValidator,
SystemConfigService $systemConfigService,
SeparateStreetService $separateStreetService,
RequestStack $requestStack
) {
$this->dataValidator = $dataValidator;
$this->systemConfigService = $systemConfigService;
$this->separateStreetService = $separateStreetService;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onDataMappingEvent',
CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onDataMappingEvent',
CustomerEvents::MAPPING_ADDRESS_CREATE => 'onDataMappingEvent'
];
}
/**
* @param DataMappingEvent $event
*/
public function onDataMappingEvent(DataMappingEvent $event): void
{
$this->setAddressOutput($event);
}
/**
* @param DataMappingEvent $event
*/
private function setAddressOutput(DataMappingEvent $event): void
{
$output = $event->getOutput();
$houseNumber = $event->getInput()->get('houseNumber');
$salesChannelId = $event->getContext()->hasExtension('AcrisSeparateStreetSalesChannel') ? $event->getContext()->getExtension('AcrisSeparateStreetSalesChannel')->get('id') : null;
if (empty($salesChannelId) && !empty($this->requestStack) && !empty($request = $this->requestStack->getCurrentRequest())) {
$salesChannelId = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
}
if ($this->separateStreetService->skipValidation($event->getInput()->all()) !== true && !empty($salesChannelId) && $this->systemConfigService->get('AcrisSeparateStreetCS.config.activateHouseNumberFieldValidation', $salesChannelId)) {
$definition = new DataValidationDefinition();
$definition->add('houseNumber', new NotBlank(), new Type('string'));
$this->dataValidator->validate($event->getInput()->all(), $definition);
}
if (!$houseNumber) return;
$output['street'] = trim($output['street'])." ".trim($houseNumber);
if (!array_key_exists('customFields', $output)) {
$output['customFields'] = [
'acris_separate_street_address_street' => trim($event->getInput()->get('street')),
'acris_separate_street_address_house_number' => trim($event->getInput()->get('houseNumber'))
];
} else {
$output['customFields']['acris_separate_street_address_street'] = trim($event->getInput()->get('street'));
$output['customFields']['acris_separate_street_address_house_number'] = trim($event->getInput()->get('houseNumber'));
}
$event->setOutput($output);
}
}