Completed
Push — master ( ae9ad1...baed7e )
by Derek Stephen
02:49
created

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