Test Failed
Push — master ( 8adf73...89961a )
by Darío
02:22
created

View::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
/**
14
 * View class
15
 *
16
 * This class manages views and parameters
17
 */
18
class View
19
{
20
    use \Drone\Util\ParamTrait;
21
22
    /**
23
     * View name
24
     *
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * The path where views are located.
31
     *
32
     * @var string
33
     */
34
    protected $path;
35
36
    /**
37
     * Returns the view name
38
     *
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return $this->name;
44
    }
45
46
    /**
47
     * Returns the path
48
     *
49
     * @return string
50
     */
51
    public function getPath()
52
    {
53
        return $this->path;
54
    }
55
56
    /**
57
     * Sets the view name
58
     *
59
     * @param string
60
     *
61
     * @return null
62
     */
63
    public function setName($name)
64
    {
65
        $this->name = $name;
66
    }
67
68
    /**
69
     * Sets the path
70
     *
71
     * @param string
72
     *
73
     * @return string
74
     */
75 1
    public function setPath($path)
76
    {
77 1
        $this->path = $path;
78 1
    }
79
80
    /**
81
     * Constructor
82
     *
83
     * @param string $name
84
     * @param array  $parameters
85
     */
86 1
    public function __construct($name, Array $parameters = [])
87
    {
88 1
        $this->name  = $name;
89 1
        $this->setParams($parameters);
90 1
    }
91
92
    /**
93
     * Get the contents of the view
94
     *
95
     * @throws Exception\ViewNotFoundException
96
     *
97
     * @return  null
98
     */
99 1
    public function getContents()
100
    {
101 1
        $contents = file_get_contents($_view);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_view seems to be never defined.
Loading history...
102
103 1
        if ($contents === false) {
104
            throw new Exception\ViewGetContentsErrorException("The view '" .$this->name. "' does not exists");
105
        }
106
107 1
        return $contents;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $contents returns the type string which is incompatible with the documented return type null.
Loading history...
108
    }
109 1
110
    /**
111
     * Loads the view
112
     *
113 1
     * @return  null
114
     */
115
    public function render()
116
    {
117
        if (count($this->getParams())) {
118
            extract($this->getParams(), EXTR_SKIP);
119
        }
120
121
        include $this->getFile();
122
    }
123
124
    /**
125
     * Gets the file path
126
     *
127
     * @return  string
128
     */
129
    private function getFile()
130
    {
131
        $_view = $this->path . DIRECTORY_SEPARATOR . $this->name . '.phtml';
132
133
        if (!file_exists($_view)) {
134
            throw new Exception\ViewNotFoundException("The view '" .$this->name. "' does not exists");
135
        }
136
137
        return $_view;
138
    }
139
}
140