Completed
Pull Request — master (#31)
by Marco
05:52
created

SameSite::asString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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