Passed
Push — master ( 7dac2b...30127e )
by Atanas
01:55
created

FilenameProxy::getDefaultBinding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace WPEmerge\View;
4
5
use WPEmerge;
6
7
/**
8
 * Render view files with different engines depending on their filename
9
 */
10
class FilenameProxy implements \WPEmerge\View\EngineInterface {
11
	/**
12
	 * Container key of default engine to use
13
	 *
14
	 * @var string
15
	 */
16
	protected $default = WPEMERGE_VIEW_ENGINE_PHP_KEY;
17
18
	/**
19
	 * Array of filename_suffix=>engine_container_key bindings
20
	 *
21
	 * @var array
22
	 */
23
	protected $bindings = [];
24
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param array  $bindings
29
	 * @param string $default
30
	 */
31 3
	public function __construct( $bindings, $default = '' ) {
32 3
		$this->bindings = $bindings;
33
34 3
		if ( ! empty( $default ) ) {
35 1
			$this->default = $default;
36
		}
37 3
	}
38
39
	/**
40
	 * Get the default binding
41
	 *
42
	 * @return string $binding
43
	 */
44 2
	public function getDefaultBinding() {
45 2
		return $this->default;
46
	}
47
48
	/**
49
	 * Get all bindings
50
	 *
51
	 * @return array  $bindings
52
	 */
53 1
	public function getBindings() {
54 1
		return $this->bindings;
55
	}
56
57
	/**
58
	 * Get the engine key binding for a specific file
59
	 *
60
	 * @param  string $file
61
	 * @return string
62
	 */
63 1
	public function getBindingForFile( $file ) {
64 1
		$engine_key = $this->default;
65
66 1
		foreach ( $this->bindings as $suffix => $engine ) {
67 1
			if ( substr( $file, -strlen( $suffix ) ) === $suffix ) {
68 1
				$engine_key = $engine;
69 1
				break;
70
			}
71
		}
72
73 1
		return $engine_key;
74
	}
75
76
	/**
77
	 * {@inheritDoc}
78
	 */
79 1
	public function render( $file, $context ) {
80 1
		$engine_key = $this->getBindingForFile( $file );
81 1
		$engine_instance = WPEmerge::resolve( $engine_key );
82 1
		return $engine_instance->render( $file, $context );
83
	}
84
}
85