<?php declare(strict_types=1);
namespace App\EventSubscriber;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\PostSubmitEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LeadTypeSubscriber implements EventSubscriberInterface
{
private const MAPPER = [
'Email' => ['email', 'mail'],
'LastName' => ['lastname', 'nom'],
];
public static function getSubscribedEvents(): array
{
return [
FormEvents::POST_SUBMIT => ['setQueryParams']
];
}
public function setQueryParams(PostSubmitEvent $event)
{
$lead = $event->getData();
foreach (self::MAPPER as $key => $params) {
/** @todo Use the property Accessor to set/get values in a proper way. */
$getter = 'get' . $key;
$setter = 'set' . $key;
if ($lead->{$getter}() === null) {
$query_params = $lead->getQueryParams();
foreach ($params as $param) {
if (isset($query_params[$param])) {
$lead->{$setter}($query_params[$param]);
}
}
}
}
}
}