Completed
Push — milestone/2_0/container-condit... ( af96ef...c41f81 )
by
unknown
03:01
created

Current_User_Capability_Condition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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 Current_User_Capability_Condition extends Condition {
11
12
	/**
13
	 * Check if a user has any of the supplied capabilities
14
	 * 
15
	 * @param  array<string> $capabilities
16
	 * @return boolean
17
	 */
18
	protected function current_user_can_any( $capabilities ) {
19
		foreach ( $capabilities as $cap ) {
20
			if ( current_user_can( $cap ) ) {
21
				return true;
22
			}
23
		}
24
		return false;
25
	}
26
	
27
	/**
28
	 * Check if the condition is fulfilled
29
	 * 
30
	 * @param  array $environment
31
	 * @return bool
32
	 */
33
	public function is_fulfilled( $environment ) {
34
		switch ( $this->get_comparison_operator() ) {
35
			case '=':
36
				return current_user_can( $this->get_value() );
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 '!=':
39
				return ! current_user_can( $this->get_value() );
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 'IN':
42
				return $this->current_user_can_any( $this->get_value() );
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
			case 'NOT IN':
45
				return ! $this->current_user_can_any( $this->get_value() );
46
				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...
47
		}
48
49
		return $this->first_supported_comparer_is_correct(
50
			get_current_user_id(),
51
			$this->get_comparison_operator(),
52
			$this->get_value()
53
		);
54
	}
55
}