diff --git a/src/Exceptions/MaestroException.php b/src/Exceptions/MaestroException.php new file mode 100644 index 0000000..b90ac42 --- /dev/null +++ b/src/Exceptions/MaestroException.php @@ -0,0 +1,13 @@ +client = $client; if (!$client) { - $this->setClient((new Client())); + $this->setClient(new Client()); } } @@ -117,6 +134,8 @@ public function setEndPoint(string $endPoint) } /** + * @param array $headers + * * @return $this */ public function headers(array $headers) @@ -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; } @@ -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; @@ -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() @@ -174,6 +200,9 @@ public function send() } /** + * @throws \Maestro\Exceptions\NoUrlException + * @throws \Maestro\Exceptions\NoMethodException + * * @return mixed */ private function fetchCachedIfExists() @@ -191,8 +220,6 @@ private function fetchCachedIfExists() return $this; } - - return $this->sendRequest(); } return $this->sendRequest(); @@ -201,7 +228,7 @@ private function fetchCachedIfExists() /** * @return int */ - private function makeCacheExpiryTime() + private function makeCacheExpiryTime() : int { return time() + $this->cacheTime; } @@ -209,34 +236,30 @@ private function makeCacheExpiryTime() /** * 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(); @@ -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, @@ -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, @@ -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(); @@ -297,7 +322,7 @@ public function getResponse() */ public function parse() { - if ($this->assoc == true) { + if ($this->assoc === true) { return json_decode($this->responseBody, true); } diff --git a/tests/RestTest.php b/tests/RestTest.php index 29836f5..6ff91dc 100644 --- a/tests/RestTest.php +++ b/tests/RestTest.php @@ -9,12 +9,15 @@ namespace Maestro\Test; use GuzzleHttp\Client; +use Maestro\Exceptions\NoMethodException; +use Maestro\Exceptions\NoUrlException; +use Maestro\Exceptions\PostCachingException; use Maestro\Rest; class RestTest extends TestCase { /** - * @var + * @var \Maestro\Rest */ protected $restClass; @@ -52,8 +55,8 @@ public function testGetSetUrl() public function testGetSetEndpoint() { $endpoint = 'endpoint'; - $this->assertInstanceOf(Rest::class, $this->restClass->setEndpoint($endpoint)); - $this->assertEquals($endpoint, $this->restClass->getEndpoint()); + $this->assertInstanceOf(Rest::class, $this->restClass->setEndPoint($endpoint)); + $this->assertEquals($endpoint, $this->restClass->getEndPoint()); } /** @@ -87,7 +90,6 @@ public function testBody() */ public function testSendGet() { - $url = 'https://www.google.com'; $mock = \Mockery::mock(new Client()); $mock->shouldReceive('send') ->times(1); @@ -95,7 +97,7 @@ public function testSendGet() $this->restClass = new Rest($mock); $url = 'https://www.google.com'; - $response = $this->restClass + $this->restClass ->get() ->setUrl($url) ->send() @@ -111,7 +113,7 @@ public function testSendGet() */ public function testSendNoMethod() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(NoMethodException::class); $this->restClass->send(); } @@ -122,7 +124,7 @@ public function testSendNoMethod() */ public function testSendNoUrl() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(NoUrlException::class); $this->restClass->get()->send(); } @@ -190,9 +192,9 @@ public function testAssoc() public function testNotCachableByDefault() { $result = $this->restClass->get() - ->setUrl('http://api.example.com') - ->setEndPoint('/horses') - ->getCachingEnabled(); + ->setUrl('http://api.example.com') + ->setEndPoint('/horses') + ->getCachingEnabled(); $this->assertFalse($result); } @@ -200,10 +202,10 @@ public function testNotCachableByDefault() public function testCachable() { $result = $this->restClass->get() - ->setUrl('http://api.example.com') - ->setEndPoint('/horses') - ->cachable(60) - ->getCachingEnabled(); + ->setUrl('http://api.example.com') + ->setEndPoint('/horses') + ->cachable(60) + ->getCachingEnabled(); $this->assertTrue($result); } @@ -211,15 +213,15 @@ public function testCachable() public function testSetCacheTime() { $result = $this->restClass->get() - ->cachable(360) - ->getCacheTime(); + ->cachable(360) + ->getCacheTime(); $this->assertEquals($result, 360); } public function testPostRequestsNotCachable() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(PostCachingException::class); $rest = new Rest(); $result = $rest->post()->cachable(60); }