Completed
Push — master ( f17443...9dfb99 )
by Derek Stephen
02:14
created

Controller::getStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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