Cookie::destroy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP;
3
4
/**
5
 * Class Cookie
6
 * @package FMUP
7
 */
8
class Cookie
9
{
10
    private static $instance;
11
12 1
    private function __construct()
13
    {
14 1
    }
15
16
    /**
17
     * @codeCoverageIgnore
18
     */
19
    private function __clone()
20
    {
21
    }
22
23
    /**
24
     * Retrieve cookie system - start cookie if not started
25
     * @return Cookie
26
     */
27 2
    final public static function getInstance()
28
    {
29 2
        if (!isset(self::$instance)) {
30 1
            $class = get_called_class();
31 1
            self::$instance = new $class;
32
        }
33 2
        return self::$instance;
34
    }
35
36
    /**
37
     * Check whether a specific information exists in cookie
38
     * @param string $name
39
     * @throws Exception if parameter is not a string
40
     * @return bool
41
     */
42 4
    public function has($name)
0 ignored issues
show
Coding Style introduced by
has uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
43
    {
44 4
        if (!is_string($name)) {
45 3
            throw new Exception('Parameter must be a string');
46
        }
47 4
        return isset($_COOKIE[$name]);
48
    }
49
50
    /**
51
     * Define a new cookie
52
     * @param string $name
53
     * @param string $value
54
     * @param int $expire
55
     * @param string $path
56
     * @param string $domain
57
     * @param bool $secure
58
     * @param bool $httpOnly
59
     * @throws Exception if parameter is not a string
60
     * @return $this
61
     */
62 1
    public function set($name, $value, $expire = 0, $path = "/", $domain = "", $secure = false, $httpOnly = false)
63
    {
64 1
        if (!is_string($name)) {
65 1
            throw new Exception('Parameter must be a string');
66
        }
67 1
        $time = time();
68 1
        if ($expire < $time) {
69 1
            $expire = $time + $expire;
70
        }
71 1
        $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
72 1
        return $this;
73
    }
74
75
    /**
76
     * Method that sends cookie - Unit test only
77
     * @codeCoverageIgnore
78
     * @param $name
79
     * @param $value
80
     * @param int $expire
81
     * @param string $path
82
     * @param string $domain
83
     * @param bool|false $secure
84
     * @param bool|false $httpOnly
85
     * @return bool
86
     */
87
    protected function setCookie(
88
        $name,
89
        $value,
90
        $expire = 0,
91
        $path = "/",
92
        $domain = "",
93
        $secure = false,
94
        $httpOnly = false
95
    ) {
96
        return setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
97
    }
98
99
    /**
100
     * Retrieve a specific Cookie value
101
     * @param string $name
102
     * @throws Exception if parameter is not a string
103
     * @return mixed
104
     */
105 1
    public function get($name)
0 ignored issues
show
Coding Style introduced by
get uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
106
    {
107 1
        return $this->has($name) ? $_COOKIE[$name] : null;
108
    }
109
110
    /**
111
     * Remove a specific Cookie
112
     * @param string $name
113
     * @param string $path
114
     * @param string $domain
115
     * @param bool $secure
116
     * @return $this
117
     */
118 2
    public function remove($name, $path = "/", $domain = "", $secure = false)
0 ignored issues
show
Coding Style introduced by
remove uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
119
    {
120 2
        if ($this->has($name)) {
121 2
            $this->setCookie($name, "", time() - (3600 * 24), $path, $domain, $secure);
122 2
            unset($_COOKIE[$name]);
123
        }
124 2
        return $this;
125
    }
126
127
    /**
128
     * Remove all Cookies
129
     * @return $this
130
     */
131 1
    public function destroy()
0 ignored issues
show
Coding Style introduced by
destroy uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
132
    {
133 1
        foreach (array_keys($_COOKIE) as $cookie) {
134 1
            $this->remove($cookie);
135
        }
136 1
        return $this;
137
    }
138
}
139