Completed
Push — master ( 27d2f2...3153a3 )
by Changwan
04:10
created

Cookie::getMaxAge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Http\Cookie;
3
4
use DateTime;
5
use DateTimeZone;
6
use InvalidArgumentException;
7
8
/**
9
 * @ref Symfony HttpFoundation Cookie
10
 */
11
class Cookie
12
{
13
    /** @var string */
14
    protected $name;
15
16
    /** @var string */
17
    protected $value;
18
19
    /** @var \DateTime */
20
    protected $expire;
21
22
    /** @var string */
23
    protected $path;
24
25
    /** @var string */
26
    protected $domain;
27
28
    /** @var bool */
29
    protected $secure;
30
31
    /** @var bool */
32
    protected $httpOnly;
33
34
    /**
35
     * @param string $name
36
     * @param string $value
37
     * @param \DateTime $expire
38
     * @param string $path
39
     * @param string $domain
40
     * @param bool $secure
41
     * @param bool $httpOnly
42
     */
43 17
    public function __construct(
44
        $name,
45
        $value = null,
46
        DateTime $expire = null,
47
        $path = '/',
48
        $domain = null,
49
        $secure = false,
50
        $httpOnly = true
51
    ) {
52 17
        if (!$name) {
53 1
            throw new InvalidArgumentException('The cookie name cannot be empty.');
54
        }
55 17
        if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
56 1
            throw new InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
57
        }
58 16
        $this->name = $name;
59 16
        $this->value = $value;
60 16
        $this->expire = $expire;
61 16
        $this->path = $path;
62 16
        $this->domain = $domain;
63 16
        $this->secure = $secure;
64 16
        $this->httpOnly = $httpOnly;
65 16
    }
66
67
    /**
68
     * @return string
69
     */
70 1
    public function getName()
71
    {
72 1
        return $this->name;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 8
    public function getValue()
79
    {
80 8
        return $this->value;
81
    }
82
83
    /**
84
     * @return \DateTime
85
     */
86 1
    public function getExpire()
87
    {
88 1
        return $this->expire;
89
    }
90
91
    /**
92
     * @return int
93
     */
94 5
    public function getMaxAge()
95
    {
96 5
        return max(-1, $this->expire->getTimestamp() - time());
97
    }
98
99
    /**
100
     * @return string
101
     */
102 1
    public function getPath()
103
    {
104 1
        return $this->path;
105
    }
106
107
    /**
108
     * @return string
109
     */
110 1
    public function getDomain()
111
    {
112 1
        return $this->domain;
113
    }
114
115
    /**
116
     * @return boolean
117
     */
118 9
    public function isSecure()
119
    {
120 9
        return $this->secure;
121
    }
122
123
    /**
124
     * @return boolean
125
     */
126 9
    public function isHttpOnly()
127
    {
128 9
        return $this->httpOnly;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 8
    public function __toString()
135
    {
136 8
        $stringToReturn = urlencode($this->name).'=';
137 8
        if ($this->value) {
138 7
            $stringToReturn .= urlencode($this->value);
139 7
            if (isset($this->expire)) {
140 5
                $expire = $this->expire->setTimezone(new DateTimeZone('GMT'))->format("l, d-M-Y H:i:s T");
141 5
                $maxAge = $this->getMaxAge();
142 7
                $stringToReturn .= "; Expires={$expire}; Max-Age={$maxAge}";
143
            }
144
        } else {
145 1
            $stringToReturn .= 'deleted; Expires=Thursday, 01-Jan-1970 00:00:00 GMT; Max-Age=0';
146
        }
147 8
        if (isset($this->path)) {
148 8
            $stringToReturn .= '; Path=' . $this->path;
149
        }
150 8
        if (isset($this->domain)) {
151 1
            $stringToReturn .= '; Domain=' . $this->domain;
152
        }
153 8
        if ($this->isSecure()) {
154 1
            $stringToReturn .= '; Secure';
155
        }
156 8
        if ($this->isHttpOnly()) {
157 7
            $stringToReturn .= '; HttpOnly';
158
        }
159 8
        return $stringToReturn;
160
    }
161
}
162