View::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Fyuze\View;
3
4
use Exception;
5
use Fyuze\Error\ErrorHandler;
6
use InvalidArgumentException;
7
8
class View
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $view;
14
15
    /**
16
     * @var array
17
     */
18
    protected $params;
19
20
    /**
21
     * @param string $view
22
     * @param array $params
23
     * @throws InvalidArgumentException
24
     */
25 6
    public function __construct($view, array $params = [])
26
    {
27 6
        $this->params = $params;
28
29 6
        if (!file_exists($view)) {
30 1
            throw new InvalidArgumentException(sprintf('The view %s could not be found', $view));
31
        }
32
33 5
        $this->view = $view;
34
    }
35
36
    /**
37
     * @return string
38
     */
39 5
    public function render()
40
    {
41 5
        extract($this->params, EXTR_SKIP);
42
43 5
        ob_start();
44
45 5
        include $this->view;
46
47 4
        return ob_get_clean();
48
    }
49
50
    /**
51
     *
52
     */
53 3
    public function __toString()
54
    {
55
        try {
56 3
            return $this->render();
57 1
        } catch (Exception $e) {
58
59 1
            ob_end_clean();
60 1
            ob_start();
61 1
            $handler = new ErrorHandler();
62 1
            $handler->handle($e);
63 1
            return ob_get_clean();
64
        }
65
    }
66
}
67