View   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
eloc 19
c 3
b 1
f 0
dl 0
loc 56
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 11 2
A __construct() 0 9 2
A render() 0 9 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