Passed
Push — master ( 974680...fc5316 )
by Kirill
03:59
created

SameSite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Cookies\Cookie;
6
7
class SameSite
8
{
9
    public const STRICT = 'Strict';
10
    public const LAX    = 'Lax';
11
    public const NONE   = 'None';
12
13
    private const VALUES  = [self::STRICT, self::LAX, self::NONE];
14
    private const DEFAULT = self::LAX;
15
16
    /** @var string|null */
17
    private $sameSite;
18
19
    public function __construct(?string $sameSite = null, bool $secure = false)
20
    {
21
        $this->sameSite = $this->defineValue($sameSite, $secure);
22
    }
23
24
    public function get(): ?string
25
    {
26
        return $this->sameSite;
27
    }
28
29
    /**
30
     * @param string|null $sameSite
31
     * @param bool        $secure
32
     * @return string|null
33
     */
34
    private function defineValue(?string $sameSite, bool $secure): ?string
35
    {
36
        if ($sameSite === null) {
37
            return null;
38
        }
39
40
        $sameSite = ucfirst(strtolower($sameSite));
41
        if (!in_array($sameSite, self::VALUES, true)) {
42
            return null;
43
        }
44
45
        return ($sameSite === self::NONE && !$secure) ? self::DEFAULT : $sameSite;
46
    }
47
}
48