-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseStrategy.php
132 lines (116 loc) · 3.13 KB
/
BaseStrategy.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* This file is part of response-cache, a Matchory library.
*
* @author Moritz Friedrich <moritz@matchory.com>
*/
declare(strict_types=1);
namespace Matchory\ResponseCache\Support;
use Illuminate\Auth\AuthManager;
use Illuminate\Http\Request;
use JetBrains\PhpStorm\Pure;
use Matchory\ResponseCache\Contracts\CacheStrategy;
use Symfony\Component\HttpFoundation\Response;
use function md5;
/**
* Base Strategy
*
* @bundle Matchory\ResponseCache
*/
class BaseStrategy implements CacheStrategy
{
public function __construct(protected readonly AuthManager $auth)
{
}
/**
* @inheritDoc
*/
public function key(Request $request): string
{
$identifier = $this->extractRequestIdentifier($request);
$suffix = $this->buildSuffix($request);
return $this->hash($identifier . $suffix);
}
/**
* @inheritDoc
*/
public function shouldCache(Request $request, Response $response): bool
{
if ( ! $this->isMethodCachable($request)) {
return false;
}
return $this->isSuccessful($response);
}
/**
* @inheritDoc
*/
#[Pure]
public function tags(Request $request, Response|null $response = null): array
{
return [];
}
/**
* Retrieves a suffix to append to the key before it is hashed. This allows
* to constrain the cache key to the authenticated user, for example.
*
* @param Request $request
*
* @return string
* @noinspection PhpUnusedParameterInspection
*/
protected function buildSuffix(Request $request): string
{
return $this->auth->check()
? (string)$this->auth->id()
: '';
}
/**
* Extracts a unique identifier from a request. The default implementation
* will use the full URL as provided by Laravel.
*
* @param Request $request Current request instance.
*
* @return string Unique request identifier.
*/
protected function extractRequestIdentifier(Request $request): string
{
return $request->method() . $request->fullUrl();
}
/**
* Hashes the cache key. The default implementation will return an MD5 hash
* of the given key.
*
* @param string $key
*
* @return string
*/
#[Pure]
protected function hash(string $key): string
{
return md5($key);
}
/**
* Checks whether the request method if a request is safe to cache.
*
* @param Request $request Request instance.
*
* @return bool Whether the method is safe to cache.
*/
protected function isMethodCachable(Request $request): bool
{
return $request->isMethodCacheable();
}
/**
* Checks whether a response is successful. The default implementation will
* succeed if the status code is in the 2xx-3xx range.
*
* @param Response $response Response instance.
*
* @return bool Whether the response is successful
*/
#[Pure]
protected function isSuccessful(Response $response): bool
{
return $response->isSuccessful() || $response->isRedirection();
}
}