<?php
namespace App\Controller\Entrain;
use App\Repository\ContractRepository;
use App\Repository\JsonSurveyRepository;
use App\Service\Entrain\ContractNumberServiceEntrain;
use App\Service\Entrain\MailServiceEntrain;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class SubscriptionControllerEntrain extends AbstractController
{
public function __construct(
ContractRepository $contractRepository,
EntityManagerInterface $entityManager,
JsonSurveyRepository $jsonSurveyRepository,
ParameterBagInterface $parameterBag,
MailServiceEntrain $mailService,
ContractNumberServiceEntrain $contractNumberService,
HttpClientInterface $client
) {
$this->contractRepository = $contractRepository;
$this->entityManager = $entityManager;
$this->jsonSurveyRepository = $jsonSurveyRepository;
$this->parameterBag = $parameterBag;
$this->mailService = $mailService;
$this->contractNumberService = $contractNumberService;
$this->client = $client;
}
/**
* @Route("/subscriptionentrain", name="subscriptionentrain")
*/
public function index(Request $request, MailerInterface $mailer): Response
{
$params = $request->getContent();
$params = substr($params, 6, strlen($params) - 7);
$params = json_decode($params);
try {
$contract = $this->contractRepository->findOneBy(['folderId' => $params->iddossier]);
if ($contract->getSubscribed()) {
throw new Exception('le contrat a déja été souscrit', 400);
}
$survey = $this->jsonSurveyRepository->findBy(['contractId' => $contract]);
$jsonSurvey = json_decode($survey[0]->getSurvey());
$contract->setSubscribed(true);
$this->entityManager->persist($contract);
$this->entityManager->flush();
} catch (Exception $e) {
return $this->json([
'message' => $e->getMessage()
], $e->getCode());
}
$insurer_mails = [(isset($jsonSurvey->question57)) ? $jsonSurvey->question57 : null, (isset($jsonSurvey->question58)) ? $jsonSurvey->question58 : null];
$statut = $params->statut;
try {
foreach ($insurer_mails as $key => $insurer_mail) {
if ($statut == "valide" && $insurer_mail != null) {
$credentials = [
'email' => $insurer_mail,
'checkIfUserExist' => 'ask Personal Space Area for User'
];
$response = $this->client->request(
'POST',
$_ENV['NEW_ACCOUNT_PA'],
[
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'Accept' => 'application/json'
],
'body' => json_encode($credentials)
]
);
// check if user already have an account for eventually renew account
if ($response->getContent() != "User exist"){
$this->mailService->sendTemporaryPassword($contract, $key + 1);
}
}
}
} catch (Exception $e) {
return $this->json([
'message' => $e->getMessage()
]);
} finally {
// $this->mailService->documentsSenderAfterSubscription($contract);
$this->mailService->sendClientSubscriptionInformation();
return $this->json([
'info' => 'les documents et informations ont bien été envoyé au(x) prospect(s)!'
]);
}
}
}