<?php
namespace App\Controller\Front\Scholar\Training\Payment;
use App\Entity\Channel\Channel;
use App\Entity\Scholar\Training\Training;
use App\Form\Payment\FundingType;
use App\Service\Mailer\MailerService;
use Nellapp\Bundle\SDKBundle\ApiClientInterApp\Client\CrmClient;
use Nellapp\Bundle\SDKBundle\ApiClientInterApp\Model\Crm\FundingContactRequestDTO;
use Nellapp\Bundle\SDKBundle\Enum\ChannelUserData\ApplicantTypeEnum;
use Nellapp\Bundle\SDKBundle\Enum\ChannelUserData\FounderTypeEnum;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class PaymentFundingContactController extends AbstractController
{
public function __construct(
private MailerService $mailerService,
private CrmClient $crmClient,
private NormalizerInterface $normalizer,
private TranslatorInterface $translator,
) {}
#[Route(path: '/training_payment/funding/{training_id}/contact', name: 'front_channel_training_payment_funding_contact')]
#[ParamConverter('training', options: ['id' => 'training_id'])]
public function __invoke(Request $request, Training $training): Response
{
if ($training->getTrainingProduct() === null) {
return $this->redirectToRoute('front_channel_training_show', ['id' => $training->getId()]);
}
$fundingContactForm = $this->createForm(
type: FundingType::class,
options: ["training" => $training],
);
$fundingContactForm->handleRequest($request);
$training->getPrice();
if ($fundingContactForm->isSubmitted() && $fundingContactForm->isValid()) {
/** @var FundingContactRequestDTO $data */
$fundingContactRequest = $fundingContactForm->getData();
/** @var Channel $channel */
$channel = $training->getOwnerChannel();
try {
if ($channel->getConfiguration()?->isEnabledCrm()) {
$_response = $this->crmClient->postOpportunityFromFundingContact(
channel: $channel,
fundingContactRequest: $fundingContactRequest,
);
} else {
$this->sendEmailNotification($training, $fundingContactRequest);
}
$this->addFlash('success', 'Demande de contact envoyée avec succès');
return $this->redirectToRoute('front_channel_training_show', ['id' => $training->getId()]);
} catch (\Exception $_e) {
$this->addFlash('error', 'Erreur lors de l\'envoi de la demande.');
}
}
return $this->render('Front/Scholar/Training/Payment/payment_funding_contact.html.twig', [
'training' => $training,
'form' => $fundingContactForm->createView(),
]);
}
private function sendEmailNotification(Training $training, FundingContactRequestDTO $dto): void
{
$formData = $this->normalizer->normalize($dto);
// Handling enums tranlations before passing raw data to email templates
$formData['founderType'] = $this->translator->trans(
FounderTypeEnum::getStatusKey($dto->founderType),
domain: "NellappSDKBundle",
);
$formData['status'] = $this->translator->trans(
ApplicantTypeEnum::getStatusKey($dto->status),
domain: "NellappSDKBundle",
);
$this->mailerService->sendFundingDemandMail($formData, $training);
$owner = $training->getOwnerChannel()?->getOwner();
if (null !== $owner) {
$this->mailerService->sendFundingReceive($formData, $training, [$owner]);
}
}
}