Passed
Push — master ( b7467b...db04eb )
by Atanas
02:41
created

PostTemplate::satisfied()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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