<?php
namespace App\Service;
use App\Model\User\User;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class APIKiwi
{
public const TIMEOUT_VALUE = 60;
/**
* @var HttpClientInterface
*/
private $client;
/**
* @var string
*/
private $apiKiwiUrl;
/**
* @var string
*/
private $apiProjectName;
private $apiPublicKey;
private $apiAdminKey;
private SerializerInterface $serializer;
public function __construct(
HttpClientInterface $client,
$apiKiwiUrl,
$apiProjectName,
$apiPublicKey,
$apiAdminKey,
SerializerInterface $serializer
)
{
$this->client = $client;
$this->apiKiwiUrl = $apiKiwiUrl;
$this->apiProjectName = $apiProjectName;
$this->apiPublicKey = $apiPublicKey;
$this->apiAdminKey = $apiAdminKey;
$this->serializer = $serializer;
}
public function getKiwi($params, $getResponse = true)
{
try {
$response = $this->client->request(
'GET',
$this->apiKiwiUrl . $params['path'],
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $params['token'],
'X-Front-Name' => $this->apiProjectName,
]
]
);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
if( $getResponse === true ) {
return new Response($response->getContent(), $response->getStatusCode());
}
return $response;
}
public function getPublicKiwi($endpoint, array $query = [])
{
try {
$params['headers'] = [
'Content-Type' => 'application/json',
'X-AUTH-TOKEN' => $this->apiPublicKey,
'X-Front-Name' => $this->apiProjectName,
];
if($query) {
$params['query'] = $query;
}
$response = $this->client->request(
'GET',
$this->apiKiwiUrl.'/public/' . $endpoint,
$params
);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
return new Response($response->getContent(), $response->getStatusCode());
}
/**
* @throws TransportExceptionInterface
*/
public function postAdminKiwi($params): ResponseInterface
{
$data = [
'headers' => [
'Content-Type' => 'application/json',
'X-AUTH-TOKEN-ADMIN-API' => $this->apiAdminKey ?? false,
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
];
if (array_key_exists('data', $params)) {
$data['body'] = json_encode($params['data']);
}
return $this->client->request(
'POST',
$this->apiKiwiUrl . $params['path'],
$data
);
}
public function postPublicKiwi($params)
{
try {
$data = [
'headers' => [
'Content-Type' => 'application/json',
'X-AUTH-TOKEN' => $this->apiPublicKey,
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
];
if (array_key_exists('data', $params)) {
$data['body'] = json_encode($params['data']);
}
$response = $this->client->request(
'POST',
$this->apiKiwiUrl . $params['path'],
$data
);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
return new Response($response->getContent(), $response->getStatusCode());
}
public function postKiwi($params)
{
try {
$data = [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $params['token'],
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
];
if (array_key_exists('data', $params)) {
$data['body'] = json_encode($params['data']);
}
$response = $this->client->request(
'POST',
$this->apiKiwiUrl . $params['path'],
$data
);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
return new Response($response->getContent(), $response->getStatusCode());
}
public function postFileKiwi($params)
{
try {
$data = [
'headers' => [
'Content-Type' => $params['data']['Content-Type'],
'Authorization' => 'Bearer ' . $params['token'],
'X-Front-Name' => $this->apiProjectName,
],
'body' => $params['data']['body'],
'timeout' => self::TIMEOUT_VALUE,
];
$response = $this->client->request(
'POST',
$this->apiKiwiUrl . $params['path'],
$data
);
return new Response($response->getContent(), $response->getStatusCode());
} catch (\Exception $e) {
return new Response($e->getMessage(), $e->getCode());
}
}
public function putPublicKiwi($url, $params)
{
try {
$data = [
'headers' => [
'Content-Type' => 'application/json',
'X-AUTH-TOKEN' => $this->apiPublicKey,
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
];
if (array_key_exists('data', $params)) {
$data['body'] = json_encode($params['data']);
}
$response = $this->client->request(
'PUT',
$this->apiKiwiUrl .'/public' . $url,
$data
);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
return new Response($response->getContent(), $response->getStatusCode());
}
public function putKiwi($params)
{
try {
$data = [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $params['token'],
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
];
if (array_key_exists('data', $params)) {
$data['body'] = json_encode($params['data']);
}
$response = $this->client->request(
'PUT',
$this->apiKiwiUrl . $params['path'],
$data
);
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
return new Response($response->getContent(), $response->getStatusCode());
}
public function postLogin($params)
{
$response = $this->client->request(
'POST',
$this->apiKiwiUrl.'/authentication_token',
[
'body' => json_encode($params),
'headers' => [
'Content-Type' => 'application/json',
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
]
);
return new Response($response->getContent(), $response->getStatusCode());
}
/**
* @param $params
* @param $endpoint
* @return ResponseInterface
* @throws TransportExceptionInterface
*/
public function postPublic($params, $endpoint)
{
return $this->client->request(
'POST',
$this->apiKiwiUrl.'/public/' . $endpoint,
[
'body' => $params,
'headers' => [
'Content-Type' => 'application/json',
'X-Front-Name' => $this->apiProjectName,
'X-AUTH-TOKEN' => $_ENV['API_KIWI_PUBLIC_KEY'] ?? null
],
'timeout' => self::TIMEOUT_VALUE,
]
);
}
public function requestKiwi($method, $params)
{
$options = [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $params['token'] ?? false,
'X-Front-Name' => $this->apiProjectName,
]
];
if(isset($params['body'])) {
$options['body'] = is_array($params['body']) ? json_encode($params['body']) : $params['body'];
}
if(isset($params['query'])) {
$options['query'] = $params['query'];
}
return $this->client->request(
$method,
$this->apiKiwiUrl . $params['path'] ?? '',
$options
);
}
public function register($params)
{
$params = json_decode($params, true);
if (array_key_exists('referent', $params)) {
$params['referent'] = '/api/referents/'.$params['referent']['id'];
}
return $this->postPublic(json_encode($params), 'users');
}
public function getAvailableGame($user)
{
$query = [
'online' => true,
'startDate[strictly_before]' => date('Y-m-d H:i:s'),
'endDate[strictly_after]' => date('Y-m-d H:i:s')
];
if( $user instanceof User) {
$gameResponse = $this->requestKiwi(
'GET',
[
'token' => $user->getToken(),
'path' => '/games',
'query' => $query
]
);
} else {
$gameResponse = $this->getPublicKiwi(
'games',
$query
);
}
// retrieve current game
$gameResponse = json_decode( $gameResponse->getContent(), true);
$gameContent = $gameResponse["hydra:member"][0] ?? false;
return $gameContent;
}
public function getUserData(UserInterface $user): User
{
$userResp = $this->getKiwi(['path' => '/users/'.$user->getId(), 'token' => $user->getToken()]);
return $this->serializer->deserialize($userResp->getContent(), User::class, 'json', DeserializationContext::create()->setGroups(['get_user']));
}
public function postLoginAfterRegistration($params)
{
$response = $this->client->request(
'POST',
$this->apiKiwiUrl.'/authentication_token',
[
'body' => json_encode($params),
'headers' => [
'Content-Type' => 'application/json',
'X-Front-Name' => $this->apiProjectName,
],
'timeout' => self::TIMEOUT_VALUE,
]
);
return new Response($response->getContent(), $response->getStatusCode());
}
}