Completed
Push — master ( 59abc1...a59857 )
by Beau
35s
created

SameSite   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A strict() 0 4 1
A lax() 0 4 1
A fromString() 0 17 3
A asString() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FigCookies\Modifier;
6
7
use function sprintf;
8
use function strtolower;
9
10
final class SameSite
11
{
12
    /** @var bool */
13
    private $strict;
14
15
    private function __construct(bool $strict)
16
    {
17
        $this->strict = $strict;
18
    }
19
20
    public static function strict() : self
21
    {
22
        return new self(true);
23
    }
24
25
    public static function lax() : self
26
    {
27
        return new self(false);
28
    }
29
30
    /**
31
     * @throws \InvalidArgumentException If the given SameSite string is neither strict nor lax.
32
     */
33
    public static function fromString(string $sameSite) : self
34
    {
35
        $lowerCaseSite = strtolower($sameSite);
36
37
        if ($lowerCaseSite === 'strict') {
38
            return self::strict();
39
        }
40
41
        if ($lowerCaseSite === 'lax') {
42
            return self::lax();
43
        }
44
45
        throw new \InvalidArgumentException(sprintf(
46
            'Expected modifier value to be either "strict" or "lax", "%s" given',
47
            $sameSite
48
        ));
49
    }
50
51
    public function asString() : string
52
    {
53
        return 'SameSite=' . ($this->strict ? 'Strict' : 'Lax');
54
    }
55
}
56