View   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 75
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A render() 0 13 2
A getFileName() 0 4 1
A getPath() 0 4 1
1
<?php namespace PascalKleindienst\FormListGenerator\Support;
2
3
/**
4
 * Simple View class
5
 * @package \PascalKleindienst\FormListGenerator\Support
6
 */
7
class View
8
{
9
    /**
10
     * @var null|string
11
     */
12
    protected $path;
13
14
    /**
15
     * @var null|string
16
     */
17
    protected $extension;
18
19
    /**
20
     * Set params
21
     *
22
     * @param string $path
23
     * @param string $extension
24
     */
25 60
    public function __construct($path = '', $extension = '.phtml')
26
    {
27
        // default view path
28 60
        $this->path = realpath(__DIR__ . '/../views/');
29
30
        // custom view path
31 60
        if ($path !== '') {
32 21
            $this->path = str_replace('~', Config::get('root'), $path);
33 21
        }
34
        
35
        // Extension
36 60
        $this->extension = $extension;
37 60
    }
38
39
    /**
40
     * Load a view file
41
     *
42
     * @param string $file
43
     * @param array $data
44
     * @return void
45
     * @throws \InvalidArgumentException if the file could not be found
46
     */
47 21
    public function render($file, array $data = [])
48
    {
49
        // Check if file exists
50 21
        $fullpath = $this->getFileName($file);
51
        
52 21
        if (!file_exists($fullpath)) {
53 3
            throw new \InvalidArgumentException('Could not find file with name ' . $file);
54
        }
55
56
        // load file
57 18
        extract($data);
58 18
        include($fullpath);
59 18
    }
60
61
    /**
62
     * Get the full file name
63
     *
64
     * @param string $file
65
     * @return string
66
     */
67 21
    protected function getFileName($file)
68
    {
69 21
        return $this->path . '/_' . $file . $this->extension;
70
    }
71
72
    /**
73
     * Return path
74
     *
75
     * @return string
76
     */
77 9
    public function getPath()
78
    {
79 9
        return $this->path;
80
    }
81
}
82