Completed
Push — master ( 21e2dd...2fbabb )
by Dominik
02:22 queued 20s
created

MethodNegotiator::negotiate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 string[]
13
     */
14
    private $allowMethods;
15
16
    /**
17
     * @param 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
    /**
28
     * @param string $allowMethod
29
     */
30 4
    private function addAllowMethod(string $allowMethod): void
31
    {
32 4
        $this->allowMethods[] = $allowMethod;
33 4
    }
34
35
    /**
36
     * @param ServerRequestInterface $request
37
     *
38
     * @return bool
39
     */
40 3
    public function negotiate(ServerRequestInterface $request): bool
41
    {
42 3
        if ('' === $method = $request->getHeaderLine(self::HEADER)) {
43 1
            return false;
44
        }
45
46 2
        foreach ($this->allowMethods as $allowMethod) {
47 2
            if ($allowMethod === $method) {
48 2
                return true;
49
            }
50
        }
51
52 1
        return false;
53
    }
54
55
    /**
56
     * @return string[]
57
     */
58 1
    public function getAllowedMethods(): array
59
    {
60 1
        return $this->allowMethods;
61
    }
62
}
63