Completed
Push — master ( 5686ea...407817 )
by Nazar
05:05
created

Cookie   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init_cookie() 0 18 5
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 44
	public function init_cookie ($cookie = []) {
23 44
		$this->cookie = $cookie;
24 44
		if (!$cookie) {
25 44
			return;
26
		}
27
		/**
28
		 * Fill un-prefixed keys according to system configuration
29
		 */
30 16
		$prefix        = Config::instance()->core['cookie_prefix'];
31 16
		$prefix_length = strlen($prefix);
32 16
		if ($prefix_length) {
33 2
			foreach ($cookie as $key => $value) {
34 2
				if (strpos($key, $prefix) === 0) {
35 2
					$this->cookie[substr($key, $prefix_length)] = $value;
36
				}
37
			}
38
		}
39 16
	}
40
	/**
41
	 * Get cookie by name
42
	 *
43
	 * @param string $name
44
	 *
45
	 * @return null|string Cookie content if exists or `null` otherwise
46
	 */
47 50
	public function cookie ($name) {
48 50
		return @$this->cookie[$name];
49
	}
50
}
51