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

User_Capability_Condition::user_can_any()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
/**
6
 * Check if user has a specific capability
7
 * 
8
 * Operator "CUSTOM" is passed the user id
9
 */
10
class User_Capability_Condition extends Condition {
11
12
	/**
13
	 * Get user id from environment
14
	 * 
15
	 * @param  array         $environment
16
	 * @return array<string>
17
	 */
18
	protected function get_user_id( $environment ) {
19
		return $environment['user_id'];
20
	}
21
22
	/**
23
	 * Check if a user has any of the supplied capabilities
24
	 * 
25
	 * @param  array<string> $capabilities
26
	 * @return boolean
27
	 */
28
	protected function user_can_any( $user_id, $capabilities ) {
29
		foreach ( $capabilities as $cap ) {
30
			if ( user_can( $user_id, $cap ) ) {
31
				return true;
32
			}
33
		}
34
		return false;
35
	}
36
	
37
	/**
38
	 * Check if the condition is fulfilled
39
	 * 
40
	 * @param  array $environment
41
	 * @return bool
42
	 */
43
	public function is_fulfilled( $environment ) {
44
		$user_id = $this->get_user_id( $environment );
45
		switch ( $this->get_comparison_operator() ) {
46
			case '=':
47
				return user_can( $user_id, $this->get_value() );
48
				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...
49
			case '!=':
50
				return ! user_can( $user_id, $this->get_value() );
51
				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...
52
			case 'IN':
53
				return $this->user_can_any( $user_id, $this->get_value() );
54
				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...
55
			case 'NOT IN':
56
				return ! $this->user_can_any( $user_id, $this->get_value() );
57
				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...
58
		}
59
60
		return $this->first_supported_comparer_is_correct(
61
			$user_id,
62
			$this->get_comparison_operator(),
63
			$this->get_value()
64
		);
65
	}
66
}