Completed
Push — dev-master ( d4c7bb...33aff8 )
by Derek Stephen
06:03
created

Controller::getPost()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.072
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 2
    public function getParams()
122 2
    {
123 2
        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
        $params = $this->getParams();
133 1
        $param = isset($params[$param]) ? $params[$param] : null;
134 1
        if (is_string($params[$param])) {
135
            $param = urldecode($params[$param]);
136
        }
137
        return $param ?: $default;
138
    }
139
140
    /**
141
     * @param $key
142
     * @param $val
143
     * @return $this
144
     */
145 14
    public function setParam($key, $val)
146 14
    {
147 14
        $this->params->$key = $val;
148 14
        return $this;
149
    }
150
151
    /**
152
     *  runs after yer work is done
153
     */
154 15
    public function postDispatch()
155 15
    {
156
        // extend this t' run code after yer controller is finished
157 15
    }
158
159
    /**
160
     *  For loadin' th' cannon, so t' speak
161
     *
162
     * @return array
163
     */
164 4
    public function getHeaders()
165 4
    {
166 4
        return $this->headers;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172 4
    public function hasLayoutEnabled()
173 4
    {
174 4
        return ($this->layoutEnabled === true);
175
    }
176
177
    /**
178
     * Enables the layout
179
     */
180 1
    public function enableLayout()
181 1
    {
182 1
        $this->layoutEnabled = true;
183 1
    }
184
185
    /**
186
     * Disables the layout
187
     */
188 4
    public function disableLayout()
189 4
    {
190 4
        $this->layoutEnabled = false;
191 4
    }
192
193
    /**
194
     * @return bool
195
     */
196 4
    public function hasViewEnabled()
197 4
    {
198 4
        return ($this->viewEnabled === true);
199
    }
200
201
    /**
202
     * Enables the view
203
     */
204 1
    public function enableView()
205 1
    {
206 1
        $this->viewEnabled = true;
207 1
    }
208
209
    /**
210
     * Disables the view
211
     */
212 4
    public function disableView()
213 4
    {
214 4
        $this->viewEnabled = false;
215 4
    }
216
217
    /**
218
     * @return string
219
     */
220 5
    public function getBody()
221 5
    {
222 5
        return $this->body;
223
    }
224
225
    /**
226
     *  Only used if Layout & View disabled
227
     *
228
     * @param $body
229
     */
230 2
    public function setBody($body)
231 2
    {
232 2
        $this->body = $body;
233 2
    }
234
235
    /**
236
     * @return array
237
     */
238 1
    public function indexAction()
239 1
    {
240 1
        return ['message' => 'Override this method'];
241
    }
242
243 1
    public function errorAction()
244 1
    {
245 1
        $this->disableView();
246 1
        $this->disableLayout();
247 1
        $this->body = '500 Page Error.';
248 1
    }
249
250 1
    public function notFoundAction()
251 1
    {
252 1
        $this->disableView();
253 1
        $this->disableLayout();
254 1
        $this->body = '404 Page Not Found.';
255 1
    }
256
257
    /**
258
     * @return ServerRequestInterface
259
     */
260 1
    public function getRequest()
261 1
    {
262 1
        return $this->request;
263
    }
264
265
    /**
266
     * @param ServerRequestInterface $request
267
     * @return Controller
268
     */
269 1
    public function setRequest(ServerRequestInterface $request)
270 1
    {
271 1
        $this->request = $request;
272 1
        return $this;
273
    }
274
275
    /**
276
     * @param string $key
277
     * @param string $value
278
     * @return $this
279
     */
280 3
    public function setHeader($key, $value)
281 3
    {
282 3
        $this->headers[$key] = $value;
283 3
        return $this;
284
    }
285
286
    /**
287
     * @param int $statusCode
288
     */
289 1
    public function setStatusCode($statusCode)
290 1
    {
291 1
        $this->statusCode = $statusCode;
292 1
    }
293
294
    /**
295
     * @return int
296
     */
297 3
    public function getStatusCode()
298 3
    {
299 3
        return $this->statusCode;
300
    }
301
302
303
304
    /**
305
     * @param $key
306
     * @return string|null
307
     */
308 2
    public function getHeader($key)
309 2
    {
310 2
        return $this->headers[$key] ? $this->headers[$key] : null;
311
    }
312
313
    /**
314
     * @param array $data
315
     * @param int $statusCode
316
     */
317 1
    public function sendJsonResponse(array $data, $statusCode = 200)
318 1
    {
319 1
        $this->disableLayout();
320 1
        $this->disableView();
321 1
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
322 1
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
323 1
        $this->setHeader('Content-Type','application/json');
324 1
        $json = json_encode($data);
325 1
        $this->setBody($json);
326 1
        $this->setStatusCode($statusCode);
327 1
    }
328
329
    /**
330
     * @param null $key
331
     * @param string $default
332
     * @return array|string|null
333
     */
334 1
    public function getPost($key = null, $default = null)
335 1
    {
336 1
        if ($key) {
337
            return array_key_exists($key, $this->post) ? $this->post[$key] : $default;
338
        }
339
340 1
        return $this->post;
341
    }
342
}
343