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

PostTemplate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 0
cts 10
cp 0
wmc 6
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A satisfied() 0 5 3
A getArguments() 0 3 1
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