RefreshAnnotatedCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 41
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0
wmc 9

4 Methods

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