Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Merge pull request #42 from iwex/master
Browse files Browse the repository at this point in the history
Package-related exceptions
  • Loading branch information
marcus-campos authored Oct 21, 2017
2 parents 2d2f34e + ffe97bd commit 987b66b
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 55 deletions.
13 changes: 13 additions & 0 deletions src/Exceptions/MaestroException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Created by PhpStorm.
* User: iwex
* Date: 10/21/17
* Time: 5:39 PM.
*/

namespace Maestro\Exceptions;

abstract class MaestroException extends \Exception
{
}
17 changes: 17 additions & 0 deletions src/Exceptions/NoMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: iwex
* Date: 10/21/17
* Time: 5:39 PM.
*/

namespace Maestro\Exceptions;

class NoMethodException extends MaestroException
{
public function __construct()
{
parent::__construct('No method defined');
}
}
17 changes: 17 additions & 0 deletions src/Exceptions/NoUrlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: iwex
* Date: 10/21/17
* Time: 5:39 PM.
*/

namespace Maestro\Exceptions;

class NoUrlException extends MaestroException
{
public function __construct()
{
parent::__construct('No URL defined');
}
}
17 changes: 17 additions & 0 deletions src/Exceptions/PostCachingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: iwex
* Date: 10/21/17
* Time: 5:39 PM.
*/

namespace Maestro\Exceptions;

class PostCachingException extends MaestroException
{
public function __construct()
{
parent::__construct('Enabling caching is disabled for POST requests');
}
}
101 changes: 63 additions & 38 deletions src/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use Maestro\Exceptions\NoMethodException;
use Maestro\Exceptions\NoUrlException;
use Maestro\Exceptions\PostCachingException;
use Maestro\Http\Methods;

class Rest
Expand All @@ -27,28 +30,42 @@ class Rest

private $endPoint;

/**
* @var \Psr\Http\Message\ResponseInterface
*/
private $response;

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

/** {boolean} Indicates if caching is turned on */
/**
* {boolean} Indicates if caching is turned on.
*/
protected $cachingEnabled = false;

/** {string} The response body as a string to make it cachable */
/**
* {string} The response body as a string to make it cachable.
*/
protected $responseBody;

/** {number} Time responses will be cached for (if caching is enabled) */
/**
* {number} Time responses will be cached for (if caching is enabled).
*/
protected $cacheTime = 60;

/** {string} Used by APCu */
protected $cacheKey = '';
/**
* {string} Used by APCu.
*/
private $cacheKey = '';

public function __construct($client = null)
{
$this->client = $client;

if (!$client) {
$this->setClient((new Client()));
$this->setClient(new Client());
}
}

Expand Down Expand Up @@ -117,6 +134,8 @@ public function setEndPoint(string $endPoint)
}

/**
* @param array $headers
*
* @return $this
*/
public function headers(array $headers)
Expand All @@ -127,13 +146,15 @@ public function headers(array $headers)
}

/**
* @param array $body
*
* @return $this
*/
public function body(array $body)
{
try {
$this->body = json_encode($body);
} catch (Exception $e) {
$this->body = \GuzzleHttp\json_encode($body);
} catch (\InvalidArgumentException $e) {
$this->body = $body;
}

Expand All @@ -143,14 +164,16 @@ public function body(array $body)
/**
* Turns on caching of response body for given time.
*
* @param {number} $time - Shelf-life of cached response in seconds
* @param int $time - Shelf-life of cached response in seconds
*
* @throws \Maestro\Exceptions\PostCachingException
*
* @return $this
*/
public function cachable(int $time = 60)
{
if ($this->method == 'POST') {
throw new \InvalidArgumentException('Enabling caching is disabled for POST requests');
if ($this->method === 'POST') {
throw new PostCachingException();
}
$this->cachingEnabled = true;
$this->cacheTime = $time;
Expand All @@ -161,6 +184,9 @@ public function cachable(int $time = 60)
/**
* Either sends the request or fetches a cached response body dependent on if caching is enabled.
*
* @throws \Maestro\Exceptions\NoUrlException
* @throws \Maestro\Exceptions\NoMethodException
*
* @return $this
*/
public function send()
Expand All @@ -174,6 +200,9 @@ public function send()
}

/**
* @throws \Maestro\Exceptions\NoUrlException
* @throws \Maestro\Exceptions\NoMethodException
*
* @return mixed
*/
private function fetchCachedIfExists()
Expand All @@ -191,8 +220,6 @@ private function fetchCachedIfExists()

return $this;
}

return $this->sendRequest();
}

return $this->sendRequest();
Expand All @@ -201,42 +228,38 @@ private function fetchCachedIfExists()
/**
* @return int
*/
private function makeCacheExpiryTime()
private function makeCacheExpiryTime() : int
{
return time() + $this->cacheTime;
}

/**
* Sends the request and caches the response is caching is enabled.
*
* @throws \Maestro\Exceptions\NoUrlException
* @throws \Maestro\Exceptions\NoMethodException
*
* @return $this
*/
private function sendRequest()
{
if (!$this->method) {
throw new \InvalidArgumentException('No method defined');
} elseif (!$this->url) {
throw new \InvalidArgumentException('No url defined');
throw new NoMethodException();
}

if (!$this->url) {
throw new NoUrlException();
}

// GET method doesn't send a BODY
switch ($this->method) {
case 'GET':
$request = new Request(
$this->method,
$this->url.$this->endPoint,
$this->headers
);
break;
default:
$request = new Request(
$this->method,
$this->url.$this->endPoint,
$this->headers,
$this->body
);
$paramsToSend = [$this->method, $this->url.$this->endPoint];

if ($this->method !== 'GET') {
$paramsToSend[] = $this->body;
}

$request = new Request(...$paramsToSend);

$this->response = $this->client->send($request);

$this->cacheResponseBody();
Expand All @@ -250,7 +273,7 @@ private function cacheResponseBody()
$this->responseBody = (string) $this->response->getBody();
}

if ($this->cachingEnabled && $this->response->getReasonPhrase() == 'OK') {
if ($this->cachingEnabled && $this->response->getReasonPhrase() === 'OK') {
$batch = [
'expires' => $this->makeCacheExpiryTime(),
'responseBody' => $this->responseBody,
Expand All @@ -266,7 +289,7 @@ public function sendAsync()
{
$curl = new CurlMultiHandler();
$handler = HandlerStack::create($curl);
$this->setClient((new Client(['handler' => $handler])));
$this->setClient(new Client(['handler' => $handler]));

$request = new Request(
$this->method,
Expand All @@ -275,9 +298,11 @@ public function sendAsync()
$this->body
);

$this->response = $this->client->sendAsync($request)->then(function ($response) {
return $response;
});
$this->response = $this->client->sendAsync($request)->then(
function ($response) {
return $response;
}
);

$curl->tick();

Expand All @@ -297,7 +322,7 @@ public function getResponse()
*/
public function parse()
{
if ($this->assoc == true) {
if ($this->assoc === true) {
return json_decode($this->responseBody, true);
}

Expand Down
Loading

0 comments on commit 987b66b

Please sign in to comment.