vendor/payneticsteam/paynetics-bundle/src/Paynetics/HttpRequest/AbstractHttpRequest.php line 170

Open in your IDE?
  1. <?php
  2. /** @noinspection MagicMethodsValidityInspection */
  3. namespace Paynetics\HttpRequest;
  4. use JsonException;
  5. use Paynetics\Exception\ApiException;
  6. use Paynetics\Helper\RequestMethods;
  7. use Paynetics\Helper\SerializationTrait;
  8. use Paynetics\Helper\URLParser;
  9. use GuzzleHttp\Client;
  10. use GuzzleHttp\ClientInterface;
  11. use GuzzleHttp\Exception\GuzzleException;
  12. use GuzzleHttp\RequestOptions;
  13. use Paynetics\Cache\RedisCacheRequest;
  14. use Paynetics\HttpRequest\Headers\HeadersBag;
  15. use Psr\Http\Message\ResponseInterface;
  16. use Symfony\Component\HttpKernel\Exception\HttpException;
  17. /**
  18.  * Class AbstractHttpRequest
  19.  * @package Paynetics\HttpRequest
  20.  *
  21.  * @property HeadersBag headers
  22.  */
  23. abstract class AbstractHttpRequest implements HttpRequestInterface
  24. {
  25.     use SerializationTrait;
  26.     /**
  27.      *  .env file variable with url to destination
  28.      */
  29.     protected const SERVICE '';
  30.     /**
  31.      * @var Client
  32.      */
  33.     protected $client;
  34.     /**
  35.      * @var HeadersBag
  36.      */
  37.     protected $_headers;
  38.     /**
  39.      * @var RedisCacheRequest
  40.      */
  41.     protected $cache;
  42.     /**
  43.      * RequestService constructor.
  44.      * @param ClientInterface $client
  45.      * @param RedisCacheRequest $cache
  46.      */
  47.     public function __construct(ClientInterface $clientRedisCacheRequest $cache)
  48.     {
  49.         $this->client $client;
  50.         $this->_headers = new HeadersBag();
  51.         $this->cache $cache;
  52.     }
  53.     /**
  54.      * @param string $uri
  55.      * @param array $params
  56.      * @param array $query
  57.      * @param array $options
  58.      *
  59.      * @param bool $async
  60.      * @return mixed|ResponseInterface
  61.      *
  62.      * @throws ApiException
  63.      * @throws GuzzleException
  64.      * @throws JsonException
  65.      */
  66.     public function get(string $uri, array $params = [], array $query = [], array $options = [], bool $async false)
  67.     {
  68.         $options[RequestOptions::QUERY] = $query;
  69.         return $this->request(RequestMethods::GET$uri$paramsnulltrue$options$async);
  70.     }
  71.     /**
  72.      * @param string $uri
  73.      * @param null $data
  74.      * @param array $params
  75.      * @param bool $json
  76.      * @param array $options
  77.      *
  78.      * @param bool $async
  79.      * @return mixed|ResponseInterface
  80.      *
  81.      * @throws ApiException
  82.      * @throws GuzzleException
  83.      */
  84.     public function post(string $uri$data null, array $params = [], bool $json true, array $options = [], bool $async false)
  85.     {
  86.         return $this->request(RequestMethods::POST$uri$params$data$json$options$async);
  87.     }
  88.     /**
  89.      * @param string $uri
  90.      * @param null $data
  91.      * @param array $params
  92.      * @param bool $json
  93.      * @param array $options
  94.      *
  95.      * @param bool $async
  96.      * @return mixed|ResponseInterface
  97.      *
  98.      * @throws ApiException
  99.      * @throws GuzzleException
  100.      */
  101.     public function put(string $uri$data null, array $params = [], bool $json true, array $options = [], bool $async false)
  102.     {
  103.         return $this->request(RequestMethods::PUT$uri$params$data$json$options$async);
  104.     }
  105.     /**
  106.      * @param string $uri
  107.      * @param null $data
  108.      * @param array $params
  109.      * @param bool $json
  110.      * @param array $options
  111.      *
  112.      * @param bool $async
  113.      * @return mixed|ResponseInterface
  114.      *
  115.      * @throws ApiException
  116.      * @throws GuzzleException
  117.      */
  118.     public function patch(string $uri$data null, array $params = [], bool $json true, array $options = [], bool $async false)
  119.     {
  120.         return $this->request(RequestMethods::PATCH$uri$params$data$json$options$async);
  121.     }
  122.     /**
  123.      * @param string $uri
  124.      * @param null $data
  125.      * @param array $params
  126.      * @param array $options
  127.      *
  128.      * @param bool $async
  129.      * @return mixed|ResponseInterface
  130.      *
  131.      * @throws ApiException
  132.      * @throws GuzzleException
  133.      */
  134.     public function delete(string $uri$data null, array $params = [], array $options = [], bool $async false)
  135.     {
  136.         return $this->request(RequestMethods::DELETE$uri$params$datatrue$options$async);
  137.     }
  138.     /**
  139.      * @param string $method
  140.      * @param string $uri
  141.      * @param array $params
  142.      * @param $data
  143.      * @param bool $json
  144.      * @param array $options
  145.      *
  146.      * @param bool $async
  147.      * @return mixed|ResponseInterface
  148.      *
  149.      * @throws ApiException
  150.      * @throws GuzzleException
  151.      * @throws JsonException
  152.      */
  153.     public function request(string $methodstring $uri, array $params$databool $json true, array $options = [], bool $async false)
  154.     {
  155.         if ($json) {
  156.             if ($data && !is_string($data)) {
  157.                 $data $this->serialize($data);
  158.             }
  159.             if (is_array($data)) {
  160.                 $data json_encode($dataJSON_THROW_ON_ERROR512);
  161.             }
  162.             $data = [RequestOptions::BODY => $data];
  163.         } else {
  164.             $data = [RequestOptions::FORM_PARAMS => $data];
  165.         }
  166.         $this->preRequest($options);
  167.         $options array_merge($data, [RequestOptions::HEADERS => $this->_headers->all()], $options);
  168.         if (!$async) {
  169.             $response $this->client->request($methodURLParser::parse($uri, static::SERVICE$params), $options);
  170.             $this->_headers->flush();
  171.             return $this->handle($response);
  172.         }
  173.         $this->_headers->flush();
  174.         return $this->client->requestAsync($methodURLParser::parse($uri, static::SERVICE$params), $options)->then();
  175.     }
  176.     /**
  177.      * Pre Request Hook
  178.      * @param $options
  179.      */
  180.     public function preRequest(array &$options): void
  181.     {
  182.     }
  183.     /**
  184.      * @param ResponseInterface $response
  185.      *
  186.      * @return HttpException|void
  187.      *
  188.      * @throws ApiException
  189.      */
  190.     abstract protected function handle(ResponseInterface $response);
  191. }