MatchQuery   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\QueryRepository\Exception\UnmatchedQuery;
8
use BEAR\Resource\ResourceObject;
9
use Override;
10
use ReflectionMethod;
11
12
use function sprintf;
13
14
final class MatchQuery implements MatchQueryInterface
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    #[Override]
20
    public function __invoke(ResourceObject $ro): array
21
    {
22
        $refParameters = (new ReflectionMethod($ro::class, 'onGet'))->getParameters();
23
        $getQuery = [];
24
        $query = $ro->uri->query;
25
        foreach ($refParameters as $parameter) {
26
            if (! isset($query[$parameter->name])) {
27
                throw new UnmatchedQuery(sprintf('%s %s', $ro->uri->method, (string) $ro->uri));
28
            }
29
30
            /** @psalm-suppress MixedAssignment */
31
            $getQuery[$parameter->name] = $query[$parameter->name];
32
        }
33
34
        return $getQuery;
35
    }
36
}
37