Passed
Push — master ( cf11ab...94e773 )
by Jean-Christophe
07:00
created

UCookie   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 7
eloc 8
dl 0
loc 48
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 2 2
A deleteAll() 0 3 2
A delete() 0 5 2
A set() 0 2 1
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
/**
6
 * Http Cookies utilities
7
 * Ubiquity\utils\http$UCookie
8
 * This class is part of Ubiquity
9
 *
10
 * @author jcheron <[email protected]>
11
 * @version 1.0.2
12
 *
13
 */
14
class UCookie {
15
16
	/**
17
	 * Sends a cookie
18
	 *
19
	 * @param string $name
20
	 *        	the name of the cookie
21
	 * @param string $value
22
	 *        	The value of the cookie.
23
	 * @param int $duration
24
	 *        	default : 1 day
25
	 * @param string $path
26
	 *        	default : / the cookie will be available within the entire domain
27
	 */
28 1
	public static function set($name, $value, $duration = 60*60*24, $path = "/") {
29 1
		\setcookie ( $name, $value, \time () + $duration, $path );
30 1
	}
31
32
	/**
33
	 * Returns the Cookie with the name $name
34
	 *
35
	 * @param string $name
36
	 * @param string $default
37
	 * @return null|string
38
	 */
39 2
	public static function get($name, $default = null) {
40 2
		return isset ( $_COOKIE [$name] ) ? $_COOKIE [$name] : $default;
41
	}
42
43
	/**
44
	 * Removes the cookie with the name $name
45
	 *
46
	 * @param string $name
47
	 * @param string $path
48
	 */
49 1
	public static function delete($name, $path = "/") {
50 1
		if (isset ( $_COOKIE [$name] )) {
51
			unset ( $_COOKIE [$name] );
52
		}
53 1
		\setcookie ( $name, "", \time () - 3600, $path );
54 1
	}
55
56
	/**
57
	 * Deletes all cookies
58
	 */
59 1
	public static function deleteAll($path = "/") {
60 1
		foreach ( $_COOKIE as $name => $value ) {
61
			self::delete ( $name, $path );
62
		}
63 1
	}
64
}
65