Passed
Push — master ( 904eb0...5301fa )
by Atanas
02:22
created

NameProxy   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 99
ccs 34
cts 34
cp 1
rs 10
c 1
b 0
f 0
wmc 12
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A exists() 0 5 1
A canonical() 0 5 1
A render() 0 11 3
A getDefaultBinding() 0 3 1
A getBindings() 0 3 1
A getBindingForFile() 0 12 3
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 NameProxy 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 1
		}
37 3
	}
38
39
	/**
40
	 * {@inheritDoc}
41
	 */
42 1
	public function exists( $view ) {
43 1
		$engine_key = $this->getBindingForFile( $view );
44 1
		$engine_instance = WPEmerge::resolve( $engine_key );
45 1
		return $engine_instance->exists( $view );
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 */
51 1
	public function canonical( $view ) {
52 1
		$engine_key = $this->getBindingForFile( $view );
53 1
		$engine_instance = WPEmerge::resolve( $engine_key );
54 1
		return $engine_instance->canonical( $view );
55
	}
56
57
	/**
58
	 * {@inheritDoc}
59
	 */
60 2
	public function render( $views, $context ) {
61 2
		foreach ( $views as $view ) {
62 2
			if ( $this->exists( $view ) ) {
63 1
				$engine_key = $this->getBindingForFile( $view );
64 1
				$engine_instance = WPEmerge::resolve( $engine_key );
65 1
				return $engine_instance->render( [$view], $context );
66
			}
67 1
		}
68
69 1
		return '';
70
	}
71
72
	/**
73
	 * Get the default binding
74
	 *
75
	 * @return string $binding
76
	 */
77 2
	public function getDefaultBinding() {
78 2
		return $this->default;
79
	}
80
81
	/**
82
	 * Get all bindings
83
	 *
84
	 * @return array  $bindings
85
	 */
86 1
	public function getBindings() {
87 1
		return $this->bindings;
88
	}
89
90
	/**
91
	 * Get the engine key binding for a specific file
92
	 *
93
	 * @param  string $file
94
	 * @return string
95
	 */
96 1
	public function getBindingForFile( $file ) {
97 1
		$engine_key = $this->default;
98
99 1
		foreach ( $this->bindings as $suffix => $engine ) {
100 1
			if ( substr( $file, -strlen( $suffix ) ) === $suffix ) {
101 1
				$engine_key = $engine;
102 1
				break;
103
			}
104 1
		}
105
106 1
		return $engine_key;
107
	}
108
}
109