Passed
Branch refactor/route-groups (411840)
by Atanas
01:44
created

MultipleCondition::isSatisfied()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Routing\Conditions;
11
12
use WPEmerge\Facades\RouteCondition;
13
use WPEmerge\Requests\RequestInterface;
14
15
/**
16
 * Check against an array of conditions in an AND logical relationship.
17
 */
18
class MultipleCondition implements ConditionInterface, HasUrlInterface {
19
	/**
20
	 * Array of conditions to check.
21
	 *
22
	 * @var array<ConditionInterface>
23
	 */
24
	protected $conditions = [];
25
26
	/**
27
	 * Constructor.
28
	 *
29
	 * @param array $conditions
30
	 */
31
	public function __construct( $conditions ) {
32 1
		$this->conditions = array_map( function ( $condition ) {
33 1
			if ( $condition instanceof ConditionInterface ) {
34 1
				return $condition;
35
			}
36 1
			return RouteCondition::make( $condition );
37 1
		}, $conditions );
38 1
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43 1
	public function isSatisfied( RequestInterface $request ) {
44 1
		foreach ( $this->conditions as $condition ) {
45 1
			if ( ! $condition->isSatisfied( $request ) ) {
46 1
				return false;
47
			}
48
		}
49 1
		return true;
50
	}
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55 1
	public function getArguments( RequestInterface $request ) {
56 1
		$arguments = [];
57
58 1
		foreach ( $this->conditions as $condition ) {
59 1
			$arguments = array_merge( $arguments, $condition->getArguments( $request ) );
60
		}
61
62 1
		return $arguments;
63
	}
64
65
	/**
66
	 * Get all assigned conditions
67
	 *
68
	 * @return \WPEmerge\Routing\Conditions\ConditionInterface[]
69
	 */
70 1
	public function getConditions() {
71 1
		return $this->conditions;
72
	}
73
74
	/**
75
	 * {@inheritDoc}
76
	 * @codeCoverageIgnore
77
	 */
78
	public function getUrlWhere() {
79
		return [];
80
	}
81
82
	/**
83
	 * {@inheritDoc}
84
	 * @codeCoverageIgnore
85
	 */
86
	public function setUrlWhere( $where ) {
87
		foreach ( $this->conditions as $condition ) {
88
			if ( $condition instanceof HasUrlInterface ) {
89
				$condition->setUrlWhere( $where );
90
			}
91
		}
92
	}
93
}
94