src/Insurance/Controller/HomeController.php line 56

Open in your IDE?
  1. <?php
  2. namespace Insurance\Controller;
  3. use Insurance\Model\User;
  4. use Insurance\Request\FirstConnectionRequest;
  5. use Insurance\Request\PasswordResetRequest;
  6. use Insurance\Request\PostLoginRequest;
  7. use Insurance\Response\PostLoginResponse;
  8. use Insurance\Service\BcaAccountService;
  9. use Insurance\Service\BcaConnectService;
  10. use Insurance\Service\BcaRequestService;
  11. use Insurance\Service\PricingService;
  12. use Insurance\Service\SamSchoolService;
  13. use GuzzleHttp\Exception\GuzzleException;
  14. use JMS\Serializer\SerializationContext;
  15. use JMS\Serializer\SerializerInterface;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  23. class HomeController extends AbstractController
  24. {
  25.     private PricingService $pricingService;
  26.     private SerializerInterface $serializer;
  27.     private BcaConnectService $bcaConnectService;
  28.     private BcaAccountService $bcaAccountService;
  29.     private SamSchoolService $samSchoolService;
  30.     private BcaRequestService $bcaRequestService;
  31.     public function __construct(
  32.         PricingService $pricingService,
  33.         SerializerInterface $serializer,
  34.         BcaConnectService $bcaConnectService,
  35.         BcaAccountService $bcaAccountService,
  36.         SamSchoolService $samSchoolService,
  37.         BcaRequestService $bcaRequestService
  38.     )
  39.     {
  40.         $this->pricingService $pricingService;
  41.         $this->serializer $serializer;
  42.         $this->bcaConnectService $bcaConnectService;
  43.         $this->bcaAccountService $bcaAccountService;
  44.         $this->samSchoolService $samSchoolService;
  45.         $this->bcaRequestService $bcaRequestService;
  46.     }
  47.     /**
  48.      * @Route("/", name="home")
  49.      */
  50.     public function index()
  51.     {
  52.         return $this->render('home/index.html.twig');
  53.     }
  54.     /**
  55.      * @Route("/login", name="postLogin", methods={"POST"})
  56.      * @ParamConverter("postLoginRequest", converter="postLoginConverter", class="Insurance\Request\PostLoginRequest")
  57.      *
  58.      * @param PostLoginRequest $postLoginRequest
  59.      * @return Response
  60.      * @throws TransportExceptionInterface
  61.      * @throws GuzzleException
  62.      */
  63.     public function postLogin(PostLoginRequest $postLoginRequest)
  64.     {
  65.         /** @var PostLoginResponse $postLoginResponse */
  66.         $postLoginResponse $this->bcaConnectService->postLogin($postLoginRequest);
  67.         $customerView $this->bcaAccountService->getCustomerView($postLoginRequest->getEmail());
  68.         $customerProducts $this->bcaAccountService->getCustomerProducts($postLoginResponse->getId());
  69.         $user = new User();
  70.         $user->withCustomerViewResponse($customerView);
  71.         if(!empty($customerProducts)){
  72.             $user->setProduct($customerProducts[0]);
  73.         }
  74.         $jsonUser $this->serializer->serialize($user,'json');
  75.         return new JsonResponse($jsonUser200, [], true);
  76.     }
  77.     /**
  78.      * @Route("/user", name="getUserByToken", methods={"GET"})
  79.      */
  80.     public function getUserByToken(Request $request)
  81.     {
  82.         $customerId $this->bcaRequestService->getCustomerId($request->query->get('token'));
  83.         $customerView $this->bcaAccountService->getCustomerById($customerId);
  84.         $customerProducts $this->bcaAccountService->getCustomerProducts($customerId);
  85.         
  86.         $user User::fromCustomerView($customerView);
  87.         if(!empty($customerProducts)){
  88.             $user->setProduct($customerProducts[0]);
  89.         }
  90.         $jsonUser $this->serializer->serialize($user'json');
  91.         return new JsonResponse($jsonUser200, [], true);
  92.     }
  93.     /**
  94.      * @Route("/firstConnection", name="firstConnection", methods={"POST"})
  95.      * @param FirstConnectionRequest $firstConnectionRequest
  96.      * @return JsonResponse
  97.      * @throws TransportExceptionInterface
  98.      * @throws GuzzleException
  99.      */
  100.     public function firstConnection(FirstConnectionRequest $firstConnectionRequest)
  101.     {
  102.         $forgotRequestResponse $this->bcaConnectService->firstConnection($firstConnectionRequest);
  103.         $customerView $this->bcaAccountService->getCustomerView($firstConnectionRequest->getEmail());
  104.         $user = new User();
  105.         $user->withCustomerViewResponse($customerView)
  106.             ->setToken($forgotRequestResponse->getToken());
  107.         $context SerializationContext::create()->setGroups(['authentication']);
  108.         $jsonUser $this->serializer->serialize($user,'json'$context);
  109.         return new JsonResponse($jsonUser200, [], true);
  110.     }
  111.     /**
  112.      * @Route("/passwordReset", name="passwordReset", methods={"POST"})
  113.      * @param PasswordResetRequest $passwordResetRequest
  114.      * @return Response
  115.      * @throws TransportExceptionInterface
  116.      * @throws GuzzleException
  117.      */
  118.     public function resetPassword(PasswordResetRequest $passwordResetRequest){
  119.         $this->bcaConnectService->resetPassword($passwordResetRequest);
  120.         return new Response();
  121.     }
  122.     /**
  123.      * @Route("/context", name="context", methods={"POST"})
  124.      *
  125.      * @param User $user
  126.      * @return Response
  127.      */
  128.     public function createContext(User $user)
  129.     {
  130.         $url $this->samSchoolService->createContext($user);
  131.         return new JsonResponse(["url"=>$url], Response::HTTP_CREATED);
  132.     }
  133.     /**
  134.      * @Route("/formule/{formule}", name="formule", methods={"GET"})
  135.      */
  136.     public function getFormule(string $formule): JsonResponse
  137.     {
  138.         $content $this->pricingService->getFormulePrice($formule);
  139.         return $this->json($contentResponse::HTTP_OK, [], [
  140.             'groups' => 'get',
  141.         ]);
  142.     }
  143. }