Created
August 17, 2018 11:15
-
-
Save ostrolucky/7e2a8916283bcf5dba9829b943c6d0f0 to your computer and use it in GitHub Desktop.
StripeHttplugClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Service\Http; | |
use GuzzleHttp\Psr7\Request; | |
use GuzzleHttp\Psr7\Uri; | |
use Http\Client\HttpClient; | |
use Stripe\HttpClient\ClientInterface; | |
class StripeHttplugClient implements ClientInterface | |
{ | |
/** | |
* @var HttpClient | |
*/ | |
private $client; | |
public function __construct(HttpClient $stripeClient) | |
{ | |
$this->client = $stripeClient; | |
} | |
/** | |
* {@inheritdoc} | |
* | |
* @param string $method The HTTP method being used | |
* @param string $absUrl The URL being requested, including domain and protocol | |
* @param string[] $rawHeaders | |
* @param string[] $params KV pairs for parameters. Can be nested for arrays and hashes | |
* @param bool $hasFile Whether or not $params references a file (via an @ prefix or CurlFile) | |
* | |
* @return array [$rawBody, $httpStatusCode, $httpHeader] | |
*/ | |
public function request($method, $absUrl, $rawHeaders, $params, $hasFile): array | |
{ | |
$uri = (new Uri($absUrl))->withQuery(http_build_query($params, '', '&', PHP_QUERY_RFC3986)); | |
$response = $this->client->sendRequest(new Request($method, $uri, $this->processHeaders($rawHeaders))); | |
return [$response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders()]; | |
} | |
/** | |
* @param string[] $rawHeaders | |
* | |
* @return string[] | |
*/ | |
private function processHeaders(array $rawHeaders): array | |
{ | |
$keyValueHeaders = []; | |
foreach ($rawHeaders as $rawHeader) { | |
$key = strstr($rawHeader, ':', true); | |
$keyValueHeaders[$key] = substr($rawHeader, strlen($key) + 2); | |
} | |
return $keyValueHeaders; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment