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

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