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

Commit 5c74426

Browse files
Merge pull request #49 from marcus-campos/fix/refactoring
Fix/refactoring
2 parents a95cc3c + 0948244 commit 5c74426

File tree

6 files changed

+124
-105
lines changed

6 files changed

+124
-105
lines changed

src/Cache/Cache.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* User: marcus-campos
4+
* Date: 24/10/17
5+
* Time: 12:33
6+
*/
7+
8+
namespace Maestro\Cache;
9+
10+
use Maestro\Exceptions\PostCachingException;
11+
12+
trait Cache
13+
{
14+
use CachingGetters;
15+
16+
/**
17+
* {number} Time responses will be cached for (if caching is enabled).
18+
*/
19+
protected $cacheTime = 60;
20+
21+
/**
22+
* {string} Used by APCu.
23+
*/
24+
private $cacheKey = '';
25+
26+
/**
27+
* {boolean} Indicates if caching is turned on.
28+
*/
29+
protected $cachingEnabled = false;
30+
31+
/**
32+
* Turns on caching of response body for given time.
33+
*
34+
* @param int $time - Shelf-life of cached response in seconds
35+
*
36+
* @throws \Maestro\Exceptions\PostCachingException
37+
*
38+
* @return $this
39+
*/
40+
public function cachable(int $time = 60)
41+
{
42+
if ($this->method === 'POST') {
43+
throw new PostCachingException();
44+
}
45+
$this->cachingEnabled = true;
46+
$this->cacheTime = $time;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* @throws \Maestro\Exceptions\NoUrlException
53+
* @throws \Maestro\Exceptions\NoMethodException
54+
*
55+
* @return mixed
56+
*/
57+
private function fetchCachedIfExists()
58+
{
59+
// Generate a key to use for caching
60+
$this->cacheKey = md5($this->url . $this->endPoint);
61+
62+
// Set the response from APCu cache
63+
if (apcu_exists($this->cacheKey)) {
64+
$batch = apcu_fetch($this->cacheKey);
65+
// Check that expiry date is after now but also check that it is before our current cache time
66+
// just incase a cache has been created by a previous request with a longer cache time.
67+
if ($batch['expires'] > time() && $batch['expires'] < $this->makeCacheExpiryTime()) {
68+
$this->responseBody = $batch['responseBody'];
69+
70+
return $this;
71+
}
72+
}
73+
74+
return $this->sendRequest();
75+
}
76+
77+
/**
78+
* @return int
79+
*/
80+
private function makeCacheExpiryTime(): int
81+
{
82+
return time() + $this->cacheTime;
83+
}
84+
85+
private function cacheResponseBody()
86+
{
87+
if (method_exists($this->response, 'getBody')) {
88+
$this->responseBody = (string)$this->response->getBody();
89+
}
90+
91+
if ($this->cachingEnabled && $this->response->getReasonPhrase() === 'OK') {
92+
$batch = [
93+
'expires' => $this->makeCacheExpiryTime(),
94+
'responseBody' => $this->responseBody,
95+
];
96+
apcu_store($this->cacheKey, $batch);
97+
}
98+
}
99+
}

src/CachingGetters.php renamed to src/Cache/CachingGetters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Maestro;
3+
namespace Maestro\Cache;
44

55
trait CachingGetters
66
{

src/Rest.php

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
use GuzzleHttp\Handler\CurlMultiHandler;
1212
use GuzzleHttp\HandlerStack;
1313
use GuzzleHttp\Psr7\Request;
14+
use Maestro\Cache\Cache;
1415
use Maestro\Exceptions\NoMethodException;
1516
use Maestro\Exceptions\NoUrlException;
16-
use Maestro\Exceptions\PostCachingException;
1717
use Maestro\Http\Methods;
1818

1919
class Rest
2020
{
21-
use Methods, CachingGetters;
21+
use Methods, Cache;
2222

2323
protected $url;
2424

@@ -40,26 +40,11 @@ class Rest
4040
*/
4141
private $client;
4242

43-
/**
44-
* {boolean} Indicates if caching is turned on.
45-
*/
46-
protected $cachingEnabled = false;
47-
4843
/**
4944
* {string} The response body as a string to make it cachable.
5045
*/
5146
protected $responseBody;
5247

53-
/**
54-
* {number} Time responses will be cached for (if caching is enabled).
55-
*/
56-
protected $cacheTime = 60;
57-
58-
/**
59-
* {string} Used by APCu.
60-
*/
61-
private $cacheKey = '';
62-
6348
public function __construct($client = null)
6449
{
6550
$this->client = $client;
@@ -161,25 +146,6 @@ public function body(array $body)
161146
return $this;
162147
}
163148

164-
/**
165-
* Turns on caching of response body for given time.
166-
*
167-
* @param int $time - Shelf-life of cached response in seconds
168-
*
169-
* @throws \Maestro\Exceptions\PostCachingException
170-
*
171-
* @return $this
172-
*/
173-
public function cachable(int $time = 60)
174-
{
175-
if ($this->method === 'POST') {
176-
throw new PostCachingException();
177-
}
178-
$this->cachingEnabled = true;
179-
$this->cacheTime = $time;
180-
181-
return $this;
182-
}
183149

184150
/**
185151
* Either sends the request or fetches a cached response body dependent on if caching is enabled.
@@ -199,40 +165,6 @@ public function send()
199165
return $this->sendRequest();
200166
}
201167

202-
/**
203-
* @throws \Maestro\Exceptions\NoUrlException
204-
* @throws \Maestro\Exceptions\NoMethodException
205-
*
206-
* @return mixed
207-
*/
208-
private function fetchCachedIfExists()
209-
{
210-
// Generate a key to use for caching
211-
$this->cacheKey = md5($this->url.$this->endPoint);
212-
213-
// Set the response from APCu cache
214-
if (apcu_exists($this->cacheKey)) {
215-
$batch = apcu_fetch($this->cacheKey);
216-
// Check that expiry date is after now but also check that it is before our current cache time
217-
// just incase a cache has been created by a previous request with a longer cache time.
218-
if ($batch['expires'] > time() && $batch['expires'] < $this->makeCacheExpiryTime()) {
219-
$this->responseBody = $batch['responseBody'];
220-
221-
return $this;
222-
}
223-
}
224-
225-
return $this->sendRequest();
226-
}
227-
228-
/**
229-
* @return int
230-
*/
231-
private function makeCacheExpiryTime() : int
232-
{
233-
return time() + $this->cacheTime;
234-
}
235-
236168
/**
237169
* Sends the request and caches the response is caching is enabled.
238170
*
@@ -251,14 +183,7 @@ private function sendRequest()
251183
throw new NoUrlException();
252184
}
253185

254-
// GET method doesn't send a BODY
255-
$paramsToSend = [$this->method, $this->url.$this->endPoint, $this->headers];
256-
257-
if ($this->method !== 'GET') {
258-
$paramsToSend[] = $this->body;
259-
}
260-
261-
$request = new Request(...$paramsToSend);
186+
$request = $this->newRequest();
262187

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

@@ -267,21 +192,6 @@ private function sendRequest()
267192
return $this;
268193
}
269194

270-
private function cacheResponseBody()
271-
{
272-
if (method_exists($this->response, 'getBody')) {
273-
$this->responseBody = (string) $this->response->getBody();
274-
}
275-
276-
if ($this->cachingEnabled && $this->response->getReasonPhrase() === 'OK') {
277-
$batch = [
278-
'expires' => $this->makeCacheExpiryTime(),
279-
'responseBody' => $this->responseBody,
280-
];
281-
apcu_store($this->cacheKey, $batch);
282-
}
283-
}
284-
285195
/**
286196
* @return $this
287197
*/
@@ -291,12 +201,7 @@ public function sendAsync()
291201
$handler = HandlerStack::create($curl);
292202
$this->setClient(new Client(['handler' => $handler]));
293203

294-
$request = new Request(
295-
$this->method,
296-
$this->url.$this->endPoint,
297-
$this->headers,
298-
$this->body
299-
);
204+
$request = $this->newRequest();
300205

301206
$this->response = $this->client->sendAsync($request)->then(
302207
function ($response) {
@@ -309,6 +214,21 @@ function ($response) {
309214
return $this;
310215
}
311216

217+
/**
218+
* @return Request
219+
*/
220+
private function newRequest()
221+
{
222+
// GET method doesn't send a BODY
223+
$paramsToSend = [$this->method, $this->url . $this->endPoint, $this->headers];
224+
225+
if ($this->method !== 'GET') {
226+
$paramsToSend[] = $this->body;
227+
}
228+
229+
return new Request(...$paramsToSend);
230+
}
231+
312232
/**
313233
* @return mixed
314234
*/
@@ -320,7 +240,7 @@ public function getResponse()
320240
/**
321241
* @return int
322242
*/
323-
public function status() : int
243+
public function status(): int
324244
{
325245
return $this->response->getStatusCode();
326246
}

tests/FakeResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function getBody()
1616
{
1717
return $this->body;
1818
}
19-
19+
2020
public function getStatusCode()
2121
{
2222
return 200;

tests/RestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ public function testStatus()
188188
{
189189
$mock = \Mockery::mock(new Client());
190190
$mock->shouldReceive('send')
191-
->times(1)
192-
->andReturn(new FakeResponse());
191+
->times(1)
192+
->andReturn(new FakeResponse());
193193

194194
$this->restClass = new Rest($mock);
195195

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
* Add additional configuration/setup your application needs when running
66
* unit tests in this file.
77
*/
8-
require_once dirname(__DIR__).DIRECTORY_SEPARATOR.'vendor/autoload.php';
8+
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
99

1010
$_SERVER['PHP_SELF'] = '/';

0 commit comments

Comments
 (0)