Cookie::unsetId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Sesshin\Id\Store;
3
4
use Sesshin\Exception;
5
6
class Cookie implements StoreInterface
7
{
8
    /** @var string */
9
    private $id;
10
11
    /** @var string Session cookie name. */
12
    private $name;
13
14
    /** @var string Session cookie path */
15
    private $path;
16
17
    /** @var string Session cookie domain */
18
    private $domain;
19
20
    /** @var bool Should cookie be secure (SSL)? */
21
    private $secure;
22
23
    /** @var bool */
24
    private $httpOnly;
25
26
    /**
27
     * @param string $name
28
     * @param string $path
29
     * @param string|null $domain
30
     * @param bool $secure
31
     * @param bool $httpOnly
32
     */
33
    public function __construct($name = 'sid', $path = '/', $domain = null, $secure = false, $httpOnly = true)
34
    {
35
        $this->name = $name;
36
        $this->path = $path;
37
        $this->domain = $domain;
38
        $this->secure = $secure;
39
        $this->httpOnly = $httpOnly;
40
    }
41
42
    /**
43
     * @param string $id
44
     */
45
    public function setId($id)
46
    {
47
        if (setcookie($this->name, $id, 0, $this->path, $this->domain, $this->secure, $this->httpOnly)) {
48
            $this->id = $id;
49
        }
50
    }
51
52
    /**
53
     * @return string
54
     * @throws Exception
55
     */
56
    public function getId()
57
    {
58
        if ($this->issetId()) {
59
            return isset($this->id) ? $this->id : $_COOKIE[$this->name];
60
        }
61
        throw new Exception('Id is not set');
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    public function issetId()
68
    {
69
        return (isset($this->id) || isset($_COOKIE[$this->name]));
70
    }
71
72
    /**
73
     * @return void
74
     */
75
    public function unsetId()
76
    {
77
        setcookie($this->name, '', 1, $this->path, $this->domain, $this->secure, $this->httpOnly);
78
    }
79
}
80