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

OriginNegotiator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 AllowOriginInterface[]
13
     */
14
    private $allowOrigins;
15
16
    /**
17
     * @param 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
    /**
28
     * @param AllowOriginInterface $allowOrigin
29
     */
30 3
    private function addAllowOrigin(AllowOriginInterface $allowOrigin): void
31
    {
32 3
        $this->allowOrigins[] = $allowOrigin;
33 3
    }
34
35
    /**
36
     * @param ServerRequestInterface $request
37
     *
38
     * @return string|null
39
     */
40 3
    public function negotiate(ServerRequestInterface $request): ?string
41
    {
42 3
        if ('' === $origin = $request->getHeaderLine(self::HEADER)) {
43 1
            return null;
44
        }
45
46 2
        foreach ($this->allowOrigins as $allowOrigin) {
47 2
            if ($allowOrigin->match($origin)) {
48 2
                return $origin;
49
            }
50
        }
51
52 1
        return null;
53
    }
54
}
55