|
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
|
|
|
/** |
|
26
|
|
|
* @param MethodInvocation $invocation |
|
27
|
|
|
* @param ResourceObject $resourceObject |
|
28
|
|
|
*/ |
|
29
|
8 |
|
public function command(MethodInvocation $invocation, ResourceObject $resourceObject) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$method = $invocation->getMethod()->getName(); |
|
|
|
|
|
|
32
|
8 |
|
if ($method === 'onGet' || $method === 'onPost') { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
8 |
|
unset($invocation); |
|
36
|
8 |
|
$onGet = [$resourceObject, 'onGet']; |
|
37
|
8 |
|
$getQuery = $this->getQuery($resourceObject, $onGet); |
|
|
|
|
|
|
38
|
7 |
|
$delUri = clone $resourceObject->uri; |
|
39
|
7 |
|
$delUri->query = $getQuery; |
|
40
|
|
|
|
|
41
|
|
|
// delete data in repository |
|
42
|
7 |
|
$this->repository->purge($delUri); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
// GET for re-generate (in interceptor) |
|
45
|
7 |
|
$resourceObject->uri->query = $getQuery; |
|
46
|
7 |
|
call_user_func_array($onGet, $getQuery); |
|
47
|
7 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ResourceObject $resourceObject |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
8 |
|
private function getQuery(ResourceObject $resourceObject) |
|
55
|
|
|
{ |
|
56
|
8 |
|
$refParameters = (new \ReflectionMethod($resourceObject, 'onGet'))->getParameters(); |
|
57
|
8 |
|
$getQuery = []; |
|
58
|
8 |
|
$query = $resourceObject->uri->query; |
|
59
|
8 |
|
foreach ($refParameters as $parameter) { |
|
60
|
8 |
|
if (! isset($query[$parameter->name])) { |
|
61
|
1 |
|
throw new UnmatchedQuery($resourceObject->uri->method . ' ' . (string) $resourceObject->uri); |
|
62
|
|
|
} |
|
63
|
8 |
|
$getQuery[$parameter->name] = $query[$parameter->name]; |
|
64
|
8 |
|
} |
|
65
|
|
|
|
|
66
|
7 |
|
return $getQuery; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|