Completed
Push — master ( 12b05e...7485cd )
by Nazar
04:15
created

Cookie   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 36
rs 10
c 4
b 0
f 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init_cookie() 0 15 4
A cookie() 0 3 2
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Request;
9
use
10
	cs\Config;
11
12
trait Cookie {
13
	/**
14
	 * Cookie array, similar to `$_COOKIE`, but also contains un-prefixed keys according to system configuration
15
	 *
16
	 * @var array
17
	 */
18
	public $cookie;
19
	/**
20
	 * @param array $cookie Typically `$_COOKIE`
21
	 */
22
	function init_cookie ($cookie = []) {
23
		$this->cookie = $cookie;
24
		/**
25
		 * Fill un-prefixed keys according to system configuration
26
		 */
27
		$prefix        = Config::instance()->core['cookie_prefix'];
28
		$prefix_length = strlen($prefix);
29
		if ($prefix_length) {
30
			foreach ($cookie as $key => $value) {
31
				if (strpos($key, $prefix) === 0) {
32
					$this->cookie[substr($key, $prefix_length)] = $value;
33
				}
34
			}
35
		}
36
	}
37
	/**
38
	 * Get cookie by name
39
	 *
40
	 * @param string $name
41
	 *
42
	 * @return false|string Cookie content if exists or `false` otherwise
43
	 */
44
	function cookie ($name) {
45
		return isset($this->cookie[$name]) ? $this->cookie[$name] : false;
46
	}
47
}
48