Completed
Push — master ( 32eaad...6cdc53 )
by Nazar
04:10
created

Cookie   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 8
cts 11
cp 0.7272
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init_cookie() 0 15 4
A cookie() 0 3 1
1
<?php
2
/**
3
 * @package   CleverStyle Framework
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 12
	function init_cookie ($cookie = []) {
23 12
		$this->cookie = $cookie;
24
		/**
25
		 * Fill un-prefixed keys according to system configuration
26
		 */
27 12
		$prefix        = Config::instance()->core['cookie_prefix'];
28 12
		$prefix_length = strlen($prefix);
29 12
		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 12
	}
37
	/**
38
	 * Get cookie by name
39
	 *
40
	 * @param string $name
41
	 *
42
	 * @return null|string Cookie content if exists or `null` otherwise
43
	 */
44 16
	function cookie ($name) {
45 16
		return @$this->cookie[$name];
46
	}
47
}
48