OriginNegotiator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 36
ccs 14
cts 14
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A addAllowOrigin() 0 3 1
A negotiate() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Cors\Negotiation\Origin;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
final class OriginNegotiator implements OriginNegotiatorInterface
10
{
11
    /**
12
     * @var array<AllowOriginInterface>
13
     */
14
    private $allowOrigins;
15
16
    /**
17
     * @param array<AllowOriginInterface> $allowOrigins
18
     */
19 3
    public function __construct(array $allowOrigins)
20
    {
21 3
        $this->allowOrigins = [];
22 3
        foreach ($allowOrigins as $allowOrigin) {
23 3
            $this->addAllowOrigin($allowOrigin);
24
        }
25 3
    }
26
27 3
    public function negotiate(ServerRequestInterface $request): ?string
28
    {
29 3
        if ('' === $origin = $request->getHeaderLine(self::HEADER)) {
30 1
            return null;
31
        }
32
33 2
        foreach ($this->allowOrigins as $allowOrigin) {
34 2
            if ($allowOrigin->match($origin)) {
35 2
                return $origin;
36
            }
37
        }
38
39 1
        return null;
40
    }
41
42 3
    private function addAllowOrigin(AllowOriginInterface $allowOrigin): void
43
    {
44 3
        $this->allowOrigins[] = $allowOrigin;
45 3
    }
46
}
47