-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheResponseTrait.php
118 lines (99 loc) · 3.38 KB
/
CacheResponseTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Exbil\LexOffice\Traits;
use Exbil\LexOffice\Exceptions\CacheException;
use Exbil\LexOffice\Utils;
use GuzzleHttp\Psr7\Response;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use stdClass;
trait CacheResponseTrait
{
/**
* @var CacheItemPoolInterface|null
*/
protected ?CacheItemPoolInterface $cacheInterface = null;
/**
* @param CacheItemPoolInterface $cache
* @return $this
*/
public function setCacheInterface(CacheItemPoolInterface $cache): self
{
$this->cacheInterface = $cache;
return $this;
}
/**
* @param RequestInterface $request
* @return string
*/
protected function getCacheName(RequestInterface $request): string
{
return 'lex-office-' . str_replace('/', '-', $request->getUri()->getPath()) . $request->getUri()->getQuery();
}
/**
* @param RequestInterface $request
* @return Response|null
* @throws CacheException
*/
public function getCacheResponse(RequestInterface $request): ?Response
{
$cacheName = $this->getCacheName($request);
try {
if ($request->getMethod() == 'GET') {
$cache = $this->cacheInterface->getItem($cacheName);
if ($cache && $cache->isHit()) {
$cache = Utils::jsonDecode($cache->get());
return new Response(
$cache->status,
(array)$cache->headers,
Utils::streamFor($cache->body),
$cache->version,
$cache->reason
);
}
}
} catch (InvalidArgumentException $exception) {
throw new CacheException(
'could not load cache response from request' . $request->getUri()->getPath(),
$exception->getCode(),
$exception
);
}
return null;
}
/**
* @param RequestInterface $request
* @param ResponseInterface $response
* @return $this
* @throws CacheException
*/
public function setCacheResponse(RequestInterface $request, ResponseInterface $response): self
{
if ($request->getMethod() != 'GET') {
return $this;
}
if (!$this->cacheInterface) {
throw new CacheException('response could not be cached, cacheInterface is not defined');
}
try {
$cacheName = $this->getCacheName($request);
$cache = new stdClass();
$cache->status = $response->getStatusCode();
$cache->headers = $response->getHeaders();
$cache->body = $response->getBody()->__toString();
$cache->version = $response->getProtocolVersion();
$cache->reason = $response->getReasonPhrase();
$item = $this->cacheInterface->getItem($cacheName);
$item->set(Utils::jsonEncode($cache));
$this->cacheInterface->save($item);
} catch (InvalidArgumentException $exception) {
throw new CacheException(
'response could not set cache for request ' . $request->getUri()->getPath(),
$exception->getCode(),
$exception
);
}
return $this;
}
}