Passed
Pull Request — 1.x (#99)
by Akihito
10:41
created

MatchQuery::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 10
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 ReflectionMethod;
10
11
use function get_class;
12
use function sprintf;
13
14
final class MatchQuery implements MatchQueryInterface
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function __invoke(ResourceObject $ro): array
20
    {
21
        $refParameters = (new ReflectionMethod(get_class($ro), 'onGet'))->getParameters();
22
        $getQuery = [];
23
        $query = $ro->uri->query;
24
        foreach ($refParameters as $parameter) {
25
            if (! isset($query[$parameter->name])) {
26
                throw new UnmatchedQuery(sprintf('%s %s', $ro->uri->method, (string) $ro->uri));
27
            }
28
29
            /** @psalm-suppress MixedAssignment */
30
            $getQuery[$parameter->name] = $query[$parameter->name];
31
        }
32
33
        return $getQuery;
34
    }
35
}
36