RefreshAnnotatedCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUri() 0 6 2
A request() 0 15 4
A command() 0 6 2
A __construct() 0 4 1
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