Completed
Push — master ( d70e6c...215c97 )
by Nazar
04:09
created

Any::set_any_permissions()   C

Complexity

Conditions 12
Paths 74

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 12.0247

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 12
eloc 40
c 1
b 1
f 1
nc 74
nop 3
dl 0
loc 59
ccs 34
cts 36
cp 0.9444
crap 12.0247
rs 6.4485

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Permission;
9
use
10
	cs\Cache;
11
12
/**
13
 * Class Any with common methods for User and Group classes
14
 *
15
 * @property Cache $cache
16
 *
17
 * @method \cs\DB\_Abstract db()
18
 * @method \cs\DB\_Abstract db_prime()
19
 */
20
trait Any {
21
	/**
22
	 * @param int    $id
23
	 * @param string $type
24
	 *
25
	 * @return int[]|false
26
	 */
27 6
	protected function get_any_permissions ($id, $type) {
28 6
		$id = (int)$id;
29 6
		if (!$id) {
30
			return false;
31
		}
32
		switch ($type) {
33 6
			case 'user':
34 4
				$table = '[prefix]users_permissions';
35 4
				break;
36 4
			case 'group':
37 4
				$table = '[prefix]groups_permissions';
38 4
				break;
39
			default:
40
				return false;
41
		}
42 6
		return $this->cache->get(
43 6
			"permissions/$id",
44 6
			function () use ($id, $table) {
45
				$permissions       = false;
46
				$permissions_array = $this->db()->qfa(
47
					"SELECT
48
						`permission`,
49
						`value`
50
					FROM `$table`
51
					WHERE `id` = '$id'
52
					ORDER BY `permission` ASC"
53
				);
54
				if (is_array($permissions_array)) {
55
					$permissions = [];
56
					foreach ($permissions_array as $permission) {
57
						$permissions[$permission['permission']] = (int)(bool)$permission['value'];
58
					}
59
				}
60
				return $permissions;
61 6
			}
62
		);
63
	}
64
	/**
65
	 * @param array  $data
66
	 * @param int    $id
67
	 * @param string $type
68
	 *
69
	 * @return bool
70
	 */
71 4
	protected function set_any_permissions ($data, $id, $type) {
72 4
		$id = (int)$id;
73 4
		if (!is_array($data) || empty($data) || !$id) {
74
			return false;
75
		}
76
		switch ($type) {
77 4
			case 'user':
78 2
				$table = '[prefix]users_permissions';
79 2
				break;
80 4
			case 'group':
81 4
				$table = '[prefix]groups_permissions';
82 4
				break;
83
			default:
84
				return false;
85
		}
86 4
		$insert_update = [];
87 4
		$delete        = [];
88 4
		foreach ($data as $permission => $value) {
89 4
			if ($value == -1) {
90 4
				$delete[] = (int)$permission;
91
			} else {
92 4
				$insert_update[] = [$permission, (int)(bool)$value];
93
			}
94
		}
95 4
		unset($permission, $value);
96 4
		$return = true;
97 4
		if ($delete) {
98 4
			$delete = implode(', ', $delete);
99 4
			$return = (bool)$this->db_prime()->q(
100 4
				"DELETE FROM `$table`
101
				WHERE
102 4
					`id`			= '$id' AND
103 4
					`permission`	IN ($delete)"
104
			);
105
		}
106 4
		unset($delete);
107 4
		if ($insert_update) {
108
			$return =
109 4
				$return &&
110 4
				$this->db_prime()->insert(
111 4
					"REPLACE INTO `$table`
112
						(
113
							`id`,
114
							`permission`,
115
							`value`
116
						) VALUES (
117 4
							'$id',
118
							'%d',
119
							'%d'
120 4
						)",
121
					$insert_update
122
				);
123
		}
124 4
		$this->cache->del("permissions/$id");
125 4
		if ($type == 'group') {
126 4
			Cache::instance()->del('users/permissions');
127
		}
128 4
		return (bool)$return;
129
	}
130
	/**
131
	 * @param int    $id
132
	 * @param string $type
133
	 *
134
	 * @return bool
135
	 */
136 8
	protected function del_any_permissions_all ($id, $type) {
137 8
		$id = (int)$id;
138 8
		if (!$id) {
139
			return false;
140
		}
141
		switch ($type) {
142 8
			case 'user':
143 6
				$table = '[prefix]users_permissions';
144 6
				break;
145 2
			case 'group':
146 2
				$table = '[prefix]groups_permissions';
147 2
				break;
148
			default:
149
				return false;
150
		}
151 8
		$return = $this->db_prime()->q(
152 8
			"DELETE FROM `$table`
153 8
			WHERE `id` = '$id'"
154
		);
155 8
		$this->cache->del("permissions/$id");
156 8
		if ($type == 'group') {
157 2
			Cache::instance()->del('users/permissions');
158
		}
159 8
		return (bool)$return;
160
	}
161
}
162