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