Passed
Push — master ( d2008b...aa9f10 )
by Atanas
01:54
created

PostTemplateCondition::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Routing\Conditions;
4
5
use WPEmerge\Requests\Request;
6
7
/**
8
 * Check against the current post's template
9
 *
10
 * @codeCoverageIgnore
11
 */
12
class PostTemplateCondition implements ConditionInterface {
13
	/**
14
	 * Post template to check against
15
	 *
16
	 * @var string
17
	 */
18
	protected $post_template = '';
19
20
	/**
21
	 * Post types to check against
22
	 *
23
	 * @var string[]
24
	 */
25
	protected $post_types = [];
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param string          $post_template
31
	 * @param string|string[] $post_types
32
	 */
33
	public function __construct( $post_template, $post_types = [] ) {
34
		$this->post_template = $post_template;
35
		$this->post_types = is_array( $post_types ) ? $post_types : [$post_types];
36
	}
37
38
	/**
39
	 * {@inheritDoc}
40
	 */
41
	public function isSatisfied( Request $request ) {
42
		$template = get_post_meta( get_the_ID(), '_wp_page_template', true );
43
		$template = $template ? $template : 'default';
44
		return ( is_singular( $this->post_types ) && $this->post_template === $template );
45
	}
46
47
	/**
48
	 * {@inheritDoc}
49
	 */
50
	public function getArguments( Request $request ) {
51
		return [$this->post_template, $this->post_types];
52
	}
53
}
54