src/Controller/Front/Scholar/Training/Payment/PaymentFundingContactController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front\Scholar\Training\Payment;
  3. use App\Entity\Scholar\Training\Training;
  4. use App\Form\Payment\FundingType;
  5. use App\Service\Mailer\MailerService;
  6. use Nellapp\Bundle\SDKBundle\ApiClientInterApp\Client\CrmClient;
  7. use Nellapp\Bundle\SDKBundle\ApiClientInterApp\Model\Crm\FundingContactRequestDTO;
  8. use Nellapp\Bundle\SDKBundle\Enum\ChannelUserData\ApplicantTypeEnum;
  9. use Nellapp\Bundle\SDKBundle\Enum\ChannelUserData\FounderTypeEnum;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class PaymentFundingContactController extends AbstractController
  18. {
  19. public function __construct(
  20. private MailerService $mailerService,
  21. private CrmClient $crmClient,
  22. private NormalizerInterface $normalizer,
  23. private TranslatorInterface $translator,
  24. ) {}
  25. #[Route(path: '/training_payment/funding/{training_id}/contact', name: 'front_channel_training_payment_funding_contact')]
  26. #[ParamConverter('training', options: ['id' => 'training_id'])]
  27. public function __invoke(Request $request, Training $training): Response
  28. {
  29. if ($training->getTrainingProduct() === null) {
  30. return $this->redirectToRoute('front_channel_training_show', ['id' => $training->getId()]);
  31. }
  32. $fundingContactForm = $this->createForm(
  33. type: FundingType::class,
  34. options: ["training" => $training],
  35. );
  36. $fundingContactForm->handleRequest($request);
  37. $training->getPrice();
  38. $channel = $training->getOwnerChannel();
  39. if ($fundingContactForm->isSubmitted() && $fundingContactForm->isValid()) {
  40. /** @var FundingContactRequestDTO $data */
  41. $fundingContactRequest = $fundingContactForm->getData();
  42. try {
  43. if ($channel->getConfiguration()?->isEnabledCrm()) {
  44. $_response = $this->crmClient->postOpportunityFromFundingContact(
  45. channel: $channel,
  46. fundingContactRequest: $fundingContactRequest,
  47. );
  48. } else {
  49. $this->sendEmailNotification($training, $fundingContactRequest);
  50. }
  51. $this->addFlash('success', 'Demande de contact envoyée avec succès');
  52. return $this->redirectToRoute('front_channel_training_show', ['id' => $training->getId()]);
  53. } catch (\Exception $_e) {
  54. $this->addFlash('error', 'Erreur lors de l\'envoi de la demande.');
  55. }
  56. }
  57. return $this->render('Front/Scholar/Training/Payment/payment_funding_contact.html.twig', [
  58. 'channel' => $channel,
  59. 'training' => $training,
  60. 'form' => $fundingContactForm->createView(),
  61. ]);
  62. }
  63. private function sendEmailNotification(Training $training, FundingContactRequestDTO $dto): void
  64. {
  65. $formData = $this->normalizer->normalize($dto);
  66. // Handling enums tranlations before passing raw data to email templates
  67. $formData['founderType'] = $this->translator->trans(
  68. FounderTypeEnum::getStatusKey($dto->founderType),
  69. domain: "NellappSDKBundle",
  70. );
  71. $formData['status'] = $this->translator->trans(
  72. ApplicantTypeEnum::getStatusKey($dto->status),
  73. domain: "NellappSDKBundle",
  74. );
  75. $this->mailerService->sendFundingDemandMail($formData, $training);
  76. $owner = $training->getOwnerChannel()?->getOwner();
  77. if (null !== $owner) {
  78. $this->mailerService->sendFundingReceive($formData, $training, [$owner]);
  79. }
  80. }
  81. }