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

MethodNegotiator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 54
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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