<?php
namespace App\Services;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CallApiServices
{
private $apiKey;
private $apiToken;
private $accountId;
private $serviceId;
private $apiURL;
private $baseURL;
private $session;
public function __construct(SessionInterface $session, $apiKey, $accountId, $baseURL, $serviceId, $apiURL )
{
$this->apiKey = $apiKey;
$this->apiToken = "";
$this->accountId = $accountId;
$this->serviceId = $serviceId;
$this->apiURL = $apiURL;
$this->baseURL = $baseURL;
$this->session = $session;
}
public function getData(string $colletionName): array
{
$url = 'http://localhost:8080/api';
$request_url = $url . '/' . $colletionName;
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if ($response === false) {
return null;
} else {
$results = [];
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
$response = json_decode($response, true);
}
}
curl_close($curl);
return $response;
}
public function getDataById(string $colletionName, string $id): array
{
$url = 'http://localhost:8080/api';
$request_url = $url . '/' . $colletionName . '/' . $id;
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if ($response === false) {
return null;
} else {
$results = [];
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
$response = json_decode($response, true);
}
}
curl_close($curl);
return $response;
}
public function authApi(string $login, string $password): array
{
$url = 'http://localhost:8080/api/login';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
);
$data = [
"username" => $login,
"password" => $password
];
$dataEncoded = json_encode($data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataEncoded);
$response = curl_exec($curl);
if ($response === false) {
return ["null" => null];
} else {
$results = [];
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
$response = json_decode($response, true);
} else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 401) {
throw new \Exception('identifiant incorecte');
}
}
curl_close($curl);
return $response;
}
public function getUserDetails(string $token): array
{
$url = 'http://localhost:8080/api/me';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Bearer " . $token . ""
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
if ($response === false) {
return ["null" => null];
} else {
$results = [];
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
$response = json_decode($response, true);
}
}
curl_close($curl);
return $response;
}
// ** +++++++++++++++++ ------------ --------- +++++++++++++++++ **
// ** +++++++++++++++++ ------------ --------- +++++++++++++++++ **
public function getListConseillers($accountId, $serviceId): array
{
$data = [
"accountId" => $accountId,
"serviceId" => $serviceId
];
$results = $this->restApiPOSTcURL('/plateforme/listeConseillersEtPresence' , $data);
//dd($results);
return $results;
}
public function configuratationPlateforme($accountId, $serviceId): array
{
$data = [
"accountId" => $accountId,
"serviceId" => $serviceId
];
$results = $this->restApiPOSTcURL('/plateforme/configuratationPlateforme' , $data);
return $results;
}
public function conseillerEtPlanning($pseudoId, $serviceId): array
{
$data = [
"complementId" => $pseudoId,
"serviceId" => $serviceId
];
$results = $this->restApiPOSTcURL('/plateforme/conseillerEtPlanning' , $data);
return $results;
}
public function clientAuthentification(string $login, string $password, $serviceId): array
{
$data = [
"serviceId" => $serviceId,
"login" => $login,
"password" => $password
];
$results = $this->restApiPOSTcURL('/plateforme/clientAuthentification' , $data);
if (isset($results['responseCode']) && ($results['responseCode']!=200)) {
return $results;
}
$this->session->set('apiToken', $results["apiToken"]);
return $results;
}
//Avis
public function conseillerAvis($serviceId, $complementId): array
{
$data = [
"serviceId" => $serviceId,
"complementId" => $complementId
];
$results = $this->restApiPOSTcURL('/plateforme/conseillerAvis' , $data);
return $results;
}
public function clientAvis($serviceId, $clientId): array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURL('/plateforme/clientAvis' , $data);
return $results;
}
public function listePlateformeAvis($serviceId): array
{
$data = [
"serviceId" => $serviceId,
];
$results = $this->restApiPOSTcURL('/plateforme/listePlateformeAvis' , $data);
return $results;
}
public function clientOperations($serviceId, $clientId): array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURL('/plateforme/clientOperations' , $data);
return $results;
}
public function clientProfile($serviceId, $clientId): array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURL('/plateforme/clientProfile' , $data);
return $results;
}
public function clientTransactions($serviceId, $clientId): array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURL('/plateforme/clientTransactions' , $data);
return $results;
}
public function clientConsultations($serviceId, $clientId): array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURL('/plateforme/clientConsultations' , $data);
return $results;
}
public function clientInscription($serviceId, $parrainID, $genre, $email, $password, $firtsname, $lastname, $phoneNumber): ?array
{
$data = [
"serviceId" => $serviceId,
"parrainId" => $parrainID,
"genre" => $genre,
"username" => $email,
"password" => $password,
"nom" => $firtsname,
"prenom" => $lastname,
"pays" => "FRANCE",
"telephone" => $phoneNumber
];
$results = $this->restApiPOSTcURL('/plateforme/clientInscription' , $data);
return $results;
}
public function socialInscription($serviceId, $genre, $email, $password, $firtsname, $lastname, $country, $phoneNumber, $social, $socialUid): ?array
{
$data = [
"apiKey" => $this->apiKey,
"serviceId" => $serviceId,
"parrainId" => "0",
"genre" => $genre,
"username" => $email,
"password" => $password,
"nom" => $firtsname,
"prenom" => $lastname,
"pays" => $country,
"telephone" => $phoneNumber,
"social" => $social,
"socialUid" => $socialUid
];
$results = $this->restApiPOSTcURL('/plateforme/clientInscription' , $data);
return $results;
}
public function clientForgotPassword($serviceId, $username): ?array
{
$data = [
"serviceId" => $serviceId,
"username" => $username
];
$results = $this->restApiPOSTcURL('/plateforme/clientForgotPassword' , $data);
return $results;
}
public function clientResetPassword($serviceId, $username, $oldPassword, $newPassword, $reqToken, $limitToken): ?array
{
$data = [
"serviceId" => $serviceId,
"username" => $username,
"oldPassword" => $oldPassword,
"newPassword" => $newPassword
];
$results = $this->restApiPOSTcURL('/plateforme/clientResetPassword' , $data);
return $results;
}
public function clientInscriptionValidation($serviceId, $email, $password, $code, $actif): ?array
{
$data = [
"serviceId" => $serviceId,
"username" => $email,
"password" => $password,
"code" => $code,
"actif" => $actif
];
$results = $this->restApiPOSTcURL('/plateforme/clientInscriptionValidation' , $data);
return $results;
}
//Tarifs
//Tous les tarifs
public function tarifsPlateforme($serviceId): ?array
{
$data = [
"serviceId" => $serviceId,
];
$results = $this->restApiPOSTcURL('/plateforme/tarifsPlateforme' , $data);
return $results;
}
public function cbTransaction($data): ?array
{
$results = $this->restApiPOSTcURL('/plateforme/cbTransaction' , $data);
return $results;
}
//Question Mail
//Test mise a jour lu et repondu
public function questionsMailMajMessage($serviceId, $clientId, $complementId, $emailId, $answered, $isRead): ?array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId,
"complementId" => $complementId,
"emailsId" => $emailId,
"answered" => $answered,
"isRead" => $isRead
];
$results = $this->restApiPOSTcURL('/plateforme/questionsMailMajMessage' , $data);
return $results;
}
//Info Mail
public function questionsMailConseillersListe($serviceId, $clientId): ?array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURL('/plateforme/questionsMailConseillersListe' , $data);
return $results;
}
//no route found for POST and GET
public function questionsMailConseiller($serviceId, $clientid, $complementId): ?array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientid,
"complementId" => $complementId,
];
$results = $this->restApiPOSTcURL('/plateforme/questionsMailConseiller' , $data);
return $results;
}
//Envoi de Mail
public function questionsMailEnvoiMessage($serviceId, $clientId, $complementId, $titre, $content, $date): ?array
{
$data = [
"serviceId" => $serviceId,
"clientId" => $clientId,
"complementId" => $complementId,
"title" => $titre,
"content" => $content,
"date" => $date->format('Y-m-d H:i:s'),
"client" => "1",
"answered" => "0",
"isRead" => "0",
"messageCost" => "12"
];
$results = $this->restApiPOSTcURL('/plateforme/questionsMailEnvoiMessage' , $data);
return $results;
}
//Reponse Mail
public function questionsMailConseillerReponse(): ?array
{
$data = [
"serviceId" => "1010",
"clientId" => "1006",
"complementId" => "45",
"title" => " réponse de test titre",
"content" => "il va faire beau",
"date" => "2022-02-22 02:22:22",
"client" => "0",
"answered" => "0",
"isRead" => "0"
];
$results = $this->restApiPOSTcURL('/plateforme/questionsMailConseillerReponse' , $data);
return $results;
}
//Horoscope
public function horoscopeJour(): ?array
{
$data = [
"accountId"=> $this->accountId
];
$results = $this->restApiPOSTcURL('/horoscope/horoscopeJour' , $data);
return $results;
}
public function horoscopeHebdomadaire(): ?array
{
$data = [
"accountId"=> $this->accountId
];
$results = $this->restApiPOSTcURL('/horoscope/horoscopeHebdomadaire' , $data);
return $results;
}
public function horoscopeMensuel(): ?array
{
$data = [
"accountId"=> $this->accountId
];
$results = $this->restApiPOSTcURL('/horoscope/horoscopeMensuel' , $data);
return $results;
}
public function restApiPOSTcURL($endpoint , $data)
{
$results = [];
$this->apiToken = $this->session->get('apiToken');
// ajouter les clés
$data['apiKey'] = $this->apiKey;
$data['apiToken'] = $this->apiToken;
$url = $this->apiURL . $endpoint;
$dataEncoded = json_encode($data);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataEncoded);
curl_setopt($curl, CURLOPT_REFERER, $this->baseURL);
$response = curl_exec($curl);
$curl_error = curl_error($curl);
$curl_errno = curl_errno($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
//echo "<br> url:". print_r($url, 1)."<br>";
if ($response === false) {
$res = [ 'responseCode' => $curl_error, 'message' => $curl_errno ];
$results [throw new \Exception( $res['message'] )];
} else {
if ($http_code === 200) {
$results = json_decode($response, true);
} else {
$results = json_decode($response, true);
if (!is_array($results)) {
$results = [ 'responseCode' => $http_code, 'message' => $results ];
}
if ($http_code === 401) {
// Accès non autorisé
} else if ($http_code === 403) {
// Aucun enregistrement correspondant
} else if ($http_code === 404) {
// Erreur sauvegarde
} else if ($http_code === 409) {
// Conflit
} else if ($http_code === 410) {
// token n’est plus disponible
}
//$results [throw new \Exception( $res['message'] )];
}
}
return $results;
}
}