Cookie   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 2
b 0
f 0
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A set() 0 23 2
A remove() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Libraries;
6
7
final class Cookie extends \WebServCo\Framework\AbstractLibrary
8
{
9
    public function get(string $name, string $defaultValue = ''): string
10
    {
11
        return $_COOKIE[$name] ?? $defaultValue;
12
    }
13
14
    public function remove(string $name): bool
15
    {
16
        if (!isset($_COOKIE[$name])) {
17
            return false;
18
        }
19
20
21
        unset($_COOKIE[$name]);
22
        $this->set($name, '', -1);
23
        return true;
24
    }
25
26
    /**
27
    * @param string $name,
28
    * @param string $value,
29
    * @param int $expires,
30
    * @param string $path,
31
    * @param string $domain,
32
    * @param bool $secure,
33
    * @param bool $httponly,
34
    * @param 'Lax'|'None'|'Strict' $samesite
0 ignored issues
show
Documentation Bug introduced by
The doc comment 'Lax'|'None'|'Strict' at position 0 could not be parsed: Unknown type name ''Lax'' at position 0 in 'Lax'|'None'|'Strict'.
Loading history...
35
    */
36
    public function set(
37
        string $name,
38
        string $value = '',
39
        int $expires = 0,
40
        string $path = '',
41
        string $domain = '',
42
        bool $secure = true,
43
        bool $httponly = false,
44
        string $samesite = 'Lax'
45
    ): bool {
46
        if (!\in_array($samesite, ['Lax', 'None', 'Strict'], true)) {
47
            throw new \InvalidArgumentException('Invalid argument: samesite');
48
        }
49
        return \setcookie(
50
            $name,
51
            \WebServCo\Framework\Helpers\RequestHelper::sanitizeString($value),
52
            [
53
                'domain' => $domain,
54
                'expires' => $expires,
55
                'httponly' => $httponly,
56
                'path' => $path,
57
                'samesite' => $samesite,
58
                'secure' => $secure,
59
            ],
60
        );
61
    }
62
}
63