src/Services/CallApiServices.php line 546

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\Routing\RouterInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. class CallApiServices
  8. {
  9.     private $apiKey;
  10.     private $apiToken;
  11.     private $accountId;
  12.     private $serviceId;
  13.     private $apiURL;
  14.     private $baseURL;
  15.     private $session;
  16.     public function __construct(SessionInterface $session$apiKey$accountId$baseURL$serviceId$apiURL )
  17.     {
  18.         $this->apiKey $apiKey;
  19.         $this->apiToken "";
  20.         $this->accountId $accountId;
  21.         $this->serviceId $serviceId;
  22.         $this->apiURL $apiURL;
  23.         $this->baseURL $baseURL;
  24.         $this->session $session;
  25.     }
  26.     public function getData(string $colletionName): array
  27.     {
  28.         
  29.         $url 'http://localhost:8080/api';
  30.         $request_url $url '/' $colletionName;
  31.         $curl curl_init($request_url);
  32.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  33.         $response curl_exec($curl);
  34.         if ($response === false) {
  35.             return null;
  36.         } else {
  37.             $results = [];
  38.             if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 200) {
  39.                 $response json_decode($responsetrue);
  40.             }
  41.         }
  42.         curl_close($curl);
  43.         return $response;
  44.     }
  45.     public function getDataById(string $colletionNamestring $id): array
  46.     {
  47.         $url 'http://localhost:8080/api';
  48.         $request_url $url '/' $colletionName '/' $id;
  49.         $curl curl_init($request_url);
  50.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  51.         $response curl_exec($curl);
  52.         if ($response === false) {
  53.             return null;
  54.         } else {
  55.             $results = [];
  56.             if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 200) {
  57.                 $response json_decode($responsetrue);
  58.             }
  59.         }
  60.         curl_close($curl);
  61.         return $response;
  62.     }
  63.     public function authApi(string $loginstring $password): array
  64.     {
  65.         $url 'http://localhost:8080/api/login';
  66.         $curl curl_init($url);
  67.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  68.         $headers = array(
  69.             "Accept: application/json",
  70.             "Content-Type: application/json",
  71.         );
  72.         $data = [
  73.             "username" => $login,
  74.             "password" => $password
  75.         ];
  76.         $dataEncoded json_encode($data);
  77.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  78.         curl_setopt($curlCURLOPT_POSTFIELDS$dataEncoded);
  79.         $response curl_exec($curl);
  80.         if ($response === false) {
  81.             return ["null" => null];
  82.         } else {
  83.             $results = [];
  84.             if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 200) {
  85.                 $response json_decode($responsetrue);
  86.             } else if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 401) {
  87.                 throw new \Exception('identifiant incorecte');
  88.             }
  89.         }
  90.         curl_close($curl);
  91.         return $response;
  92.     }
  93.     public function getUserDetails(string $token): array
  94.     {
  95.         $url 'http://localhost:8080/api/me';
  96.         $curl curl_init($url);
  97.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  98.         $headers = array(
  99.             "Accept: application/json",
  100.             "Content-Type: application/json",
  101.             "Authorization: Bearer " $token ""
  102.         );
  103.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  104.         $response curl_exec($curl);
  105.         if ($response === false) {
  106.             return ["null" => null];
  107.         } else {
  108.             $results = [];
  109.             if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 200) {
  110.                 $response json_decode($responsetrue);
  111.             }
  112.         }
  113.         curl_close($curl);
  114.         return $response;
  115.     }
  116. // ** +++++++++++++++++ ------------        --------- +++++++++++++++++ **
  117. // ** +++++++++++++++++ ------------        --------- +++++++++++++++++ **
  118.     public function getListConseillers($accountId$serviceId): array
  119.     {
  120.         $data = [
  121.             "accountId" => $accountId,
  122.             "serviceId" => $serviceId
  123.         ];
  124.         $results $this->restApiPOSTcURL('/plateforme/listeConseillersEtPresence' $data);
  125. //dd($results);
  126.         return $results;
  127.     }
  128.     public function configuratationPlateforme($accountId$serviceId): array
  129.     {
  130.         $data = [
  131.             "accountId" => $accountId,
  132.             "serviceId" => $serviceId
  133.         ];
  134.         $results $this->restApiPOSTcURL('/plateforme/configuratationPlateforme' $data);
  135.         return $results;
  136.     }
  137.     public function conseillerEtPlanning($pseudoId$serviceId): array
  138.     {
  139.         $data = [
  140.             "complementId" => $pseudoId,
  141.             "serviceId" => $serviceId
  142.         ];
  143.         $results $this->restApiPOSTcURL('/plateforme/conseillerEtPlanning' $data);
  144.         return $results;
  145.     }
  146.     public function clientAuthentification(string $loginstring $password$serviceId): array
  147.     {
  148.         $data = [
  149.             "serviceId" => $serviceId,
  150.             "login" => $login,
  151.             "password" => $password
  152.         ];
  153.         $results $this->restApiPOSTcURL('/plateforme/clientAuthentification' $data);
  154.         if (isset($results['responseCode']) && ($results['responseCode']!=200))  {
  155.             return $results;
  156.         }
  157.         $this->session->set('apiToken'$results["apiToken"]);
  158.         return $results;
  159.     }
  160.     //Avis
  161.     public function conseillerAvis($serviceId$complementId): array
  162.     {
  163.         $data = [
  164.             "serviceId" => $serviceId,
  165.             "complementId" => $complementId
  166.         ];
  167.         $results $this->restApiPOSTcURL('/plateforme/conseillerAvis' $data);
  168.         return $results;
  169.     }
  170.     public function clientAvis($serviceId$clientId): array
  171.     {
  172.         $data = [
  173.             "serviceId" => $serviceId,
  174.             "clientId" => $clientId
  175.         ];
  176.         $results $this->restApiPOSTcURL('/plateforme/clientAvis' $data);
  177.         return $results;
  178.     }
  179.     public function listePlateformeAvis($serviceId): array
  180.     {
  181.         $data = [
  182.             "serviceId" => $serviceId,
  183.         ];
  184.         $results $this->restApiPOSTcURL('/plateforme/listePlateformeAvis' $data);
  185.         return $results;
  186.     }
  187.     public function clientOperations($serviceId$clientId): array
  188.     {
  189.         $data = [
  190.             "serviceId" => $serviceId,
  191.             "clientId" => $clientId
  192.         ];
  193.         $results $this->restApiPOSTcURL('/plateforme/clientOperations' $data);
  194.         return $results;
  195.     }
  196.     public function clientProfile($serviceId$clientId): array
  197.     {
  198.         $data = [
  199.             "serviceId" => $serviceId,
  200.             "clientId" => $clientId
  201.         ];
  202.         $results $this->restApiPOSTcURL('/plateforme/clientProfile' $data);
  203.         return $results;
  204.     }
  205.     public function clientTransactions($serviceId$clientId): array
  206.     {
  207.         $data = [
  208.             "serviceId" => $serviceId,
  209.             "clientId" => $clientId
  210.         ];
  211.         $results $this->restApiPOSTcURL('/plateforme/clientTransactions' $data);
  212.         return $results;
  213.     }
  214.     public function clientConsultations($serviceId$clientId): array
  215.     {
  216.         $data = [
  217.             "serviceId" => $serviceId,
  218.             "clientId" => $clientId
  219.         ];
  220.         $results $this->restApiPOSTcURL('/plateforme/clientConsultations' $data);
  221.         return $results;
  222.     }
  223.     public function clientInscription($serviceId$parrainID$genre$email$password$firtsname$lastname$phoneNumber): ?array
  224.     {
  225.         $data = [
  226.             "serviceId" => $serviceId,
  227.             "parrainId" => $parrainID,
  228.             "genre" => $genre,
  229.             "username" => $email,
  230.             "password" => $password,
  231.             "nom" => $firtsname,
  232.             "prenom" => $lastname,
  233.             "pays" => "FRANCE",
  234.             "telephone" => $phoneNumber
  235.         ];
  236.         $results $this->restApiPOSTcURL('/plateforme/clientInscription' $data);
  237.         return $results;
  238.     }
  239.     public function socialInscription($serviceId$genre$email$password$firtsname$lastname$country$phoneNumber$social$socialUid): ?array
  240.     {
  241.         $data = [
  242.             "apiKey" =>  $this->apiKey,
  243.             "serviceId" => $serviceId,
  244.             "parrainId" => "0",
  245.             "genre" => $genre,
  246.             "username" => $email,
  247.             "password" => $password,
  248.             "nom" => $firtsname,
  249.             "prenom" => $lastname,
  250.             "pays" => $country,
  251.             "telephone" => $phoneNumber,
  252.             "social" => $social,
  253.             "socialUid" => $socialUid
  254.         ];
  255.         $results $this->restApiPOSTcURL('/plateforme/clientInscription' $data);
  256.         return $results;
  257.     }
  258.     public function clientForgotPassword($serviceId$username): ?array
  259.     {
  260.         $data = [
  261.             "serviceId" => $serviceId,
  262.             "username" => $username
  263.         ];
  264.         $results $this->restApiPOSTcURL('/plateforme/clientForgotPassword' $data);
  265.         return $results;
  266.     }
  267.     public function clientResetPassword($serviceId$username$oldPassword$newPassword$reqToken$limitToken): ?array
  268.     {
  269.         $data = [
  270.             "serviceId" => $serviceId,
  271.             "username" => $username,
  272.             "oldPassword" => $oldPassword,
  273.             "newPassword" => $newPassword
  274.         ]; 
  275.         $results $this->restApiPOSTcURL('/plateforme/clientResetPassword' $data);
  276.         return $results;
  277.     }
  278.     public function clientInscriptionValidation($serviceId$email$password$code$actif): ?array
  279.     {
  280.         $data = [
  281.             "serviceId" => $serviceId,
  282.             "username" => $email,
  283.             "password" => $password,
  284.             "code" => $code,
  285.             "actif" => $actif
  286.         ];
  287.         $results $this->restApiPOSTcURL('/plateforme/clientInscriptionValidation' $data);
  288.         return $results;
  289.     }
  290.     //Tarifs
  291.     //Tous les tarifs
  292.     public function tarifsPlateforme($serviceId): ?array
  293.     {
  294.         $data = [
  295.             "serviceId" => $serviceId,
  296.         ];
  297.         $results $this->restApiPOSTcURL('/plateforme/tarifsPlateforme' $data);
  298.         return $results;
  299.     }
  300.     public function cbTransaction($data): ?array
  301.     {
  302.         $results $this->restApiPOSTcURL('/plateforme/cbTransaction' $data);
  303.         return $results;
  304.     }
  305.     //Question Mail
  306.     //Test mise a jour lu et repondu
  307.     public function questionsMailMajMessage($serviceId$clientId$complementId$emailId$answered$isRead): ?array
  308.     {
  309.         $data = [
  310.             "serviceId" => $serviceId,
  311.             "clientId" => $clientId,
  312.             "complementId" => $complementId,
  313.             "emailsId" => $emailId,
  314.             "answered" => $answered,
  315.             "isRead" => $isRead
  316.         ];
  317.         $results $this->restApiPOSTcURL('/plateforme/questionsMailMajMessage' $data);
  318.         return $results;
  319.     }
  320.     //Info Mail
  321.     public function questionsMailConseillersListe($serviceId$clientId): ?array
  322.     {
  323.         $data = [
  324.             "serviceId" => $serviceId,
  325.             "clientId" => $clientId
  326.         ];
  327.         $results $this->restApiPOSTcURL('/plateforme/questionsMailConseillersListe' $data);
  328.         return $results;
  329.     }
  330.     //no route found for POST and GET
  331.     public function questionsMailConseiller($serviceId$clientid$complementId): ?array
  332.     {
  333.         $data = [
  334.             "serviceId" => $serviceId,
  335.             "clientId" => $clientid,
  336.             "complementId" => $complementId,
  337.         ];
  338.         $results $this->restApiPOSTcURL('/plateforme/questionsMailConseiller' $data);
  339.         return $results;
  340.     }
  341.     //Envoi de Mail
  342.     public function questionsMailEnvoiMessage($serviceId$clientId$complementId$titre$content$date): ?array
  343.     {
  344.         $data = [
  345.             "serviceId" => $serviceId,
  346.             "clientId" => $clientId,
  347.             "complementId" => $complementId,
  348.             "title" => $titre,
  349.             "content" => $content,
  350.             "date" => $date->format('Y-m-d H:i:s'),
  351.             "client" => "1",
  352.             "answered" => "0",
  353.             "isRead" => "0",
  354.             "messageCost" => "12"
  355.         ];
  356.         $results $this->restApiPOSTcURL('/plateforme/questionsMailEnvoiMessage' $data);
  357.         return $results;
  358.     }
  359.     //Reponse Mail
  360.     public function questionsMailConseillerReponse(): ?array
  361.     {
  362.         $data = [
  363.             "serviceId" => "1010",
  364.             "clientId" => "1006",
  365.             "complementId" => "45",
  366.             "title" => " réponse de test titre",
  367.             "content" => "il va faire beau",
  368.             "date" => "2022-02-22 02:22:22",
  369.             "client" => "0",
  370.             "answered" => "0",
  371.             "isRead" => "0"
  372.         ];
  373.         $results $this->restApiPOSTcURL('/plateforme/questionsMailConseillerReponse' $data);
  374.         return $results;
  375.     }
  376.     //Horoscope
  377.     public function horoscopeJour(): ?array
  378.     {
  379.         $data = [
  380.             "accountId"=> $this->accountId
  381.         ];
  382.         $results $this->restApiPOSTcURL('/horoscope/horoscopeJour' $data);
  383.         return $results;
  384.     }
  385.     public function horoscopeHebdomadaire(): ?array
  386.     {
  387.         $data = [
  388.             "accountId"=> $this->accountId
  389.         ];
  390.         $results $this->restApiPOSTcURL('/horoscope/horoscopeHebdomadaire' $data);
  391.         return $results;
  392.     }
  393.     public function horoscopeMensuel(): ?array
  394.     {
  395.         $data = [
  396.             "accountId"=> $this->accountId
  397.         ];
  398.         $results $this->restApiPOSTcURL('/horoscope/horoscopeMensuel' $data);
  399.         return $results;
  400.     }
  401.     public function restApiPOSTcURL($endpoint $data)
  402.     {
  403.         $results = [];
  404.         $this->apiToken $this->session->get('apiToken');
  405.         // ajouter les clés
  406.         $data['apiKey'] =  $this->apiKey;
  407.         $data['apiToken'] =  $this->apiToken;
  408.         $url $this->apiURL $endpoint;
  409.         $dataEncoded json_encode($data);
  410.         $headers = array(
  411.             "Accept: application/json",
  412.             "Content-Type: application/json",
  413.         );
  414.         $curl curl_init($url);
  415.         curl_setopt($curlCURLOPT_POSTtrue);
  416.         curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  417.         curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  418.         curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  419.         curl_setopt($curlCURLOPT_POSTFIELDS$dataEncoded);
  420.         curl_setopt($curlCURLOPT_REFERER$this->baseURL);
  421.         $response curl_exec($curl);
  422.         $curl_error curl_error($curl);
  423.         $curl_errno curl_errno($curl);
  424.         $http_code curl_getinfo($curlCURLINFO_HTTP_CODE);
  425.         curl_close($curl);
  426.         //echo "<br> url:". print_r($url, 1)."<br>";
  427.         if ($response === false) {
  428.             $res =  [ 'responseCode' => $curl_error'message' => $curl_errno ];
  429.             $results [throw new \Exception$res['message'] )];
  430.         } else {
  431.             if ($http_code === 200) {
  432.                 $results json_decode($responsetrue);
  433.             } else {
  434.                 $results json_decode($responsetrue);
  435.                 if (!is_array($results)) {
  436.                     $results =  [ 'responseCode' => $http_code'message' => $results ];
  437.                 }
  438.                 if ($http_code === 401) {
  439.                     // Accès non autorisé
  440.                 } else if ($http_code === 403) {
  441.                     // Aucun enregistrement correspondant
  442.                 } else if ($http_code === 404) {
  443.                     // Erreur sauvegarde
  444.                 } else if ($http_code === 409) {
  445.                     // Conflit 
  446.                 } else if ($http_code === 410) {
  447.                     // token n’est plus disponible
  448.                 }
  449.                 //$results [throw new \Exception( $res['message'] )];
  450.             }
  451.         }
  452.         return $results;
  453.     }
  454. }