Completed
Push — master ( d1a069...108e08 )
by Derek Stephen
02:05
created

Controller::setViewEngine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
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\ServerRequestInterface;
10
use stdClass;
11
use Zend\Diactoros\Response\TextResponse;
12
13
class Controller
14
{
15
    /** @var ServerRequestInterface */
16
    protected $request;
17
18
    /** @var ViewEngine $plates */
19
    protected $viewEngine;
20
21
    /** @var string $controller */
22
    protected $controller;
23
24
    /** @var string $action  */
25
    protected $action;
26
27
    /** @var stdClass $view */
28
    public $view;
29
30
    /** @var string $body */
31
    private $body;
32
33
    /** @var bool */
34
    private $layoutEnabled;
35
36
    /** @var bool */
37
    private $viewEnabled;
38
39
    /** @var array $headers */
40
    private $headers;
41
42
    /** @var int $statusCode */
43
    private $statusCode = 200;
44
45
    /** @var object $params */
46
    public $params;
47
48
    /** @var array $post */
49
    protected $post = [];
50
51
    /** @var MySQL */
52
    protected $_db;
53
54
    /**
55
     * Controller constructor.
56
     * @param ServerRequestInterface $request
57
     */
58 26
    public function __construct(ServerRequestInterface $request)
59 26
    {
60 26
        $this->request = $request;
61 26
        $this->headers = [];
62 26
        $this->params = (object) $this->request->getQueryParams();
63
64 26
        if ($this->request->getMethod() == 'POST') {
65
            $this->post = $this->request->getParsedBody();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->request->getParsedBody() can also be of type null or object. However, the property $post is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66
        }
67
68 26
        $this->initViewEngine();
69 26
        $this->view = new stdClass();
70 26
        $this->layoutEnabled = true;
71 26
        $this->viewEnabled = true;
72 26
    }
73
74
    /**
75
     * @return void
76
     */
77 1
    protected function setDB()
78 1
    {
79 1
        $config = Registry::ahoy()->get('db');
80 1
        $this->_db = new MySQL($config);
81 1
    }
82
83
    /**
84
     * @return void
85
     */
86 26
    protected function initViewEngine()
87 26
    {
88 26
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
89 26
        $engine = new PlatesEngine($viewPath);
90 26
        $this->viewEngine = $engine;
91 26
    }
92
93
    public function setViewEngine(ViewEngine $engine)
94
    {
95
        $this->viewEngine = $engine;
96
    }
97
98
    /**
99
     * @return PDO
100
     */
101 1
    public function getDbAdapter()
102 1
    {
103 1
        if(!$this->_db)
104
        {
105 1
            $this->setDB();
106
        }
107 1
        return $this->_db->getConnection();
108
    }
109
110
    /**
111
     * @return ViewEngine
112
     */
113 4
    public function getViewEngine()
114 4
    {
115 4
        return $this->viewEngine;
116
    }
117
118
119
    /**
120
     *  runs before th' controller action
121
     */
122 17
    public function init()
123 17
    {
124
        // extend this t' initialise th' controller
125 17
    }
126
127 1
    public function getParams()
128 1
    {
129 1
        return array_merge((array) $this->params, $this->post);
130
    }
131
132
    /**
133
     * @param $param
134
     * @return mixed
135
     */
136 1
    public function getParam($param, $default = null)
137 1
    {
138 1
        return isset($this->params->$param) ? urldecode($this->params->$param) : $default;
139
    }
140
141
    /**
142
     * @param $key
143
     * @param $val
144
     * @return $this
145
     */
146 15
    public function setParam($key, $val)
147 15
    {
148 15
        $this->params->$key = $val;
149 15
        return $this;
150
    }
151
152
    /**
153
     *  runs after yer work is done
154
     */
155 17
    public function postDispatch()
156 17
    {
157
        // extend this t' run code after yer controller is finished
158 17
    }
159
160
    /**
161
     *  For loadin' th' cannon, so t' speak
162
     *
163
     * @return array
164
     */
165 6
    public function getHeaders()
166 6
    {
167 6
        return $this->headers;
168
    }
169
170
    /**
171
     * @return bool
172
     */
173 4
    public function hasLayoutEnabled()
174 4
    {
175 4
        return ($this->layoutEnabled === true);
176
    }
177
178
    /**
179
     * Enables the layout
180
     */
181 1
    public function enableLayout()
182 1
    {
183 1
        $this->layoutEnabled = true;
184 1
    }
185
186
    /**
187
     * Disables the layout
188
     */
189 5
    public function disableLayout()
190 5
    {
191 5
        $this->layoutEnabled = false;
192 5
    }
193
194
    /**
195
     * @return bool
196
     */
197 4
    public function hasViewEnabled()
198 4
    {
199 4
        return ($this->viewEnabled === true);
200
    }
201
202
    /**
203
     * Enables the view
204
     */
205 1
    public function enableView()
206 1
    {
207 1
        $this->viewEnabled = true;
208 1
    }
209
210
    /**
211
     * Disables the view
212
     */
213 5
    public function disableView()
214 5
    {
215 5
        $this->viewEnabled = false;
216 5
    }
217
218
    /**
219
     * @return string
220
     */
221 5
    public function getBody()
222 5
    {
223 5
        return $this->body;
224
    }
225
226
    /**
227
     *  Only used if Layout & View disabled
228
     *
229
     * @param $body
230
     */
231 2
    public function setBody($body)
232 2
    {
233 2
        $this->body = $body;
234 2
    }
235
236
    /**
237
     * @return array
238
     */
239 1
    public function indexAction()
240 1
    {
241 1
        return ['message' => 'Override this method'];
242
    }
243
244 2
    public function errorAction()
245 2
    {
246 2
        $this->disableView();
247 2
        $this->disableLayout();
248 2
        $this->body = '500 Page Error.';
249 2
        return new TextResponse($this->body, 500);
250
    }
251
252 1
    public function notFoundAction()
253 1
    {
254 1
        $this->disableView();
255 1
        $this->disableLayout();
256 1
        $this->body = '404 Page Not Found.';
257 1
    }
258
259
    /**
260
     * @return ServerRequestInterface
261
     */
262 1
    public function getRequest()
263 1
    {
264 1
        return $this->request;
265
    }
266
267
    /**
268
     * @param ServerRequestInterface $request
269
     * @return Controller
270
     */
271 1
    public function setRequest(ServerRequestInterface $request)
272 1
    {
273 1
        $this->request = $request;
274 1
        return $this;
275
    }
276
277
    /**
278
     * @param string $key
279
     * @param string $value
280
     * @return $this
281
     */
282 3
    public function setHeader($key, $value)
283 3
    {
284 3
        $this->headers[$key] = $value;
285 3
        return $this;
286
    }
287
288
    /**
289
     * @param array $headers
290
     */
291 2
    public function setHeaders(array $headers)
292 2
    {
293 2
        $this->headers = $headers;
294 2
    }
295
296
    /**
297
     * @param int $statusCode
298
     */
299 2
    public function setStatusCode($statusCode)
300 2
    {
301 2
        $this->statusCode = $statusCode;
302 2
    }
303
304
    /**
305
     * @return int
306
     */
307 4
    public function getStatusCode()
308 4
    {
309 4
        return $this->statusCode;
310
    }
311
312
313
314
    /**
315
     * @param $key
316
     * @return string|null
317
     */
318 3
    public function getHeader($key)
319 3
    {
320 3
        return $this->headers[$key] ? $this->headers[$key] : null;
321
    }
322
323
    /**
324
     * @param array $data
325
     * @param int $statusCode
326
     */
327 1
    public function sendJsonResponse(array $data, $statusCode = 200)
328 1
    {
329 1
        $this->disableLayout();
330 1
        $this->disableView();
331 1
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
332 1
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
333 1
        $this->setHeader('Content-Type','application/json');
334 1
        $json = json_encode($data);
335 1
        $this->setBody($json);
336 1
        $this->setStatusCode($statusCode);
337 1
    }
338
339
    /**
340
     * @param null $key
341
     * @param string $default
342
     * @return array|string|null
343
     */
344 1
    public function getPost($key = null, $default = null)
345 1
    {
346 1
        if ($key) {
347
            return array_key_exists($key, $this->post) ? $this->post[$key] : $default;
348
        }
349
350 1
        return $this->post;
351
    }
352
}
353