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 | final readonly class DonutCommandInterceptor implements MethodInterceptor |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 23 | { |
||
| 24 | public function __construct( |
||
| 25 | private DonutRepositoryInterface $repository, |
||
| 26 | private MatchQueryInterface $matchQuery |
||
| 27 | ){ |
||
| 28 | } |
||
| 29 | |||
| 30 | #[\Override] |
||
| 31 | public function invoke(MethodInvocation $invocation): ResourceObject |
||
| 32 | { |
||
| 33 | $ro = $invocation->proceed(); |
||
| 34 | assert($ro instanceof ResourceObject); |
||
| 35 | if ($ro->code >= Code::BAD_REQUEST) { |
||
| 36 | return $ro; |
||
| 37 | } |
||
| 38 | |||
| 39 | $this->refreshDonutAndState($ro); |
||
| 40 | |||
| 41 | return $ro; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function refreshDonutAndState(ResourceObject $ro): void |
||
| 45 | { |
||
| 46 | $getQuery =($this->matchQuery)($ro); |
||
| 47 | $delUri = clone $ro->uri; |
||
| 48 | $delUri->query = $getQuery; |
||
| 49 | |||
| 50 | // purge donut, resource state cache and etag |
||
| 51 | $this->repository->purge($delUri); |
||
| 52 | // update donut and create resource state |
||
| 53 | $this->refresh($getQuery, $ro); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param array<string, mixed> $getQuery |
||
| 58 | */ |
||
| 59 | private function refresh(array $getQuery, ResourceObject $ro): void |
||
| 60 | { |
||
| 61 | $ro->uri->query = $getQuery; |
||
| 62 | $get = [$ro, 'onGet']; |
||
| 63 | if (is_callable($get)) { |
||
| 64 | call_user_func_array($get, array_values($getQuery)); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 |