Passed
Push — master ( 774cb9...35b872 )
by Atanas
05:13
created

PhpView::getName()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace WPEmerge\View;
4
5
use Exception;
6
use GuzzleHttp\Psr7;
7
use GuzzleHttp\Psr7\Response;
8
use WPEmerge\Facades\View;
9
10
/**
11
 * Render a view file with php.
12
 */
13
class PhpView implements ViewInterface {
14
	use HasNameTrait, HasContextTrait;
15
16
	/**
17
	 * Filepath to view.
18
	 *
19
	 * @var string
20
	 */
21
	protected $filepath = '';
22
23
	/**
24
	 * Get filepath.
25
	 *
26
	 * @return string
27
	 */
28 1
	public function getFilepath() {
29 1
		return $this->filepath;
30
	}
31
32
	/**
33
	 * Set filepath.
34
	 *
35
	 * @param  string $filepath
36
	 * @return self   $this
37
	 */
38 1
	public function setFilepath( $filepath ) {
39 1
		$this->filepath = $filepath;
40 1
		return $this;
41
	}
42
43
	/**
44
	 * {@inheritDoc}
45
	 */
46 7
	public function toString() {
47 7
		if ( empty( $this->getName() ) ) {
48 1
			throw new Exception( 'View must have a name.' );
49
		}
50
51 6
		if ( empty( $this->getFilepath() ) ) {
52 1
			throw new Exception( 'View must have a filepath.' );
53
		}
54
55 5
		$context = $this->getContext();
56 5
		$this->with( ['global' => View::getGlobals()] );
57 5
		View::compose( $this );
58 5
		$this->with( $context );
59
60 5
		$renderer = function() {
61 5
			ob_start();
62 5
			$__context = $this->getContext();
63 5
			extract( $__context );
64 5
			include( $this->getFilepath() );
65 5
			return ob_get_clean();
66 5
		};
67
68 5
		return $renderer();
69
	}
70
71
	/**
72
	 * {@inheritDoc}
73
	 */
74 1
	public function toResponse() {
75 1
		return (new Response())
76 1
			->withHeader( 'Content-Type', 'text/html' )
77 1
			->withBody( Psr7\stream_for( $this->toString() ) );
78
	}
79
}
80