MethodNegotiator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 44
ccs 16
cts 16
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getAllowedMethods() 0 3 1
A negotiate() 0 13 4
A addAllowMethod() 0 3 1
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