LinkVerifier::__invoke()   C
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 17
cts 17
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 23
nc 2
nop 2
crap 7
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Request\Verifier;
5
6
use Innmind\Rest\Server\{
7
    Request\Verifier\Verifier,
8
    Definition\HttpResource
9
};
10
use Innmind\Http\{
11
    Message\ServerRequest,
12
    Message\Method,
13
    Exception\Http\BadRequest,
14
    Header\LinkValue
15
};
16
use Symfony\Component\Routing\RouterInterface;
17
18
final class LinkVerifier implements Verifier
19
{
20
    private $router;
21
22 10
    public function __construct(RouterInterface $router)
23
    {
24 10
        $this->router = $router;
25 10
    }
26
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws BadRequest
31
     */
32 8
    public function __invoke(
33
        ServerRequest $request,
34
        HttpResource $definition
35
    ): void {
36
        if (
37 8
            (string) $request->method() !== Method::LINK &&
38 8
            (string) $request->method() !== Method::UNLINK
39
        ) {
40 2
            return;
41
        }
42
43
        $request
44 6
            ->headers()
45 6
            ->get('Link')
46 6
            ->values()
47 6
            ->foreach(function(LinkValue $link) use ($definition) {
48 6
                $infos = $this->router->match((string) $link->url());
49
50
                if (
51 6
                    !isset($infos['_innmind_resource']) ||
52 6
                    !isset($infos['identity'])
53
                ) {
54 2
                    throw new BadRequest;
55
                }
56
57 4
                $path = $infos['_innmind_resource'];
58
59
                if (
60 4
                    !$definition->allowedLinks()->contains($link->relationship()) ||
61 4
                    $definition->allowedLinks()->get($link->relationship()) !== $path
62
                ) {
63 2
                    throw new BadRequest;
64
                }
65 6
            });
66
    }
67
}
68