Passed
Push — master ( 199d92...bf1436 )
by Spuds
39s
created

Cookie::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
/**
3
 * @name      OpenImporter
4
 * @copyright OpenImporter contributors
5
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
6
 *
7
 * @version 1.0 Alpha
8
 */
9
10
namespace OpenImporter;
11
12
/**
13
 * We need Cooooookies.
14
 *
15
 * @package OpenImporter
16
 */
17
class Cookie
18
{
19
	/**
20
	 * Cookie constructor.
21
	 */
22 4
	public function __construct()
23
	{
24 4
		return true;
25
	}
26
27
	/**
28
	 * Set a cookie
29
	 *
30
	 * @param mixed[] $data
31
	 * @param string $name
32
	 * @return boolean
33
	 */
34 4
	public function set($data, $name = 'openimporter_cookie')
35
	{
36 4
		if (!empty($data))
37
		{
38 4
			setcookie($name, serialize($data), time()+ 86400);
39 4
			$_COOKIE[$name] = serialize($data);
40
41 4
			return true;
42
		}
43
44 1
		return false;
45
	}
46
47
	/**
48
	 * Get our cookie
49
	 *
50
	 * @param string $name
51
	 *
52
	 * @return boolean
53
	 */
54 2
	public function get($name = 'openimporter_cookie')
55
	{
56 2
		if (isset($_COOKIE[$name]))
57
		{
58 2
			$cookie = unserialize($_COOKIE[$name]);
59
60 2
			return $cookie;
61
		}
62
63 1
		return false;
64
	}
65
66
	/**
67
	 * Once we are done, we should destroy our cookie
68
	 *
69
	 * @param string $name
70
	 *
71
	 * @return boolean
72
	 */
73 1
	public function destroy($name = 'openimporter_cookie')
74
	{
75 1
		setcookie($name, '');
76 1
		unset($_COOKIE[$name]);
77
78 1
		return true;
79
	}
80
81
	/**
82
	 * Extend the cookie with new information
83
	 *
84
	 * @param mixed[] $data
85
	 * @param string $name
86
	 *
87
	 * @return boolean
88
	 */
89 1
	public function extend($data, $name = 'openimporter_cookie')
90
	{
91 1
		$cookie = $this->get($name);
92
93 1
		if (!empty($data))
94
		{
95 1
			if ($cookie === false)
96
				$merged = $data;
97
			else
98 1
				$merged = array_merge((array) $cookie, (array) $data);
99
100 1
			return $this->set($merged, $name);
101
		}
102
103 1
		return false;
104
	}
105
}