Passed
Push — master ( 655649...07d5b2 )
by Spuds
06:48 queued 05:03
created

Cookie   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 89
ccs 23
cts 24
cp 0.9583
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extend() 0 19 3
A get() 0 8 2
A set() 0 11 2
A __construct() 0 2 1
A destroy() 0 6 1
1
<?php
2
/**
3
 * @name      OpenImporter
4
 * @copyright OpenImporter contributors
5
 * @license   BSD https://opensource.org/licenses/BSD-3-Clause
6
 *
7
 * @version 1.0
8
 */
9
10
namespace OpenImporter;
11
12
/**
13
 * We need Cooooookies.
14
 *
15
 * @class Cookie
16
 */
17
class Cookie
18
{
19
	/**
20
	 * Cookie constructor.
21
	 */
22 4
	public function __construct()
23
	{
24 4
	}
25
26
	/**
27
	 * Set a cookie
28
	 *
29
	 * @param array $data
30
	 * @param string $name
31
	 *
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
			return unserialize($_COOKIE[$name], array('allowed_classes' => false));
59
		}
60
61 1
		return false;
62
	}
63
64
	/**
65
	 * Once we are done, we should destroy our cookie
66
	 *
67
	 * @param string $name
68
	 *
69
	 * @return boolean
70
	 */
71 1
	public function destroy($name = 'openimporter_cookie')
72
	{
73 1
		setcookie($name, '');
74 1
		unset($_COOKIE[$name]);
75
76 1
		return true;
77
	}
78
79
	/**
80
	 * Extend the cookie with new information
81
	 *
82
	 * @param array $data
83
	 * @param string $name
84
	 *
85
	 * @return boolean
86
	 */
87 1
	public function extend($data, $name = 'openimporter_cookie')
88
	{
89 1
		$cookie = $this->get($name);
90
91 1
		if (!empty($data))
92
		{
93 1
			if ($cookie !== false)
94
			{
95 1
				$merged = array_merge((array) $cookie, (array) $data);
96
			}
97
			else
98
			{
99
				$merged = $data;
100
			}
101
102 1
			return $this->set($merged, $name);
103
		}
104
105 1
		return false;
106
	}
107
}
108