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

PhpView   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 67
ccs 26
cts 26
cp 1
rs 10
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilepath() 0 3 1
A setFilepath() 0 4 1
B toString() 0 24 3
A toResponse() 0 5 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