<?php
declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\AmazonPay\Framework\EventListeners;
use Shopware\Core\Framework\Validation\BuildValidationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraints\Optional;
class AmazonPayAddressValidationListener implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
RequestStack $requestStack
) {
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
'framework.validation.address.create' => 'disableAdditionalAddressValidation',
'framework.validation.address.update' => 'disableAdditionalAddressValidation',
'framework.validation.customer.create' => 'disableBirthdayValidation',
];
}
public function disableAdditionalAddressValidation(BuildValidationEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if ($request === null) {
return;
}
if (\mb_strpos($request->getPathInfo(), 'swag_amazon_pay') === false) {
return;
}
$definition = $event->getDefinition();
$definition->set('additionalAddressLine1', new Optional());
$definition->set('additionalAddressLine2', new Optional());
$definition->set('phoneNumber', new Optional());
}
public function disableBirthdayValidation(BuildValidationEvent $event): void
{
$request = $this->requestStack->getCurrentRequest();
if ($request === null) {
return;
}
if (\mb_strpos($request->getPathInfo(), 'swag_amazon_pay') === false) {
return;
}
$definition = $event->getDefinition();
$definition->set('birthdayDay', new Optional());
$definition->set('birthdayMonth', new Optional());
$definition->set('birthdayYear', new Optional());
}
}