Completed
Push — dev-master ( 966d5f...958bb4 )
by Derek Stephen
05:48
created

Controller::getViewEngine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Bone\Mvc;
4
5
use Bone\Db\Adapter\MySQL;
6
use Bone\Mvc\View\ViewEngine;
7
use Bone\Mvc\View\PlatesEngine;
8
use PDO;
9
use Psr\Http\Message\RequestInterface;;
10
use stdClass;
11
12
class Controller
13
{
14
    /** @var RequestInterface */
15
    protected $request;
16
17
    /** @var ViewEngine $plates */
18
    protected $viewEngine;
19
20
    /** @var string $controller */
21
    protected $controller;
22
23
    /** @var string $action  */
24
    protected $action;
25
26
    /** @var stdClass $view */
27
    public $view;
28
29
    /** @var string $body */
30
    private $body;
31
32
    /** @var bool */
33
    private $layoutEnabled;
34
35
    /** @var bool */
36
    private $viewEnabled;
37
38
39
40
    /**
41
     * @var \Bone\Db\Adapter\MySQL
42
     */
43
    protected $_db;
44
45 19
    public function __construct(RequestInterface $request)
46 19
    {
47 19
        $this->request = $request;
48 19
        $this->params = (object) $this->request->getQueryParams();
0 ignored issues
show
Bug introduced by
The property params does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
50 19
        $this->initViewEngine();
51 19
        $this->view = new stdClass();
52 19
        $this->layoutEnabled = true;
53 19
        $this->viewEnabled = true;
54 19
    }
55
56
    /**
57
     * @return void
58
     */
59 1
    protected function setDB()
60 1
    {
61 1
        $config = Registry::ahoy()->get('db');
62 1
        $this->_db = new MySQL($config);
63 1
    }
64
65
    /**
66
     * @return void
67
     */
68 19
    protected function initViewEngine()
69 19
    {
70 19
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
71 19
        $engine = new PlatesEngine($viewPath);
72 19
        $this->viewEngine = $engine;
73 19
    }
74
75
    /**
76
     * @return PDO
77
     */
78 1
    public function getDbAdapter()
79 1
    {
80 1
        if(!$this->_db)
81
        {
82 1
            $this->setDB();
83
        }
84 1
        return $this->_db->getConnection();
85
    }
86
87
    /**
88
     * @return ViewEngine
89
     */
90 4
    public function getViewEngine()
91 4
    {
92 4
        return $this->viewEngine;
93
    }
94
95
96
    /**
97
     *  runs before th' controller action
98
     */
99 3
    public function init()
100 3
    {
101
        // extend this t' initialise th' controller
102 3
    }
103
104 2
    public function getParams()
105 2
    {
106 2
        return (object) $this->params;
107
    }
108
109
    /**
110
     * @param $param
111
     * @return mixed
112
     */
113 1
    public function getParam($param)
114 1
    {
115 1
        $params = $this->getParams();
116 1
        return isset($params->$param) ? $params->$param : null;
117
    }
118
119
    /**
120
     * @param $key
121
     * @param $val
122
     * @return $this
123
     */
124 12
    public function setParam($key, $val)
125 12
    {
126 12
        $this->params->$key = $val;
127 12
        return $this;
128
    }
129
130
    /**
131
     *  runs after yer work is done
132
     */
133 3
    public function postDispatch()
134 3
    {
135
        // extend this t' run code after yer controller is finished
136 3
    }
137
138
    /**
139
     *  For loadin' th' cannon, so t' speak
140
     *
141
     * @return array
142
     */
143 2
    public function getHeaders()
144 2
    {
145 2
        return $this->request->getHeaders();
146
    }
147
148 3
    public function hasLayoutEnabled()
149 3
    {
150 3
        return ($this->layoutEnabled === true);
151
    }
152
153 1
    public function enableLayout()
154 1
    {
155 1
        $this->layoutEnabled = true;
156 1
    }
157
158 3
    public function disableLayout()
159 3
    {
160 3
        $this->layoutEnabled = false;
161 3
    }
162
163 4
    public function hasViewEnabled()
164 4
    {
165 4
        return ($this->viewEnabled === true);
166
    }
167
168 1
    public function enableView()
169 1
    {
170 1
        $this->viewEnabled = true;
171 1
    }
172
173 3
    public function disableView()
174 3
    {
175 3
        $this->viewEnabled = false;
176 3
    }
177
178
    /**
179
     * @return string
180
     */
181 4
    public function getBody()
182 4
    {
183 4
        return $this->body;
184
    }
185
186
    /**
187
     *  Only used if Layout & View disabled
188
     *
189
     * @param $body
190
     */
191 1
    public function setBody($body)
192 1
    {
193 1
        $this->body = $body;
194 1
    }
195
196 1
    private function errorAction()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
197 1
    {
198 1
        $this->disableView();
199 1
        $this->disableLayout();
200 1
        $this->body = '500 Page Error.';
201 1
    }
202
203 1
    private function notFoundAction()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
204 1
    {
205 1
        $this->disableView();
206 1
        $this->disableLayout();
207 1
        $this->body = '404 Page Not Found.';
208
    }
209
}