bearsunday /
BEAR.QueryRepository
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BEAR\QueryRepository; |
||
| 4 | |||
| 5 | use BEAR\Resource\ResourceObject; |
||
| 6 | use Ray\Aop\MethodInvocation; |
||
| 7 | use ReflectionException; |
||
| 8 | use function array_values; |
||
| 9 | use function call_user_func_array; |
||
| 10 | use function is_callable; |
||
| 11 | |||
| 12 | // phpcs:ignoreFile SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing -- for call_user_func_array |
||
| 13 | |||
| 14 | final readonly class RefreshSameCommand implements CommandInterface |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 15 | { |
||
| 16 | public function __construct( |
||
| 17 | private QueryRepositoryInterface $repository, |
||
| 18 | private MatchQueryInterface $matchQuery |
||
| 19 | ){ |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return void |
||
| 24 | */ |
||
| 25 | #[\Override] |
||
| 26 | public function command(MethodInvocation $invocation, ResourceObject $ro) |
||
| 27 | { |
||
| 28 | unset($invocation); |
||
| 29 | $getQuery = $this->getQuery($ro); |
||
| 30 | $delUri = clone $ro->uri; |
||
| 31 | $delUri->query = $getQuery; |
||
| 32 | |||
| 33 | // delete data in repository |
||
| 34 | $this->repository->purge($delUri); |
||
| 35 | |||
| 36 | // GET for re-generate (in interceptor) |
||
| 37 | $ro->uri->query = $getQuery; |
||
| 38 | $get = [$ro, 'onGet']; |
||
| 39 | if (is_callable($get)) { |
||
| 40 | call_user_func_array($get, array_values($getQuery)); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return array<string, mixed> |
||
| 46 | * |
||
| 47 | * @throws ReflectionException |
||
| 48 | */ |
||
| 49 | private function getQuery(ResourceObject $ro): array |
||
| 50 | { |
||
| 51 | return $this->matchQuery->__invoke($ro); |
||
| 52 | } |
||
| 53 | } |
||
| 54 |