Completed
Push — master ( 193422...5265e9 )
by Nazar
04:06
created

Data   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 56.52%

Importance

Changes 0
Metric Value
dl 0
loc 142
ccs 39
cts 69
cp 0.5652
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
C get_data() 0 66 15
B set_data() 0 31 6
A del_data() 0 18 4
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\User;
9
use
10
	cs\User;
11
12
/**
13
 * Trait that contains all methods for `cs\User` for working with user's data
14
 *
15
 * @property \cs\Cache\Prefix $cache
16
 * @property int              $id
17
 *
18
 * @method \cs\DB\_Abstract db()
19
 * @method \cs\DB\_Abstract db_prime()
20
 */
21
trait Data {
22
	/**
23
	 * Getting additional data item(s) of specified user
24
	 *
25
	 * @param string|string[] $item
26
	 * @param false|int       $user If not specified - current user assumed
27
	 *
28
	 * @return false|string|mixed[]
29
	 */
30 2
	function get_data ($item, $user = false) {
31 2
		$user = (int)$user ?: $this->id;
32 2
		if (!$item || $user == User::GUEST_ID) {
33
			return false;
34
		}
35 2
		$data = $this->cache->{"data/$user"} ?: [];
36 2
		if (is_array($item)) {
37
			$result = [];
38
			$absent = [];
39
			foreach ($item as $i) {
40
				if (isset($data[$i])) {
41
					$result[$i] = $data[$i];
42
				} else {
43
					$absent[] = $i;
44
				}
45
			}
46
			if ($absent) {
47
				$absent = implode(
48
					',',
49
					$this->db()->s($absent)
50
				);
51
				$absent = array_column(
52
					$this->db()->qfa(
53
						"SELECT `item`, `value`
54
						FROM `[prefix]users_data`
55
						WHERE
56
							`id`	= '$user' AND
57
							`item`	IN($absent)"
58
					),
59
					'value',
60
					'item'
61
				);
62
				foreach ($absent as &$a) {
63
					$a = _json_decode($a);
64
					if ($a === null) {
65
						$a = false;
66
					}
67
				}
68
				unset($a);
69
				$result += $absent;
70
				$data += $absent;
71
				$this->cache->{"data/$user"} = $data;
72
			}
73
			return $result;
74
		}
75 2
		if ($data === false || !isset($data[$item])) {
76 2
			if (!is_array($data)) {
77
				$data = [];
78
			}
79 2
			$data[$item] = _json_decode(
80 2
				$this->db()->qfs(
81
					"SELECT `value`
82
					FROM `[prefix]users_data`
83
					WHERE
84 2
						`id`	= '$user' AND
85 2
						`item`	= '%s'",
86
					$item
87
				)
88
			);
89 2
			if ($data[$item] === null) {
90
				$data[$item] = false;
91
			}
92 2
			$this->cache->{"data/$user"} = $data;
93
		}
94 2
		return $data[$item];
95
	}
96
	/**
97
	 * Setting additional data item(s) of specified user
98
	 *
99
	 * @param array|string $item Item-value array may be specified for setting several items at once
100
	 * @param mixed|null   $value
101
	 * @param false|int    $user If not specified - current user assumed
102
	 *
103
	 * @return bool
104
	 */
105 2
	function set_data ($item, $value = null, $user = false) {
106 2
		$user = (int)$user ?: $this->id;
107 2
		if (!$item || $user == User::GUEST_ID) {
108
			return false;
109
		}
110 2
		if (!is_array($item)) {
111
			$item = [
112 2
				$item => $value
113
			];
114
		}
115 2
		$params = [];
116 2
		foreach ($item as $i => $v) {
117 2
			$params[] = [$i, _json_encode($v)];
118
		}
119 2
		unset($i, $v);
120 2
		$result = $this->db_prime()->insert(
121
			"REPLACE INTO `[prefix]users_data`
122
				(
123
					`id`,
124
					`item`,
125
					`value`
126
				) VALUES (
127 2
					$user,
128
					'%s',
129
					'%s'
130 2
				)",
131
			$params
132
		);
133 2
		$this->cache->del("data/$user");
134 2
		return $result;
135
	}
136
	/**
137
	 * Deletion of additional data item(s) of specified user
138
	 *
139
	 * @param string|string[] $item
140
	 * @param false|int       $user If not specified - current user assumed
141
	 *
142
	 * @return bool
143
	 */
144 2
	function del_data ($item, $user = false) {
145 2
		$user = (int)$user ?: $this->id;
146 2
		if (!$item || $user == User::GUEST_ID) {
147
			return false;
148
		}
149 2
		$item   = implode(
150 2
			',',
151 2
			$this->db_prime()->s((array)$item)
152
		);
153 2
		$result = $this->db_prime()->q(
154
			"DELETE FROM `[prefix]users_data`
155
			WHERE
156 2
				`id`	= '$user' AND
157 2
				`item`	IN($item)"
158
		);
159 2
		$this->cache->del("data/$user");
160 2
		return (bool)$result;
161
	}
162
}
163