bearsunday /
BEAR.QueryRepository
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BEAR\QueryRepository; |
||
| 6 | |||
| 7 | use BEAR\QueryRepository\Exception\ReturnValueIsNotResourceObjectException; |
||
| 8 | use BEAR\RepositoryModule\Annotation\Commands; |
||
| 9 | use BEAR\Resource\Code; |
||
| 10 | use BEAR\Resource\ResourceObject; |
||
| 11 | use Override; |
||
| 12 | use Ray\Aop\MethodInterceptor; |
||
| 13 | use Ray\Aop\MethodInvocation; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Interceptor for cache invalidation on CQRS commands with #[Purge] or #[Refresh] |
||
| 17 | * |
||
| 18 | * Automatically bound to all command methods (onPut/onPatch/onDelete) of #[Cacheable] classes. |
||
| 19 | * Processes #[Purge] and #[Refresh] annotations on these methods and executes cache |
||
| 20 | * invalidation after successful write operations. |
||
| 21 | * |
||
| 22 | * For non-Cacheable classes, use RefreshInterceptor instead by explicitly marking methods |
||
| 23 | * with #[Purge] or #[Refresh]. |
||
| 24 | * |
||
| 25 | * @see \BEAR\RepositoryModule\Annotation\Purge |
||
| 26 | 13 | * @see \BEAR\RepositoryModule\Annotation\Refresh |
|
| 27 | * @see https://bearsunday.github.io/manuals/1.0/en/cache.html#tag-based-cache-invalidation |
||
| 28 | 13 | */ |
|
| 29 | 13 | final readonly class CommandInterceptor implements MethodInterceptor |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | { |
||
| 31 | /** @param CommandInterface[] $commands */ |
||
| 32 | public function __construct( |
||
| 33 | #[Commands] |
||
| 34 | private array $commands, |
||
| 35 | ) { |
||
| 36 | 10 | } |
|
| 37 | |||
| 38 | 10 | /** |
|
| 39 | 10 | * {@inheritDoc} |
|
| 40 | 1 | * |
|
| 41 | * @throws ReturnValueIsNotResourceObjectException |
||
| 42 | */ |
||
| 43 | 9 | #[Override] |
|
| 44 | 2 | public function invoke(MethodInvocation $invocation) |
|
| 45 | { |
||
| 46 | /** @psalm-suppress MixedAssignment */ |
||
| 47 | 8 | $ro = $invocation->proceed(); |
|
| 48 | 8 | if (! $ro instanceof ResourceObject) { |
|
| 49 | throw new ReturnValueIsNotResourceObjectException($invocation->getThis()::class); |
||
| 50 | } |
||
| 51 | 7 | ||
| 52 | if ($ro->code >= Code::BAD_REQUEST) { |
||
| 53 | return $ro; |
||
| 54 | } |
||
| 55 | |||
| 56 | foreach ($this->commands as $command) { |
||
| 57 | $command->command($invocation, $ro); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $ro; |
||
| 61 | } |
||
| 62 | } |
||
| 63 |