1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\RepositoryModule\Annotation\AbstractCommand; |
8
|
|
|
use BEAR\RepositoryModule\Annotation\Purge; |
9
|
|
|
use BEAR\RepositoryModule\Annotation\Refresh; |
10
|
|
|
use BEAR\Resource\ResourceInterface; |
11
|
|
|
use BEAR\Resource\ResourceObject; |
12
|
|
|
use BEAR\Resource\Uri; |
13
|
|
|
use Ray\Aop\MethodInvocation; |
14
|
|
|
|
15
|
|
|
use function is_array; |
16
|
|
|
use function uri_template; |
17
|
|
|
|
18
|
|
|
final class RefreshAnnotatedCommand implements CommandInterface |
19
|
|
|
{ |
20
|
|
|
public function __construct( |
21
|
|
|
private readonly QueryRepositoryInterface $repository, |
22
|
|
|
private readonly ResourceInterface $resource, |
23
|
|
|
) { |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function command(MethodInvocation $invocation, ResourceObject $ro): void |
27
|
|
|
{ |
28
|
15 |
|
$method = $invocation->getMethod(); |
29
|
|
|
$annotations = $method->getAnnotations(); |
30
|
|
|
foreach ($annotations as $annotation) { |
31
|
|
|
$this->request($ro, $annotation); |
32
|
15 |
|
} |
33
|
15 |
|
} |
34
|
15 |
|
|
35
|
|
|
private function getUri(ResourceObject $ro, AbstractCommand $annotation): string |
36
|
9 |
|
{ |
37
|
|
|
$body = is_array($ro->body) ? $ro->body : []; |
38
|
|
|
$query = $body + $ro->uri->query; |
39
|
9 |
|
|
40
|
9 |
|
return uri_template($annotation->uri, $query); |
41
|
9 |
|
} |
42
|
4 |
|
|
43
|
|
|
private function request(ResourceObject $ro, object $annotation): void |
44
|
9 |
|
{ |
45
|
|
|
if (! $annotation instanceof AbstractCommand) { |
46
|
4 |
|
return; |
47
|
|
|
} |
48
|
4 |
|
|
49
|
4 |
|
$uri = new Uri($this->getUri($ro, $annotation)); |
50
|
|
|
if ($annotation instanceof Purge) { |
51
|
4 |
|
$this->repository->purge($uri); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
if ($annotation instanceof Refresh) { |
55
|
|
|
$this->repository->purge($uri); |
56
|
4 |
|
$ro = $this->resource->get((string) $uri); |
57
|
2 |
|
$this->repository->put($ro); |
58
|
|
|
} |
59
|
4 |
|
} |
60
|
|
|
} |
61
|
|
|
|