Issues (61)

src/RefreshInterceptor.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\QueryRepository\Exception\ReturnValueIsNotResourceObjectException;
8
use BEAR\Resource\Code;
9
use BEAR\Resource\ResourceObject;
10
use Override;
11
use Ray\Aop\MethodInterceptor;
12
use Ray\Aop\MethodInvocation;
13
14
/**
15
 * Interceptor for cache refresh commands with #[Purge] or #[Refresh]
16
 *
17
 * Bound only to methods explicitly marked with #[Purge] or #[Refresh] on non-Cacheable classes.
18
 * Unlike CommandInterceptor (which automatically binds to all command methods of #[Cacheable]
19
 * classes), this interceptor requires explicit attribute annotation on each method.
20
 *
21
 * Executes cache invalidation commands after successful method execution.
22
 *
23
 * @see \BEAR\RepositoryModule\Annotation\Purge
24
 * @see \BEAR\RepositoryModule\Annotation\Refresh
25
 * @see https://bearsunday.github.io/manuals/1.0/en/cache.html#event-driven-content
26
 */
27
final readonly class RefreshInterceptor implements MethodInterceptor
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 27 at column 6
Loading history...
28
{
29
    public function __construct(
30
        private RefreshAnnotatedCommand $command,
31
    ) {
32
    }
33
34
    #[Override]
35
    public function invoke(MethodInvocation $invocation): ResourceObject
36
    {
37
        /** @psalm-suppress MixedAssignment */
38
        $ro = $invocation->proceed();
39
        if (! $ro instanceof ResourceObject) {
40
            throw new ReturnValueIsNotResourceObjectException($invocation->getThis()::class); // @codeCoverageIgnore
41
        }
42
43
        if ($ro->code < Code::BAD_REQUEST) {
44
            $this->command->command($invocation, $ro);
45
        }
46
47
        return $ro;
48
    }
49
}
50