Passed
Push — master ( 6a9989...c85e47 )
by Jean-Christophe
10:21
created

UCookie::deleteAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.1481
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.4
12
 *
13
 */
14
class UCookie {
15
16
	/**
17
	 * Sends a cookie
18
	 *
19
	 * @param string $name the name of the cookie
20
	 * @param string $value The value of the cookie.
21
	 * @param int $duration default : 1 day
22
	 * @param string $path default : / the cookie will be available within the entire domain
23
	 * @param boolean $secure Indicates that the cookie should only be transmitted over asecure HTTPS
24
	 * @param boolean $httpOnly When true the cookie will be made accessible only through the HTTPprotocol
25
	 * @return boolean
26
	 */
27 1
	public static function set($name, $value, $duration = 60*60*24, $path = '/', $secure = false, $httpOnly = false): bool {
28 1
		return \setcookie ( $name, $value, \time () + $duration, $path, $secure, $httpOnly );
29
	}
30
31
	/**
32
	 * Returns the Cookie with the name $name
33
	 *
34
	 * @param string $name
35
	 * @param string $default
36
	 * @return ?string
37
	 */
38 2
	public static function get($name, $default = null): ?string {
39 2
		return isset ( $_COOKIE [$name] ) ? $_COOKIE [$name] : $default;
40
	}
41
42
	/**
43
	 * Removes the cookie with the name $name
44
	 *
45
	 * @param string $name
46
	 * @param string $path
47
	 */
48 1
	public static function delete($name, $path = '/'): bool {
49 1
		if (isset ( $_COOKIE [$name] )) {
50
			unset ( $_COOKIE [$name] );
51
		}
52 1
		return \setcookie ( $name, '', \time () - 3600, $path );
53
	}
54
55
	/**
56
	 * Deletes all cookies
57
	 */
58 1
	public static function deleteAll($path = '/'): void {
59 1
		foreach ( $_COOKIE as $name => $value ) {
60
			self::delete ( $name, $path );
61
		}
62 1
	}
63
64
	/**
65
	 * Tests the existence of a cookie
66
	 *
67
	 * @param string $name
68
	 * @return boolean
69
	 * @since Ubiquity 2.0.11
70
	 */
71
	public static function exists($name): bool {
72
		return isset ( $_COOKIE [$name] );
73
	}
74
75
	/**
76
	 * Sends a raw cookie without urlencoding the cookie value
77
	 *
78
	 * @param string $name the name of the cookie
79
	 * @param string $value The value of the cookie.
80
	 * @param int $duration default : 1 day
81
	 * @param string $path default : / the cookie will be available within the entire domain
82
	 * @param boolean $secure Indicates that the cookie should only be transmitted over asecure HTTPS
83
	 * @param boolean $httpOnly When true the cookie will be made accessible only through the HTTPprotocol
84
	 * @return boolean
85
	 * @since Ubiquity 2.0.11
86
	 */
87
	public static function setRaw($name, $value, $duration = 60*60*24, $path = '/', $secure = false, $httpOnly = false): bool {
88
		return \setrawcookie ( $name, $value, \time () + $duration, $path, $secure, $httpOnly );
89
	}
90
}
91