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

UCookie   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 7
eloc 8
dl 0
loc 41
ccs 2
cts 13
cp 0.1538
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAll() 0 3 2
A set() 0 2 1
A get() 0 2 2
A delete() 0 5 2
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