Completed
Push — brickrouge3 ( 23d6f2...241af5 )
by Olivier
08:26
created

PermissionResolver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 15
c 5
b 0
f 1
lcom 1
cbo 0
dl 102
loc 102
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 4 4 1
A offsetExists() 4 4 1
A offsetGet() 4 4 1
A offsetSet() 4 4 1
A offsetUnset() 4 4 1
B synthesize_config() 27 27 4
A __construct() 7 7 2
B __invoke() 23 23 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Icybee\Modules\Users;
13
14 View Code Duplication
class PermissionResolver implements \ArrayAccess, \IteratorAggregate, PermissionResolverInterface
15
{
16
	/**
17
	 * Synthesizes the `users_permission_resolver_list` config from `users` fragments.
18
	 *
19
	 * @param array $fragments
20
	 *
21
	 * @return array
22
	 */
23
	static public function synthesize_config(array $fragments)
24
	{
25
		$list = [];
26
		$weight = [];
27
28
		foreach ($fragments as $fragment)
29
		{
30
			if (empty($fragment['permission_resolver_list']))
31
			{
32
				continue;
33
			}
34
35
			foreach ($fragment['permission_resolver_list'] as $resolver_id => $resolver)
36
			{
37
				$resolver = ((array) $resolver) + [ 'weight' => 0 ];
38
39
				$list[$resolver_id] = $resolver[0];
40
				$weight[$resolver_id] = $resolver['weight'];
41
			}
42
		}
43
44
		return \ICanBoogie\sort_by_weight($list, function($v, $k) use($weight) {
45
46
			return $weight[$k];
47
48
		});
49
	}
50
51
	private $list = [];
52
53
	/**
54
	 * Initializes the permission resolver list.
55
	 *
56
	 * @param array $resolver_list A permission resolver list, such as one created by
57
	 * {@link autoconfig()}.
58
	 */
59
	public function __construct(array $resolver_list = [])
60
	{
61
		foreach ($resolver_list as $resolver_id => $resolver)
62
		{
63
			$this[$resolver_id] = $resolver;
64
		}
65
	}
66
67
	public function __invoke(User $user, $permission, $target = null)
68
	{
69
		$granted = false;
70
71
		foreach ($this->list as $resolver_id => &$resolver)
72
		{
73
			if (!is_callable($resolver))
74
			{
75
				$resolver = new $resolver;
76
			}
77
78
			$resolver_grant = call_user_func($resolver, $user, $permission, $target);
79
80
			if ($resolver_grant === null)
81
			{
82
				continue;
83
			}
84
85
			$granted = $resolver_grant;
86
		}
87
88
		return $granted;
89
	}
90
91
	public function getIterator()
92
	{
93
		return new \ArrayIterator($this->list);
94
	}
95
96
	public function offsetExists($resolver_id)
97
	{
98
		return isset($this->list[$resolver_id]);
99
	}
100
101
	public function offsetGet($resolver_id)
102
	{
103
		return $this->list[$resolver_id];
104
	}
105
106
	public function offsetSet($resolver_id, $resolver)
107
	{
108
		$this->list[$resolver_id] = $resolver;
109
	}
110
111
	public function offsetUnset($resolver_id)
112
	{
113
		unset($this->list[$resolver_id]);
114
	}
115
}
116
117
interface PermissionResolverInterface
118
{
119
	/**
120
	 * Resolves the permission of a user.
121
	 *
122
	 * @param User $user A user record.
123
	 * @param string $permission The permission to check.
124
	 * @param mixed $target A context for the permission
125
	 *
126
	 * @return boolean `true` if the user has the specified permission.
127
	 */
128
	public function __invoke(User $user, $permission, $target = null);
129
}
130