Completed
Push — milestone/2_0/container-condit... ( c41f81...3ee472 )
by
unknown
04:49
created

User_Role_Condition::get_user_roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
/**
6
 * Check if user has a specific role
7
 * 
8
 * Operator "CUSTOM" is passed an array of all user roles
9
 */
10
class User_Role_Condition extends Condition {
11
12
	/**
13
	 * Get roles for a user from the environment
14
	 * 
15
	 * @param  array         $environment
16
	 * @return array<string>
17
	 */
18
	protected function get_user_roles( $environment ) {
19
		return $environment['roles'];
20
	}
21
	
22
	/**
23
	 * Check if the condition is fulfilled
24
	 * 
25
	 * @param  array $environment
26
	 * @return bool
27
	 */
28
	public function is_fulfilled( $environment ) {
29
		$roles = $this->get_user_roles( $environment );
30
		
31
		switch ( $this->get_comparison_operator() ) {
32
			case '=':
33
				return in_array( $this->get_value(), $roles );
34
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
35
			case '!=':
36
				return ! in_array( $this->get_value(), $roles );
37
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
38
			case 'IN':
39
				return count( array_intersect( $roles, $this->get_value() ) ) > 0;
40
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
41
			case 'NOT IN':
42
				return count( array_intersect( $roles, $this->get_value() ) ) === 0;
43
				break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
44
		}
45
46
		return $this->first_supported_comparer_is_correct(
47
			$roles,
48
			$this->get_comparison_operator(),
49
			$this->get_value()
50
		);
51
	}
52
}