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

SameSite   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 15
c 1
b 0
f 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 3 1
A defineValue() 0 12 5
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