<?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;
/* @var $router RouterInterface */
private $router;
public function __construct(SessionInterface $session, $apiKey, $accountId, $baseURL, $serviceId, $apiURL, RouterInterface $router)
{
$this->apiKey = $apiKey;
$this->apiToken = "";
$this->accountId = $accountId;
$this->serviceId = $serviceId;
$this->apiURL = $apiURL;
$this->baseURL = $baseURL;
$this->session = $session;
$this->router = $router;
}
public function postApi($endpoint, array $data): array
{
$url = "https://api-rest.media-technologies.fr/" . "$endpoint";
$httpHost = "https://demo.media-technologies.fr";
$dataEncoded = json_encode($data);
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_REFERER, $httpHost);
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);
$response = curl_exec($curl);
$curl_http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curl_error = curl_error($curl);
$curl_errno = curl_errno($curl);
curl_close($curl);
if ($response === false) {
throw new \Exception($curl_error, $curl_errno);
return ["null" => null];
} else {
$results = [];
// if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
// $results = json_decode($response, true);
// } else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 401) {
// throw $results = new \Exception('identifiant incorecte');
// } else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 404) {
// $response = json_decode($response, true);
// $results = $response;
// } else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 403) {
// $response = json_decode($response, true);
// $results = $response;
// }
switch ($curl_http_code) {
case 200:
$results = json_decode($response, true);
break;
case 401: // Accès non autorisé, l'apiKey n'existe pas
// a rediriger vers un page d'erreur qui dirait joindre le support, car c'est une erreur qui ne devrait jamais arrivée
die(new RedirectResponse($this->router->generate('app_login')));
//throw $results = new \Exception('identifiant incorrecte');
break;
case 403:
case 404:
$response = json_decode($response, true);
$results = $response;
break;
case 409: // Conflit avec le compte ID ou service ID, l'apiKey existe, mais un conflit avec les valeurs configurées
// a rediriger vers un page d'erreur qui dirait joindre le support, car c'est une erreur qui ne devrait jamais arrivée
die(new RedirectResponse($this->router->generate('app_login')));
break;
case 410: // apiToken n'exite pas ou la date est dépassée, vous devez démarrer une nouvelle session
die(new RedirectResponse($this->router->generate('app_login')));
break;
default:
$response = json_decode($response, true);
$results = $response;
break;
}
}
// $data = [ 'message' => "Accès non autorisé", 'responseCode' => "401" ]; l'apiKey n'existe pas
// $data = [ 'message' => "Conflit avec le référent HTTP", 'responseCode' => "409" ]; l'apiKey existe, mais un conflit avec les valeurs configurées
// $data = [ 'message' => "Conflit avec le compte ID", 'responseCode' => "409" ];
// $data = [ 'message' => "Conflit avec le service ID", 'responseCode' => "409" ];
// $data = [ 'message' => "token n’est plus disponible, vous devez démarrer une nouvelle session", "responseCode" => "410"}]
return $results;
}
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);
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 prestationEcriteMails($accountId, $serviceId): array
{
$data = [
"accountId" => $accountId,
"serviceId" => $serviceId
];
$results = $this->restApiPOSTcURL('/paramaters/PrestationEcriteMails', $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, $reqToken): array
{
$data = [
"apiToken" => $reqToken,
"serviceId" => $serviceId,
"clientId" => $clientId
];
$results = $this->restApiPOSTcURLWithOUtToken('/plateforme/clientProfile', $data);
return $results;
}
public function clientProfileCheck($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, $cost): ?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" => $cost
];
$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 request($data, $endpoint): ?array
{
$results = $this->restApiPOSTcURL($endpoint, $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;
}
public function restApiPOSTcURLWithOUtToken($endpoint, $data)
{
$results = [];
$this->apiToken = $this->session->get('apiToken');
// ajouter les clés
$data['apiKey'] = $this->apiKey;
$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;
}
}