bearsunday /
BEAR.QueryRepository
| 1 | <?php |
||
| 2 | |||
| 3 | // phpcs:ignoreFile SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing -- for call_user_func_array |
||
| 4 | |||
| 5 | namespace BEAR\QueryRepository; |
||
| 6 | |||
| 7 | use BEAR\QueryRepository\Exception\UnmatchedQuery; |
||
| 8 | use BEAR\Resource\AbstractUri; |
||
| 9 | use BEAR\Resource\Code; |
||
| 10 | use BEAR\Resource\ResourceObject; |
||
| 11 | use Ray\Aop\MethodInterceptor; |
||
| 12 | use Ray\Aop\MethodInvocation; |
||
| 13 | use ReflectionMethod; |
||
| 14 | |||
| 15 | use function array_values; |
||
| 16 | use function assert; |
||
| 17 | use function call_user_func_array; |
||
| 18 | use function get_class; |
||
| 19 | use function is_callable; |
||
| 20 | use function sprintf; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Interceptor for donut cache invalidation on CQRS commands |
||
| 24 | * |
||
| 25 | * Bound to command methods (onPut/onPatch/onDelete) of classes marked with #[CacheableResponse]. |
||
| 26 | * Refreshes donut cache and resource state after successful write operations. |
||
| 27 | * |
||
| 28 | * @see \BEAR\RepositoryModule\Annotation\CacheableResponse |
||
| 29 | * @see \BEAR\RepositoryModule\Annotation\DonutCache |
||
| 30 | * @see https://bearsunday.github.io/manuals/1.0/en/cache.html#event-driven-content |
||
| 31 | */ |
||
| 32 | final readonly class DonutCommandInterceptor implements MethodInterceptor |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 33 | { |
||
| 34 | public function __construct( |
||
| 35 | private DonutRepositoryInterface $repository, |
||
| 36 | private MatchQueryInterface $matchQuery |
||
| 37 | ){ |
||
| 38 | } |
||
| 39 | |||
| 40 | #[\Override] |
||
| 41 | public function invoke(MethodInvocation $invocation): ResourceObject |
||
| 42 | { |
||
| 43 | $ro = $invocation->proceed(); |
||
| 44 | assert($ro instanceof ResourceObject); |
||
| 45 | if ($ro->code >= Code::BAD_REQUEST) { |
||
| 46 | return $ro; |
||
| 47 | } |
||
| 48 | |||
| 49 | $this->refreshDonutAndState($ro); |
||
| 50 | |||
| 51 | return $ro; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function refreshDonutAndState(ResourceObject $ro): void |
||
| 55 | { |
||
| 56 | $getQuery =($this->matchQuery)($ro); |
||
| 57 | $delUri = clone $ro->uri; |
||
| 58 | $delUri->query = $getQuery; |
||
| 59 | |||
| 60 | // purge donut, resource state cache and etag |
||
| 61 | $this->repository->purge($delUri); |
||
| 62 | // update donut and create resource state |
||
| 63 | $this->refresh($getQuery, $ro); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param array<string, mixed> $getQuery |
||
| 68 | */ |
||
| 69 | private function refresh(array $getQuery, ResourceObject $ro): void |
||
| 70 | { |
||
| 71 | $ro->uri->query = $getQuery; |
||
| 72 | $get = [$ro, 'onGet']; |
||
| 73 | if (is_callable($get)) { |
||
| 74 | call_user_func_array($get, array_values($getQuery)); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 |