Completed
Push — master ( a8898a...5c0dbc )
by Nazar
04:15
created

Data   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 127
ccs 0
cts 97
cp 0
rs 10
wmc 22
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
C get_data() 0 51 12
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
	function get_data ($item, $user = false) {
31
		$user = (int)$user ?: $this->id;
32
		if (!$item || $user == User::GUEST_ID) {
33
			return false;
34
		}
35
		if (is_array($item)) {
36
			$data   = $this->cache->get("data/$user") ?: [];
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->set("data/$user", $data);
72
			}
73
			return $result;
74
		}
75
		/**
76
		 * @var string $item
77
		 */
78
		$data = $this->get_data([$item], $user);
79
		return isset($data[$item]) ? $data[$item] : false;
80
	}
81
	/**
82
	 * Setting additional data item(s) of specified user
83
	 *
84
	 * @param array|string $item Item-value array may be specified for setting several items at once
85
	 * @param mixed|null   $value
86
	 * @param false|int    $user If not specified - current user assumed
87
	 *
88
	 * @return bool
89
	 */
90
	function set_data ($item, $value = null, $user = false) {
91
		$user = (int)$user ?: $this->id;
92
		if (!$item || $user == User::GUEST_ID) {
93
			return false;
94
		}
95
		if (!is_array($item)) {
96
			$item = [
97
				$item => $value
98
			];
99
		}
100
		$params = [];
101
		foreach ($item as $i => $v) {
102
			$params[] = [$i, _json_encode($v)];
103
		}
104
		unset($i, $v);
105
		$result = $this->db_prime()->insert(
106
			"REPLACE INTO `[prefix]users_data`
107
				(
108
					`id`,
109
					`item`,
110
					`value`
111
				) VALUES (
112
					$user,
113
					'%s',
114
					'%s'
115
				)",
116
			$params
117
		);
118
		$this->cache->del("data/$user");
119
		return $result;
120
	}
121
	/**
122
	 * Deletion of additional data item(s) of specified user
123
	 *
124
	 * @param string|string[] $item
125
	 * @param false|int       $user If not specified - current user assumed
126
	 *
127
	 * @return bool
128
	 */
129
	function del_data ($item, $user = false) {
130
		$user = (int)$user ?: $this->id;
131
		if (!$item || $user == User::GUEST_ID) {
132
			return false;
133
		}
134
		$item   = implode(
135
			',',
136
			$this->db_prime()->s((array)$item)
137
		);
138
		$result = $this->db_prime()->q(
139
			"DELETE FROM `[prefix]users_data`
140
			WHERE
141
				`id`	= '$user' AND
142
				`item`	IN($item)"
143
		);
144
		$this->cache->del("data/$user");
145
		return (bool)$result;
146
	}
147
}
148