Passed
Push — master ( dfeadf...0d8182 )
by Jean-Christophe
04:51
created

UCookie::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
/**
6
 * Http Cookies utilities
7
 * @author jc
8
 * @version 1.0.0.2
9
 */
10
class UCookie {
11
12
	/**
13
	 * Sends a cookie
14
	 * @param string $name the name of the cookie
15
	 * @param string $value The value of the cookie.
16
	 * @param int $duration default : 1 day
17
	 * @param string $path default : / the cookie will be available within the entire domain
18
	 */
19
	public static function set($name, $value, $duration=60*60*24, $path="/") {
20
		\setcookie($name, $value, \time() + $duration, $path);
21
	}
22
23
	/**
24
	 * Returns the Cookie with the name $name
25
	 * @param string $name
26
	 * @param string $default
27
	 * @return null|string
28
	 */
29 1
	public static function get($name, $default=null) {
30 1
		return isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default;
31
	}
32
33
	/**
34
	 * Removes the cookie with the name $name
35
	 * @param string $name
36
	 * @param $path
37
	 */
38
	public static function delete($name, $path="/") {
39
		if(isset($_COOKIE[$name])){
40
			unset($_COOKIE[$name]);
41
		}
42
		\setcookie($name, "", \time() - 3600, $path);
43
	}
44
45
	/**
46
	 * Deletes all cookies
47
	 */
48
	public function deleteAll($path="/") {
49
		foreach ( $_COOKIE as $name => $value ) {
50
			self::delete($name, $path);
51
		}
52
	}
53
}
54