Cookie   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 88
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 6 2
A del() 0 8 2
A exists() 0 4 1
A set() 0 4 1
1
<?php /** MicroCookie */
2
3
namespace Micro\Web;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
7
/**
8
 * Cookie class file.
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/linpax/microphp-framework
12
 * @copyright Copyright (c) 2013 Oleg Lunegov
13
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
14
 * @package Micro
15
 * @subpackage Web
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class Cookie implements ICookie
20
{
21
    /** @var ServerRequestInterface $request */
22
    protected $request;
23
24
25
    /**
26
     * Constructor of object
27
     *
28
     * @access public
29
     *
30
     * @param ServerRequestInterface $request
31
     *
32
     * @result void
33
     */
34
    public function __construct(ServerRequestInterface $request)
35
    {
36
        $this->request = $request;
37
    }
38
39
    /**
40
     * Get cookie
41
     *
42
     * @access public
43
     *
44
     * @param string $name cookie name
45
     *
46
     * @return mixed|bool
47
     */
48
    public function get($name)
49
    {
50
        $cookie = $this->request->getCookieParams();
51
52
        return array_key_exists($name, $cookie) ? $cookie[$name] : null;
53
    }
54
55
    /**
56
     * Delete cookie
57
     *
58
     * @access public
59
     *
60
     * @param string $name cookie name
61
     *
62
     * @return bool
63
     */
64
    public function del($name)
65
    {
66
        if ($this->exists($name)) {
67
            return $this->set($name, false, time() - 3600);
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Exists cookie
75
     *
76
     * @access public
77
     *
78
     * @param string $name cookie name
79
     *
80
     * @return bool
81
     */
82
    public function exists($name)
83
    {
84
        return array_key_exists($name, $this->request->getCookieParams());
85
    }
86
87
    /**
88
     * Set cookie
89
     *
90
     * @access public
91
     *
92
     * @param string $name cookie name
93
     * @param mixed $value data value
94
     * @param int $expire life time
95
     * @param string $path path access cookie
96
     * @param string $domain domain access cookie
97
     * @param bool $secure use SSL?
98
     * @param bool $httponly disable on JS?
99
     *
100
     * @return bool
101
     */
102
    public function set($name, $value, $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = true)
103
    {
104
        return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
105
    }
106
}
107