ServerHeadersBasedHostDetector::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\MultiEnv\HostDetector;
6
7
use Lamoda\MultiEnv\HostDetector\Exception\HostDetectorException;
8
use Lamoda\MultiEnv\Model\HostId;
9
10
final class ServerHeadersBasedHostDetector implements HostDetectorInterface
11
{
12
    /**
13
     * @var string $needle
14
     */
15
    private $needle;
16
17
    /**
18
     * @param string $needle
19
     * @throws HostDetectorException
20
     */
21 16
    public function __construct(string $needle)
22
    {
23 16
        $needle = trim($needle);
24
25 16
        if (empty($needle)) {
26 3
            throw HostDetectorException::becauseEmptyNeedlePassed(self::class);
27
        }
28
29 13
        $this->needle = $needle;
30 13
    }
31
32 31
    public function getCurrentHost(): HostId
33
    {
34 31
        $hostId = (string)($_SERVER[$this->needle] ?? '');
35
36 31
        return new HostId($hostId);
37
    }
38
}
39