src/Service/APIKiwi.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Model\User\User;
  4. use JMS\Serializer\DeserializationContext;
  5. use JMS\Serializer\SerializerInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  9. use Symfony\Contracts\HttpClient\HttpClientInterface;
  10. use Symfony\Contracts\HttpClient\ResponseInterface;
  11. class APIKiwi
  12. {
  13.     public const TIMEOUT_VALUE 60;
  14.     /**
  15.      * @var HttpClientInterface
  16.      */
  17.     private $client;
  18.     /**
  19.      * @var string
  20.      */
  21.     private $apiKiwiUrl;
  22.     /**
  23.      * @var string
  24.      */
  25.     private $apiProjectName;
  26.     private $apiPublicKey;
  27.     private $apiAdminKey;
  28.     private SerializerInterface $serializer;
  29.     public function __construct(
  30.         HttpClientInterface $client,
  31.         $apiKiwiUrl,
  32.         $apiProjectName,
  33.         $apiPublicKey,
  34.         $apiAdminKey,
  35.         SerializerInterface $serializer
  36.     )
  37.     {
  38.         $this->client $client;
  39.         $this->apiKiwiUrl $apiKiwiUrl;
  40.         $this->apiProjectName $apiProjectName;
  41.         $this->apiPublicKey $apiPublicKey;
  42.         $this->apiAdminKey $apiAdminKey;
  43.         $this->serializer $serializer;
  44.     }
  45.     public function getKiwi($params$getResponse true)
  46.     {
  47.         try {
  48.             $response $this->client->request(
  49.                 'GET',
  50.                 $this->apiKiwiUrl $params['path'],
  51.                 [
  52.                     'headers' => [
  53.                         'Content-Type' => 'application/json',
  54.                         'Authorization' => 'Bearer ' $params['token'],
  55.                         'X-Front-Name' => $this->apiProjectName,
  56.                     ]
  57.                 ]
  58.             );
  59.         } catch (\Exception $e) {
  60.             throw new \Exception($e->getMessage());
  61.         }
  62.         if( $getResponse === true ) {
  63.             return new Response($response->getContent(), $response->getStatusCode());
  64.         }
  65.         return $response;
  66.     }
  67.     public function getPublicKiwi($endpoint, array $query = [])
  68.     {
  69.         try {
  70.             $params['headers'] = [
  71.                 'Content-Type' => 'application/json',
  72.                 'X-AUTH-TOKEN' => $this->apiPublicKey,
  73.                 'X-Front-Name' => $this->apiProjectName,
  74.             ];
  75.             if($query) {
  76.                 $params['query'] = $query;
  77.             }
  78.             $response $this->client->request(
  79.                 'GET',
  80.                 $this->apiKiwiUrl.'/public/' $endpoint,
  81.                 $params
  82.             );
  83.         } catch (\Exception $e) {
  84.             throw new \Exception($e->getMessage());
  85.         }
  86.         return new Response($response->getContent(), $response->getStatusCode());
  87.     }
  88.     /**
  89.      * @throws TransportExceptionInterface
  90.      */
  91.     public function postAdminKiwi($params): ResponseInterface
  92.     {
  93.         $data = [
  94.             'headers' => [
  95.                 'Content-Type' => 'application/json',
  96.                 'X-AUTH-TOKEN-ADMIN-API' => $this->apiAdminKey ?? false,
  97.                 'X-Front-Name' => $this->apiProjectName,
  98.             ],
  99.             'timeout' => self::TIMEOUT_VALUE,
  100.         ];
  101.         if (array_key_exists('data'$params)) {
  102.             $data['body'] = json_encode($params['data']);
  103.         }
  104.         return $this->client->request(
  105.             'POST',
  106.             $this->apiKiwiUrl $params['path'],
  107.             $data
  108.         );
  109.     }
  110.     public function postPublicKiwi($params)
  111.     {
  112.         try {
  113.             $data = [
  114.                 'headers' => [
  115.                     'Content-Type' => 'application/json',
  116.                     'X-AUTH-TOKEN' => $this->apiPublicKey,
  117.                     'X-Front-Name' => $this->apiProjectName,
  118.                 ],
  119.                 'timeout' => self::TIMEOUT_VALUE,
  120.             ];
  121.             if (array_key_exists('data'$params)) {
  122.                 $data['body'] = json_encode($params['data']);
  123.             }
  124.             $response $this->client->request(
  125.                 'POST',
  126.                 $this->apiKiwiUrl $params['path'],
  127.                 $data
  128.             );
  129.         } catch (\Exception $e) {
  130.             throw new \Exception($e->getMessage());
  131.         }
  132.         return new Response($response->getContent(), $response->getStatusCode());
  133.     }
  134.     public function postKiwi($params)
  135.     {
  136.         try {
  137.             $data = [
  138.                 'headers' => [
  139.                     'Content-Type' => 'application/json',
  140.                     'Authorization' => 'Bearer ' $params['token'],
  141.                     'X-Front-Name' => $this->apiProjectName,
  142.                 ],
  143.                 'timeout' => self::TIMEOUT_VALUE,
  144.             ];
  145.             if (array_key_exists('data'$params)) {
  146.                 $data['body'] = json_encode($params['data']);
  147.             }
  148.             $response $this->client->request(
  149.                 'POST',
  150.                 $this->apiKiwiUrl $params['path'],
  151.                 $data
  152.             );
  153.         } catch (\Exception $e) {
  154.             throw new \Exception($e->getMessage());
  155.         }
  156.         return new Response($response->getContent(), $response->getStatusCode());
  157.     }
  158.     public function postFileKiwi($params)
  159.     {
  160.         try {
  161.             $data = [
  162.                 'headers' => [
  163.                     'Content-Type' => $params['data']['Content-Type'],
  164.                     'Authorization' => 'Bearer ' $params['token'],
  165.                     'X-Front-Name' => $this->apiProjectName,
  166.                 ],
  167.                 'body' => $params['data']['body'],
  168.                 'timeout' => self::TIMEOUT_VALUE,
  169.             ];
  170.             $response $this->client->request(
  171.                 'POST',
  172.                 $this->apiKiwiUrl $params['path'],
  173.                 $data
  174.             );
  175.             return new Response($response->getContent(), $response->getStatusCode());
  176.         } catch (\Exception $e) {
  177.             return new Response($e->getMessage(), $e->getCode());
  178.         }
  179.     }
  180.     public function putPublicKiwi($url$params)
  181.     {
  182.         try {
  183.             $data = [
  184.                 'headers' => [
  185.                     'Content-Type' => 'application/json',
  186.                     'X-AUTH-TOKEN' => $this->apiPublicKey,
  187.                     'X-Front-Name' => $this->apiProjectName,
  188.                 ],
  189.                 'timeout' => self::TIMEOUT_VALUE,
  190.             ];
  191.             if (array_key_exists('data'$params)) {
  192.                 $data['body'] = json_encode($params['data']);
  193.             }
  194.             $response $this->client->request(
  195.                 'PUT',
  196.                 $this->apiKiwiUrl .'/public' $url,
  197.                 $data
  198.             );
  199.         } catch (\Exception $e) {
  200.             throw new \Exception($e->getMessage());
  201.         }
  202.         return new Response($response->getContent(), $response->getStatusCode());
  203.     }
  204.     public function putKiwi($params)
  205.     {
  206.         try {
  207.             $data = [
  208.                 'headers' => [
  209.                     'Content-Type' => 'application/json',
  210.                     'Authorization' => 'Bearer ' $params['token'],
  211.                     'X-Front-Name' => $this->apiProjectName,
  212.                 ],
  213.                 'timeout' => self::TIMEOUT_VALUE,
  214.             ];
  215.             if (array_key_exists('data'$params)) {
  216.                 $data['body'] = json_encode($params['data']);
  217.             }
  218.             $response $this->client->request(
  219.                 'PUT',
  220.                 $this->apiKiwiUrl $params['path'],
  221.                 $data
  222.             );
  223.         } catch (\Exception $e) {
  224.             throw new \Exception($e->getMessage());
  225.         }
  226.         return new Response($response->getContent(), $response->getStatusCode());
  227.     }
  228.     public function postLogin($params)
  229.     {
  230.         $response $this->client->request(
  231.             'POST',
  232.             $this->apiKiwiUrl.'/authentication_token',
  233.             [
  234.                 'body' => json_encode($params),
  235.                 'headers' => [
  236.                     'Content-Type' => 'application/json',
  237.                     'X-Front-Name' => $this->apiProjectName,
  238.                 ],
  239.                 'timeout' => self::TIMEOUT_VALUE,
  240.             ]
  241.         );
  242.         return new Response($response->getContent(), $response->getStatusCode());
  243.     }
  244.     /**
  245.      * @param $params
  246.      * @param $endpoint
  247.      * @return ResponseInterface
  248.      * @throws TransportExceptionInterface
  249.      */
  250.     public function postPublic($params$endpoint)
  251.     {
  252.         return $this->client->request(
  253.             'POST',
  254.             $this->apiKiwiUrl.'/public/' $endpoint,
  255.             [
  256.                 'body' => $params,
  257.                 'headers' => [
  258.                     'Content-Type' => 'application/json',
  259.                     'X-Front-Name' => $this->apiProjectName,
  260.                     'X-AUTH-TOKEN' => $_ENV['API_KIWI_PUBLIC_KEY'] ?? null
  261.                 ],
  262.                 'timeout' => self::TIMEOUT_VALUE,
  263.             ]
  264.         );
  265.     }
  266.     public function requestKiwi($method$params)
  267.     {
  268.         $options = [
  269.             'headers' => [
  270.                 'Content-Type' => 'application/json',
  271.                 'Authorization' => 'Bearer ' $params['token'] ?? false,
  272.                 'X-Front-Name' => $this->apiProjectName,
  273.             ]
  274.         ];
  275.         if(isset($params['body'])) {
  276.             $options['body'] = is_array($params['body']) ? json_encode($params['body']) : $params['body'];
  277.         }
  278.         if(isset($params['query'])) {
  279.             $options['query'] = $params['query'];
  280.         }
  281.         return $this->client->request(
  282.             $method,
  283.             $this->apiKiwiUrl $params['path'] ?? '',
  284.             $options
  285.         );
  286.     }
  287.     public function register($params)
  288.     {
  289.         $params json_decode($paramstrue);
  290.         if (array_key_exists('referent'$params)) {
  291.             $params['referent'] = '/api/referents/'.$params['referent']['id'];
  292.         }
  293.         return $this->postPublic(json_encode($params), 'users');
  294.     }
  295.     public function getAvailableGame($user)
  296.     {
  297.         $query = [
  298.             'online' => true,
  299.             'startDate[strictly_before]' => date('Y-m-d H:i:s'),
  300.             'endDate[strictly_after]' => date('Y-m-d H:i:s')
  301.         ];
  302.         if( $user instanceof User) {
  303.             $gameResponse $this->requestKiwi(
  304.                 'GET',
  305.                 [
  306.                     'token' => $user->getToken(),
  307.                     'path' => '/games',
  308.                     'query' => $query
  309.                 ]
  310.             );
  311.         } else  {
  312.             $gameResponse $this->getPublicKiwi(
  313.                 'games',
  314.                 $query
  315.             );
  316.         }
  317.         // retrieve current game
  318.         $gameResponse json_decode$gameResponse->getContent(), true);
  319.         $gameContent $gameResponse["hydra:member"][0] ??  false;
  320.         return $gameContent;
  321.     }
  322.     public function getUserData(UserInterface $user): User
  323.     {
  324.         $userResp $this->getKiwi(['path' => '/users/'.$user->getId(), 'token' => $user->getToken()]);
  325.         return $this->serializer->deserialize($userResp->getContent(), User::class, 'json'DeserializationContext::create()->setGroups(['get_user']));
  326.     }
  327.     public function postLoginAfterRegistration($params)
  328.     {
  329.         $response $this->client->request(
  330.             'POST',
  331.             $this->apiKiwiUrl.'/authentication_token',
  332.             [
  333.                 'body' => json_encode($params),
  334.                 'headers' => [
  335.                     'Content-Type' => 'application/json',
  336.                     'X-Front-Name' => $this->apiProjectName,
  337.                 ],
  338.                 'timeout' => self::TIMEOUT_VALUE,
  339.             ]
  340.         );
  341.         return new Response($response->getContent(), $response->getStatusCode());
  342.     }
  343. }