Skip to content

Commit

Permalink
PSR-2 standards
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Sep 9, 2017
1 parent b305b66 commit 1298c99
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/HttpClientRequestHandlerAmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Amp\Artax\Request;
use Amp\Artax\Response;
use Amp\Promise;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use unreal4u\TelegramAPI\Exceptions\ClientException;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse;
Expand All @@ -16,8 +17,9 @@
* @param Promise $promise
* @return \React\Promise\PromiseInterface
*/
function reactAdapt(Promise $promise) {
$deferred = new \React\Promise\Deferred();
function reactAdapt(Promise $promise)
{
$deferred = new Deferred();

$promise->onResolve(function ($error = null, $result = null) use ($deferred) {
if ($error) {
Expand All @@ -30,42 +32,50 @@ function reactAdapt(Promise $promise) {
return $deferred->promise();
}

class HttpClientRequestHandlerAmp implements RequestHandlerInterface {
class HttpClientRequestHandlerAmp implements RequestHandlerInterface
{

/**
* @var DefaultClient
*/
private $client;

public function __construct() {
public function __construct()
{
$this->client = new DefaultClient;
}

/**
* @param string $uri
*
* @return PromiseInterface
* @throws \unreal4u\TelegramAPI\Exceptions\ClientException
*/
public function get(string $uri): PromiseInterface {
$request = (new Request($uri))->withMethod('GET');
public function get(string $uri): PromiseInterface
{
$request = new Request($uri);

return $this->processRequest($request);
return $this->processRequest($request);
}

/**
* @param string $uri
* @param array $formFields
*
* @return PromiseInterface
* @throws \unreal4u\TelegramAPI\Exceptions\ClientException
*/
public function post(string $uri, array $formFields): PromiseInterface {
public function post(string $uri, array $formFields): PromiseInterface
{
$request = (new Request($uri))->withMethod('POST');

if (!empty($formFields['headers']))
if (!empty($formFields['headers'])) {
$request = $request->withHeaders($formFields['headers']);
}

if (!empty($formFields['body']))
if (!empty($formFields['body'])) {
$request = $request->withBody($formFields['body']);
}

return $this->processRequest($request);
}
Expand All @@ -74,14 +84,17 @@ public function post(string $uri, array $formFields): PromiseInterface {
* @param Request $request
*
* @return PromiseInterface
* @throws \unreal4u\TelegramAPI\Exceptions\ClientException
*/
private function processRequest(Request $request) {
private function processRequest(Request $request)
{
return reactAdapt(\Amp\call(function () use ($request) {
/** @var Response $response */
$response = yield $this->client->request($request);

if ($response->getStatus() >= 400)
if ($response->getStatus() >= 400) {
throw new ClientException($response->getReason(), $response->getStatus());
}

return new TelegramResponse(yield $response->getBody(), $response->getHeaders());
}));
Expand Down

0 comments on commit 1298c99

Please sign in to comment.