|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the BEAR.QueryRepository package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace BEAR\QueryRepository; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\QueryRepository\Exception\UnmatchedQuery; |
|
10
|
|
|
use BEAR\Resource\ResourceObject; |
|
11
|
|
|
use Ray\Aop\MethodInvocation; |
|
12
|
|
|
|
|
13
|
|
|
class RefreshSameCommand implements CommandInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var QueryRepositoryInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $repository; |
|
19
|
|
|
|
|
20
|
11 |
|
public function __construct(QueryRepositoryInterface $repository) |
|
21
|
|
|
{ |
|
22
|
11 |
|
$this->repository = $repository; |
|
23
|
11 |
|
} |
|
24
|
|
|
|
|
25
|
8 |
|
public function command(MethodInvocation $invocation, ResourceObject $ro) |
|
26
|
|
|
{ |
|
27
|
8 |
|
$method = $invocation->getMethod()->getName(); |
|
|
|
|
|
|
28
|
8 |
|
if ($method === 'onGet' || $method === 'onPost') { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
8 |
|
unset($invocation); |
|
32
|
8 |
|
$onGet = [$ro, 'onGet']; |
|
33
|
8 |
|
$getQuery = $this->getQuery($ro); |
|
34
|
7 |
|
$delUri = clone $ro->uri; |
|
35
|
7 |
|
$delUri->query = $getQuery; |
|
36
|
|
|
|
|
37
|
|
|
// delete data in repository |
|
38
|
7 |
|
$this->repository->purge($delUri); |
|
39
|
|
|
|
|
40
|
|
|
// GET for re-generate (in interceptor) |
|
41
|
7 |
|
$ro->uri->query = $getQuery; |
|
42
|
7 |
|
\call_user_func_array($onGet, $getQuery); |
|
43
|
7 |
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
private function getQuery(ResourceObject $resourceObject) : array |
|
46
|
|
|
{ |
|
47
|
8 |
|
$refParameters = (new \ReflectionMethod($resourceObject, 'onGet'))->getParameters(); |
|
48
|
8 |
|
$getQuery = []; |
|
49
|
8 |
|
$query = $resourceObject->uri->query; |
|
50
|
8 |
|
foreach ($refParameters as $parameter) { |
|
51
|
8 |
|
if (! isset($query[$parameter->name])) { |
|
52
|
1 |
|
throw new UnmatchedQuery($resourceObject->uri->method . ' ' . (string) $resourceObject->uri); |
|
53
|
|
|
} |
|
54
|
8 |
|
$getQuery[$parameter->name] = $query[$parameter->name]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
7 |
|
return $getQuery; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|