1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kevinrob\GuzzleCache\Strategy\Delegate; |
4
|
|
|
|
5
|
|
|
use Kevinrob\GuzzleCache\Strategy\CacheStrategyInterface; |
6
|
|
|
use Kevinrob\GuzzleCache\Strategy\NullCacheStrategy; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
class DelegatingCacheStrategy implements CacheStrategyInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $requestMatchers = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var CacheStrategyInterface |
19
|
|
|
*/ |
20
|
|
|
private $defaultCacheStrategy; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* DelegatingCacheStrategy constructor. |
24
|
|
|
*/ |
25
|
1 |
|
public function __construct(CacheStrategyInterface $defaultCacheStrategy = null) |
26
|
|
|
{ |
27
|
1 |
|
$this->defaultCacheStrategy = $defaultCacheStrategy ?: new NullCacheStrategy(); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param CacheStrategyInterface $defaultCacheStrategy |
32
|
|
|
*/ |
33
|
|
|
public function setDefaultCacheStrategy(CacheStrategyInterface $defaultCacheStrategy) |
34
|
|
|
{ |
35
|
|
|
$this->defaultCacheStrategy = $defaultCacheStrategy; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param RequestMatcherInterface $requestMatcher |
40
|
|
|
* @param CacheStrategyInterface $cacheStrategy |
41
|
|
|
*/ |
42
|
1 |
|
final public function registerRequestMatcher(RequestMatcherInterface $requestMatcher, CacheStrategyInterface $cacheStrategy) |
43
|
|
|
{ |
44
|
1 |
|
$this->requestMatchers[] = [ |
45
|
1 |
|
$requestMatcher, |
46
|
1 |
|
$cacheStrategy, |
47
|
|
|
]; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param RequestInterface $request |
52
|
|
|
* @return CacheStrategyInterface |
53
|
|
|
*/ |
54
|
1 |
|
private function getStrategyFor(RequestInterface $request) |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @var RequestMatcherInterface $requestMatcher |
58
|
|
|
* @var CacheStrategyInterface $cacheStrategy |
59
|
|
|
*/ |
60
|
1 |
|
foreach ($this->requestMatchers as $requestMatcher) { |
61
|
1 |
|
list($requestMatcher, $cacheStrategy) = $requestMatcher; |
62
|
1 |
|
if ($requestMatcher->matches($request)) { |
63
|
1 |
|
return $cacheStrategy; |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
return $this->defaultCacheStrategy; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
1 |
|
public function fetch(RequestInterface $request) |
74
|
|
|
{ |
75
|
1 |
|
return $this->getStrategyFor($request)->fetch($request); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
1 |
|
public function cache(RequestInterface $request, ResponseInterface $response) |
82
|
|
|
{ |
83
|
1 |
|
return $this->getStrategyFor($request)->cache($request, $response); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function update(RequestInterface $request, ResponseInterface $response) |
90
|
|
|
{ |
91
|
|
|
return $this->getStrategyFor($request)->update($request, $response); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function delete(RequestInterface $request) |
98
|
|
|
{ |
99
|
|
|
return $this->getStrategyFor($request)->delete($request); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|