src/Controller/Entrain/SubscriptionControllerEntrain.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Entrain;
  3. use App\Repository\ContractRepository;
  4. use App\Repository\JsonSurveyRepository;
  5. use App\Service\Entrain\ContractNumberServiceEntrain;
  6. use App\Service\Entrain\MailServiceEntrain;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\HttpClient\HttpClientInterface;
  16. class SubscriptionControllerEntrain extends AbstractController
  17. {
  18.     public function __construct(
  19.         ContractRepository           $contractRepository,
  20.         EntityManagerInterface       $entityManager,
  21.         JsonSurveyRepository         $jsonSurveyRepository,
  22.         ParameterBagInterface        $parameterBag,
  23.         MailServiceEntrain           $mailService,
  24.         ContractNumberServiceEntrain $contractNumberService,
  25.         HttpClientInterface          $client
  26.     ) {
  27.         $this->contractRepository $contractRepository;
  28.         $this->entityManager $entityManager;
  29.         $this->jsonSurveyRepository $jsonSurveyRepository;
  30.         $this->parameterBag $parameterBag;
  31.         $this->mailService $mailService;
  32.         $this->contractNumberService $contractNumberService;
  33.         $this->client $client;
  34.     }
  35.     /**
  36.      * @Route("/subscriptionentrain", name="subscriptionentrain")
  37.      */
  38.     public function index(Request $requestMailerInterface $mailer): Response
  39.     {
  40.         $params $request->getContent();
  41.         $params substr($params6strlen($params) - 7);
  42.         $params json_decode($params);
  43.         try {
  44.             $contract $this->contractRepository->findOneBy(['folderId' => $params->iddossier]);
  45.             if ($contract->getSubscribed()) {
  46.                 throw new Exception('le contrat a déja été souscrit'400);
  47.             }
  48.             $survey $this->jsonSurveyRepository->findBy(['contractId' => $contract]);
  49.             $jsonSurvey json_decode($survey[0]->getSurvey());
  50.             $contract->setSubscribed(true);
  51.             $this->entityManager->persist($contract);
  52.             $this->entityManager->flush();
  53.         } catch (Exception $e) {
  54.             return $this->json([
  55.                 'message' => $e->getMessage()
  56.             ], $e->getCode());
  57.         }
  58.         $insurer_mails = [(isset($jsonSurvey->question57)) ? $jsonSurvey->question57 null, (isset($jsonSurvey->question58)) ? $jsonSurvey->question58 null];
  59.         $statut $params->statut;
  60.         try {
  61.             foreach ($insurer_mails as $key => $insurer_mail) {
  62.                 if ($statut == "valide" && $insurer_mail != null) {
  63.                     $credentials = [
  64.                         'email' => $insurer_mail,
  65.                         'checkIfUserExist' => 'ask Personal Space Area for User'
  66.                     ];
  67.                     $response $this->client->request(
  68.                         'POST',
  69.                         $_ENV['NEW_ACCOUNT_PA'],
  70.                         [
  71.                             'headers' => [
  72.                                 'Content-Type' => 'application/json; charset=utf-8',
  73.                                 'Accept' => 'application/json'
  74.                             ],
  75.                             'body' => json_encode($credentials)
  76.                         ]
  77.                     );
  78.                     // check if user already have an account for eventually renew account
  79.                     if ($response->getContent() != "User exist"){
  80.                         $this->mailService->sendTemporaryPassword($contract$key 1);
  81.                     }
  82.                 }
  83.             }
  84.         } catch (Exception $e) {
  85.             return $this->json([
  86.                 'message' => $e->getMessage()
  87.             ]);
  88.         } finally {
  89.             // $this->mailService->documentsSenderAfterSubscription($contract);
  90.             $this->mailService->sendClientSubscriptionInformation();
  91.             return $this->json([
  92.                 'info' => 'les documents et informations ont bien été envoyé au(x) prospect(s)!'
  93.             ]);
  94.         }
  95.     }
  96. }