Completed
Push — develop ( 25764f...92e89c )
by Baptiste
07:12
created

LinkVerifier::verify()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

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