Passed
Pull Request — 1.x (#166)
by Akihito
03:05 queued 01:47
created

RefreshInterceptor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 21
ccs 8
cts 9
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 4
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 to methods marked with #[Purge] or #[Refresh] on non-Cacheable classes.
18
 * Executes cache invalidation commands after successful method execution.
19
 *
20 2
 * @see \BEAR\RepositoryModule\Annotation\Purge
21
 * @see \BEAR\RepositoryModule\Annotation\Refresh
22 2
 * @see https://bearsunday.github.io/manuals/1.0/en/cache.html#event-driven-content
23 2
 */
24
final readonly class RefreshInterceptor implements MethodInterceptor
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 24 at column 6
Loading history...
25 2
{
26
    public function __construct(
27 2
        private RefreshAnnotatedCommand $command,
28 2
    ) {
29
    }
30
31
    #[Override]
32 2
    public function invoke(MethodInvocation $invocation): ResourceObject
33 2
    {
34
        /** @psalm-suppress MixedAssignment */
35
        $ro = $invocation->proceed();
36 2
        if (! $ro instanceof ResourceObject) {
37
            throw new ReturnValueIsNotResourceObjectException($invocation->getThis()::class); // @codeCoverageIgnore
38
        }
39
40
        if ($ro->code < Code::BAD_REQUEST) {
41
            $this->command->command($invocation, $ro);
42
        }
43
44
        return $ro;
45
    }
46
}
47