-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAbstractDonutCacheInterceptor.php
More file actions
68 lines (56 loc) · 1.91 KB
/
Copy pathAbstractDonutCacheInterceptor.php
File metadata and controls
68 lines (56 loc) · 1.91 KB
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
<?php
declare(strict_types=1);
namespace BEAR\QueryRepository;
use BEAR\Resource\Code;
use BEAR\Resource\ResourceObject;
use Override;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use Throwable;
use function assert;
use function sprintf;
use function trigger_error;
use const E_USER_WARNING;
abstract class AbstractDonutCacheInterceptor implements MethodInterceptor
{
// @phpcs:ignore SlevomatCodingStandard.TypeHints.UselessConstantTypeHint.UselessDocComment
/** @var bool */
protected const IS_ENTIRE_CONTENT_CACHEABLE = false;
public function __construct(
private readonly DonutRepositoryInterface $donutRepository,
) {
}
/**
* {@inheritDoc}
*/
#[Override]
final public function invoke(MethodInvocation $invocation)
{
$ro = $invocation->getThis();
assert($ro instanceof ResourceObject);
try {
$maybeRo = $this->donutRepository->get($ro);
if ($maybeRo instanceof ResourceObject) {
return $maybeRo;
}
} catch (Throwable $e) { // @codeCoverageIgnoreStart
// when cache server is down
$this->triggerWarning($e);
return $invocation->proceed(); // @codeCoverageIgnoreEnd
}
/** @var ResourceObject $ro */
$ro = $invocation->proceed();
// donut created in ResourceObject
if (isset($ro->headers[Header::ETAG]) || $ro->code >= Code::BAD_REQUEST) {
return $ro;
}
return static::IS_ENTIRE_CONTENT_CACHEABLE ? // phpcs:ignore - not "self"
$this->donutRepository->putStatic($ro, null, null) :
$this->donutRepository->putDonut($ro, null);
}
/** @codeCoverageIgnore */
private function triggerWarning(Throwable $e): void
{
trigger_error(sprintf('%s: %s in %s:%s', $e::class, $e->getMessage(), $e->getFile(), $e->getLine()), E_USER_WARNING);
}
}