Cookie::getCookie()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

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
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nymfonya\Component\Http;
6
7
use Nymfonya\Component\Http\Interfaces\CookieInterface;
8
9
class Cookie implements CookieInterface
10
{
11
12
    protected $cookie;
13
14
    /**
15
     * instanciate
16
     *
17
     */
18 4
    public function __construct()
19
    {
20 4
        $this->refreshCookie();
21
    }
22
23
    /**
24
     * get cookie value from cookie name
25
     *
26
     * @param string $name
27
     * @return string
28
     */
29 1
    public function getCookie(string $name): string
30
    {
31 1
        return (isset($this->cookie[$name])) ? $this->cookie[$name] : '';
32
    }
33
34
    /**
35
     * set cookie value for cookie name and ttl
36
     *
37
     * @param string $name
38
     * @param string $value
39
     * @param integer $ttl
40
     * @return CookieInterface
41
     */
42 1
    public function setCookie(string $name, string $value, int $ttl): CookieInterface
43
    {
44 1
        setcookie($name, $value, time() + $ttl);
45 1
        return $this->refreshCookie();
46
    }
47
48
    /**
49
     * refresh cookie from global
50
     *
51
     * @return CookieInterface
52
     */
53 1
    protected function refreshCookie(): CookieInterface
54
    {
55 1
        $this->cookie = $_COOKIE;
56 1
        return $this;
57
    }
58
}
59