Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Andrew Bekesh
Added: May 4, 2020 1:30 PM
Views: 8
Tags: no tags
  1. <?php
  2.  
  3. namespace Mtc\Core;
  4.  
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\Psr7\Response;
  7. use Mtc\Shop\TitanPos\Models\EposDebug;
  8.  
  9. class RestApi {
  10.     /*
  11.      * Guzzle client GuzzleHttp\Client;
  12.      */
  13.     private $client = null;
  14.  
  15.     /*
  16.      * Options for Guzzle client
  17.      */
  18.     private $defaultOptions = [
  19.         'timeout' => 60,
  20.         'connect_timeout' => 5,
  21.     ];
  22.  
  23.     /*
  24.      * Data that would be present on every request like auth details
  25.      */
  26.     private $defaultBody = [];
  27.  
  28.     /*
  29.      * Final request data excluding options
  30.      */
  31.     private $requestBody = [];
  32.  
  33.     /*
  34.      * Final request options excluding data
  35.      */
  36.     private $requestOptions = [];
  37.  
  38.     /*
  39.      * Should we send this as json request
  40.      */
  41.     private $isJsonRequest = false;
  42.  
  43.     /*
  44.      * Automatically reset api body/options on each end of the call
  45.      * - Clears custom body, options, json flags etc.
  46.      * - Does not reset default body/options
  47.      */
  48.     private $autoReset = true;
  49.  
  50.     /*
  51.      * Last requests response
  52.      */
  53.     private $response = null;
  54.  
  55.     /**
  56.      * RestApi constructor.
  57.      * @param $endpointBase
  58.      * @param array $guzzleOptions
  59.      * @param array $defaultBody
  60.      * @param bool $json
  61.      * @param bool $autoReset
  62.      */
  63.     public function __construct($endpointBase, array $guzzleOptions = [], array $defaultBody = [], $json = false, $autoReset = true)
  64.     {
  65.         $this->setEndpointBase($endpointBase);
  66.         $this->reset($guzzleOptions);
  67.         $this->setDefaultBody($defaultBody);
  68.         $this->isJsonRequest = (bool) $json;
  69.     }
  70.  
  71.     /**
  72.      * @param string $endpointBase
  73.      */
  74.     private function setEndpointBase($endpointBase)
  75.     {
  76.         $this->defaultOptions['base_uri'] = strrev($endpointBase)[0] == '/' ? $endpointBase : $endpointBase . '/';
  77.     }
  78.  
  79.     /**
  80.      * @param string $defaultBody
  81.      */
  82.     private function setDefaultBody($defaultBody)
  83.     {
  84.         $this->defaultBody = $defaultBody;
  85.     }
  86.  
  87.     /*
  88.      * Reset Guzzle Client and API object to basic state
  89.      * Will retain only defaultOptions + options passed to this method
  90.      * - Guzzle 6+ has immutable client (this is why we need to build new one)
  91.      */
  92.     public function reset(array $guzzleOptions = [], $json = null)
  93.     {
  94.         $this->requestBody = $this->requestOptions = [];
  95.  
  96.         $this->options($guzzleOptions);
  97.  
  98.         $options = array_merge($this->defaultOptions, $this->requestOptions);
  99.         $this->client = new Client($options);
  100.  
  101.         if (!is_null($json)) {
  102.             $this->isJsonRequest = (bool) $json;
  103.         }
  104.  
  105.         return $this;
  106.     }
  107.  
  108.     /*
  109.      * Add request body to Guzzle Client
  110.      * Also acts as json body if json has been enabled on the reset or initialization call
  111.      */
  112.     public function body(array $body = [])
  113.     {
  114.         $this->requestBody = $body;
  115.  
  116.         return $this;
  117.     }
  118.  
  119.     /*
  120.      * Add Guzzle Client options
  121.      * - form_params is stripped from options use body() method for that
  122.      * - base_uri is stripped for security, initialize another api object for different base_uri
  123.      *
  124.      * @see http://docs.guzzlephp.org/en/stable/request-options.html
  125.      */
  126.     public function options(array $options = [])
  127.     {
  128.         if (isset($options['form_params'])) {
  129.             unset($options['form_params']);
  130.         }
  131.  
  132.         if (isset($options['base_uri'])) {
  133.             unset($options['base_uri']);
  134.         }
  135.  
  136.         $this->requestOptions = $options;
  137.  
  138.         return $this;
  139.     }
  140.  
  141.     /**
  142.      * Retrieve latest performed api calls response
  143.      * - bodyOnly (true) | Set to false to retrieve whole object of response
  144.      * - decodeJson (false) | Set to true to decode json encoded response body
  145.      *   only available if bodyOnly = true
  146.      *
  147.      * @param bool $bodyOnly
  148.      * @param bool $decodeJson
  149.      * @return Response|mixed|null
  150.      */
  151.     public function getResponse($bodyOnly = true, $decodeJson = false)
  152.     {
  153.         $contents = $this->response;
  154.  
  155.         if ($bodyOnly === true) {
  156.             if ($decodeJson === true) {
  157.                 return \GuzzleHttp\json_decode($contents->getBody()->getContents(), true);
  158.  
  159.             }
  160.  
  161.             return $contents->getBody()->getContents();
  162.         }
  163.  
  164.         return $contents;
  165.     }
  166.  
  167.     /**
  168.      * Rely any post, get, put, update etc. calls to the
  169.      * We will populate this with our options, body contents and such
  170.      *
  171.      * @param $method
  172.      * @param $arguments
  173.      * @return mixed|null
  174.      */
  175.     public function __call($method, $arguments)
  176.     {
  177.         $body = array_merge($this->defaultBody, $this->requestBody);
  178.         $options = array_merge($this->defaultOptions, $this->requestOptions);;
  179.  
  180.         if ($this->isJsonRequest) {
  181.             $options['json'] = $body;
  182.         } else {
  183.             $options['form_params'] = $body;
  184.         }
  185.  
  186.         $arguments[] = $options;
  187.  
  188.         $this->response = call_user_func_array([$this->client, $method], $arguments);
  189.  
  190.         // Reset api at this point
  191.         if ($this->autoReset === true) {
  192.             $this->reset([], false);
  193.         }
  194.  
  195.         return $this->response;
  196.     }
  197.  
  198.     public function saveDebugInfo(array $debug_info = [])
  199.     {
  200.         if (count($debug_info) == 0) {
  201.             return false;
  202.         }
  203.  
  204.         // Log debug info under any response type
  205.         EposDebug::create($debug_info);
  206.  
  207.         return true;
  208.     }
  209. }
  210.