Completed
Push — dev-master ( 33aff8...78a69c )
by Derek Stephen
02:03
created

Controller   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 4

Test Coverage

Coverage 96.32%

Importance

Changes 0
Metric Value
wmc 39
lcom 5
cbo 4
dl 0
loc 332
ccs 131
cts 136
cp 0.9632
rs 9.28
c 0
b 0
f 0

30 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A setDB() 0 5 1
A initViewEngine() 0 6 2
A getDbAdapter() 0 8 2
A getViewEngine() 0 4 1
A init() 0 4 1
A getParams() 0 4 1
A setParam() 0 5 1
A postDispatch() 0 4 1
A getHeaders() 0 4 1
A hasLayoutEnabled() 0 4 1
A enableLayout() 0 4 1
A disableLayout() 0 4 1
A hasViewEnabled() 0 4 1
A enableView() 0 4 1
A disableView() 0 4 1
A getBody() 0 4 1
A setBody() 0 4 1
A indexAction() 0 4 1
A getParam() 0 10 4
A errorAction() 0 6 1
A notFoundAction() 0 6 1
A getRequest() 0 4 1
A setRequest() 0 5 1
A setHeader() 0 5 1
A setStatusCode() 0 4 1
A getStatusCode() 0 4 1
A getHeader() 0 4 2
A sendJsonResponse() 0 11 1
A getPost() 0 8 3
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
12
class Controller
13
{
14
    /** @var ServerRequestInterface */
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
    /** @var array $headers */
39
    private $headers;
40
41
    /** @var int $statusCode */
42
    private $statusCode = 200;
43
44
    /** @var object $params */
45
    public $params;
46
47
    /** @var array $post */
48
    protected $post = [];
49
50
    /** @var MySQL */
51
    protected $_db;
52
53
    /**
54
     * Controller constructor.
55
     * @param ServerRequestInterface $request
56
     */
57 21
    public function __construct(ServerRequestInterface $request)
58 21
    {
59 21
        $this->request = $request;
60 21
        $this->headers = [];
61 21
        $this->params = (object) $this->request->getQueryParams();
62
63 21
        if ($this->request->getMethod() == 'POST') {
64
            $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...
65
        }
66
67 21
        $this->initViewEngine();
68 21
        $this->view = new stdClass();
69 21
        $this->layoutEnabled = true;
70 21
        $this->viewEnabled = true;
71 21
    }
72
73
    /**
74
     * @return void
75
     */
76 1
    protected function setDB()
77 1
    {
78 1
        $config = Registry::ahoy()->get('db');
79 1
        $this->_db = new MySQL($config);
80 1
    }
81
82
    /**
83
     * @return void
84
     */
85 21
    protected function initViewEngine()
86 21
    {
87 21
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
88 21
        $engine = new PlatesEngine($viewPath);
89 21
        $this->viewEngine = $engine;
90 21
    }
91
92
    /**
93
     * @return PDO
94
     */
95 1
    public function getDbAdapter()
96 1
    {
97 1
        if(!$this->_db)
98
        {
99 1
            $this->setDB();
100
        }
101 1
        return $this->_db->getConnection();
102
    }
103
104
    /**
105
     * @return ViewEngine
106
     */
107 4
    public function getViewEngine()
108 4
    {
109 4
        return $this->viewEngine;
110
    }
111
112
113
    /**
114
     *  runs before th' controller action
115
     */
116 15
    public function init()
117 15
    {
118
        // extend this t' initialise th' controller
119 15
    }
120
121 1
    public function getParams()
122 1
    {
123 1
        return array_merge((array) $this->params, $this->post);
124
    }
125
126
    /**
127
     * @param $param
128
     * @return mixed
129
     */
130 1
    public function getParam($param, $default = null)
131 1
    {
132 1
        $set = isset($this->params->$param);
133 1
        if ($set && is_string($this->params->$param)) {
134 1
            return urldecode($this->params->$param);
135
        } elseif ($set) {
136
            return $this->params->$param;
137
        }
138
        return $default;
139
    }
140
141
    /**
142
     * @param $key
143
     * @param $val
144
     * @return $this
145
     */
146 14
    public function setParam($key, $val)
147 14
    {
148 14
        $this->params->$key = $val;
149 14
        return $this;
150
    }
151
152
    /**
153
     *  runs after yer work is done
154
     */
155 15
    public function postDispatch()
156 15
    {
157
        // extend this t' run code after yer controller is finished
158 15
    }
159
160
    /**
161
     *  For loadin' th' cannon, so t' speak
162
     *
163
     * @return array
164
     */
165 4
    public function getHeaders()
166 4
    {
167 4
        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 4
    public function disableLayout()
190 4
    {
191 4
        $this->layoutEnabled = false;
192 4
    }
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 4
    public function disableView()
214 4
    {
215 4
        $this->viewEnabled = false;
216 4
    }
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 1
    public function errorAction()
245 1
    {
246 1
        $this->disableView();
247 1
        $this->disableLayout();
248 1
        $this->body = '500 Page Error.';
249 1
    }
250
251 1
    public function notFoundAction()
252 1
    {
253 1
        $this->disableView();
254 1
        $this->disableLayout();
255 1
        $this->body = '404 Page Not Found.';
256 1
    }
257
258
    /**
259
     * @return ServerRequestInterface
260
     */
261 1
    public function getRequest()
262 1
    {
263 1
        return $this->request;
264
    }
265
266
    /**
267
     * @param ServerRequestInterface $request
268
     * @return Controller
269
     */
270 1
    public function setRequest(ServerRequestInterface $request)
271 1
    {
272 1
        $this->request = $request;
273 1
        return $this;
274
    }
275
276
    /**
277
     * @param string $key
278
     * @param string $value
279
     * @return $this
280
     */
281 3
    public function setHeader($key, $value)
282 3
    {
283 3
        $this->headers[$key] = $value;
284 3
        return $this;
285
    }
286
287
    /**
288
     * @param int $statusCode
289
     */
290 1
    public function setStatusCode($statusCode)
291 1
    {
292 1
        $this->statusCode = $statusCode;
293 1
    }
294
295
    /**
296
     * @return int
297
     */
298 3
    public function getStatusCode()
299 3
    {
300 3
        return $this->statusCode;
301
    }
302
303
304
305
    /**
306
     * @param $key
307
     * @return string|null
308
     */
309 2
    public function getHeader($key)
310 2
    {
311 2
        return $this->headers[$key] ? $this->headers[$key] : null;
312
    }
313
314
    /**
315
     * @param array $data
316
     * @param int $statusCode
317
     */
318 1
    public function sendJsonResponse(array $data, $statusCode = 200)
319 1
    {
320 1
        $this->disableLayout();
321 1
        $this->disableView();
322 1
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
323 1
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
324 1
        $this->setHeader('Content-Type','application/json');
325 1
        $json = json_encode($data);
326 1
        $this->setBody($json);
327 1
        $this->setStatusCode($statusCode);
328 1
    }
329
330
    /**
331
     * @param null $key
332
     * @param string $default
333
     * @return array|string|null
334
     */
335 1
    public function getPost($key = null, $default = null)
336 1
    {
337 1
        if ($key) {
338
            return array_key_exists($key, $this->post) ? $this->post[$key] : $default;
339
        }
340
341 1
        return $this->post;
342
    }
343
}
344