MethodNegotiator::negotiate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Cors\Negotiation;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
final class MethodNegotiator implements MethodNegotiatorInterface
10
{
11
    /**
12
     * @var array<string>
13
     */
14
    private $allowMethods;
15
16
    /**
17
     * @param array<string> $allowMethods
18
     */
19 4
    public function __construct(array $allowMethods)
20
    {
21 4
        $this->allowMethods = [];
22 4
        foreach ($allowMethods as $allowMethod) {
23 4
            $this->addAllowMethod($allowMethod);
24
        }
25 4
    }
26
27 3
    public function negotiate(ServerRequestInterface $request): bool
28
    {
29 3
        if ('' === $method = $request->getHeaderLine(self::HEADER)) {
30 1
            return false;
31
        }
32
33 2
        foreach ($this->allowMethods as $allowMethod) {
34 2
            if ($allowMethod === $method) {
35 2
                return true;
36
            }
37
        }
38
39 1
        return false;
40
    }
41
42
    /**
43
     * @return array<string>
44
     */
45 1
    public function getAllowedMethods(): array
46
    {
47 1
        return $this->allowMethods;
48
    }
49
50 4
    private function addAllowMethod(string $allowMethod): void
51
    {
52 4
        $this->allowMethods[] = $allowMethod;
53 4
    }
54
}
55