Completed
Push — develop ( 137ae5...0abda6 )
by Michael
04:57
created

View::get()   B

Complexity

Conditions 5
Paths 17

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 13
cts 15
cp 0.8667
rs 8.439
cc 5
eloc 16
nc 17
nop 2
crap 5.0592
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 */
9
10
namespace miBadger\Mvc;
11
12
use miBadger\Singleton\SingletonTrait;
13
14
/**
15
 * The view class of the MVC pattern.
16
 *
17
 * @see http://en.wikipedia.org/wiki/Model-view-controller
18
 * @since 1.0.0
19
 */
20
class View
21
{
22
	use SingletonTrait;
23
24
	const DIRECTORY_SEPARATOR = \DIRECTORY_SEPARATOR;
25
26
	/** @var string the base path. */
27
	private $basePath;
28
29
	/**
30
	 * Construct a object.
31
	 */
32 4
	protected function __construct()
33
	{
34 4
		$this->basePath = null;
35 4
	}
36
37
	/**
38
	 * Returns the base path.
39
	 *
40
	 * @return string the base path.
41
	 */
42 2
	public static function getBasePath()
43
	{
44 2
		return static::getInstance()->basePath;
45
	}
46
47
	/**
48
	 * Set the base path.
49
	 *
50
	 * @param string $basePath = null
0 ignored issues
show
Documentation introduced by
Should the type for parameter $basePath not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
51
	 */
52 2
	public static function setBasePath($basePath = null)
53
	{
54 2
		static::getInstance()->basePath = $basePath;
55 2
	}
56
57
	/**
58
	 * Returns the view at the given path with the given data.
59
	 *
60
	 * @param string $path
61
	 * @param string[] $data = []
62
	 * @return string a string representation of the view.
63
	 */
64 2
	public static function get($path, $data = [])
65
	{
66 2
		ob_start();
67
68 2
		extract($data);
69
70
		try {
71 2
			$basePath = static::getInstance()->basePath;
72
73 2
			if ($basePath) {
74 1
				if (mb_substr($path, 0, 1) === static::DIRECTORY_SEPARATOR) {
75
					$this->$path = mb_substr($path, 1);
76
				}
77
78 1
				if (mb_substr($basePath, -1) === static::DIRECTORY_SEPARATOR) {
79
					$this->$basePath = mb_substr($basePath, 0, -1);
80
				}
81
82 1
				$path = $basePath . static::DIRECTORY_SEPARATOR . $path;
83
			}
84
85 2
			include $path;
86 1
		} catch (\Exception $e) {
87 1
			ob_get_clean();
88 1
			throw $e;
89
		}
90
91 1
		return ob_get_clean();
92
	}
93
}
94