Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

Cookie   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

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

4 Methods

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