<?php
namespace App\EventListener;
use App\Event\ApplicantEvent;
use App\Util\AmeUtil;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
class AmeEventListener
{
private FeatureManagerInterface $featureManager;
private AmeUtil $ameUtil;
private EntityManagerInterface $entityManager;
private LoggerInterface $logger;
private ParameterBagInterface $parameterBag;
/**
* AmeEventListener constructor.
*/
public function __construct(
FeatureManagerInterface $featureManager,
AmeUtil $ameUtil,
EntityManagerInterface $entityManager,
LoggerInterface $logger,
ParameterBagInterface $parameterBag
) {
$this->featureManager = $featureManager;
$this->ameUtil = $ameUtil;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->parameterBag = $parameterBag;
}
public function applicantSubmitted(ApplicantEvent $applicantEvent): void
{
if (!$this->featureManager->isActive(FeatureFlagListener::FEATURE_AME)
|| $data = '' === $this->ameUtil->fetchAmeData()
) {
return;
}
$applicant = $applicantEvent->getApplicant();
$field = $this->parameterBag->get('ame_default_field');
$currentValue = PropertyAccess::createPropertyAccessor()->getValue($applicant, $field);
PropertyAccess::createPropertyAccessor()->setValue($applicant, $field, $currentValue ? sprintf('%s, ame_data: %s', $currentValue, $data) : $data);
$this->entityManager->persist($applicant);
$this->entityManager->flush();
$this->logger->info("Added AME info of applicant to $field");
}
}