Completed
Push — dev-master ( 3d2c20...b7e24f )
by Derek Stephen
08:18 queued 03:13
created

AbstractController::getBody()   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 0
crap 2
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Bone\Db\Adapter\MySQL;
6
use Bone\Mvc\View\PlatesEngine;
7
use Bone\Mvc\View\ViewEngine;
8
use Bone\Server\Environment;
9
use Psr\Http\Message\ServerRequestInterface;
10
use stdClass;
11
12
abstract class AbstractController
13
{
14
    /** @var Environment $serverEnvironment */
15
    protected $serverEnvironment;
16
17
    /** @var ServerRequestInterface */
18
    protected $request;
19
20
    /** @var ViewEngine $plates */
21
    protected $viewEngine;
22
23
    /** @var MySQL */
24
    protected $db;
25
26
    /** @var string $controller */
27
    protected $controller;
28
29
    /** @var string $action */
30
    protected $action;
31
32
    /** @var stdClass $view */
33
    public $view;
34
35
    /** @var string $body */
36
    private $body;
37
38
    /** @var bool */
39
    private $layoutEnabled;
40
41
    /** @var bool */
42
    private $viewEnabled;
43
44
    /** @var array $headers */
45
    private $headers;
46
47
    /** @var int $statusCode */
48
    private $statusCode = 200;
49
50
    /** @var array $params */
51
    public $params;
52
53
    /** @var array $post */
54
    protected $post = [];
55
56
    /**
57
     * Controller constructor.
58
     * @param ServerRequestInterface $request
59
     * @throws \Exception
60
     */
61
    public function __construct(ServerRequestInterface $request)
62
    {
63
        $this->request = $request;
64
        $this->headers = [];
65
        $this->params = $this->request->getQueryParams();
66
67
        if ($this->request->getMethod() == 'POST') {
68
            $body = $this->request->getParsedBody();
69
            $this->post = $body ?: [];
0 ignored issues
show
Documentation Bug introduced by
It seems like $body ?: array() can also be of type 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...
70
        }
71
72
        $this->initViewEngine();
73
        $this->view = new stdClass();
74
        $this->layoutEnabled = true;
75
        $this->viewEnabled = true;
76
    }
77
78
    public function getParams()
79
    {
80
        return array_merge($this->params, $this->post);
81
    }
82
83
    /**
84
     * @param $param
85
     * @param null $default
86
     * @return mixed|null|string
87
     */
88
    public function getParam($param, $default = null)
89
    {
90
        $set = isset($this->params[$param]);
91
        if ($set && is_string($this->params[$param])) {
92
            return urldecode($this->params[$param]);
93
        } elseif ($set) {
94
            return $this->params[$param];
95
        }
96
        return $default;
97
    }
98
99
    /**
100
     * @param $key
101
     * @param $val
102
     * @return $this
103
     */
104
    public function setParam($key, $val)
105
    {
106
        $this->params[$key] = $val;
107
        return $this;
108
    }
109
110
    /**
111
     * @return void
112
     */
113
    protected function setDB()
114
    {
115
        $config = Registry::ahoy()->get('db');
116
        $this->db = new MySQL($config);
117
    }
118
119
    /**
120
     * @return void
121
     */
122
    protected function initViewEngine()
123
    {
124
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
125
        $engine = new PlatesEngine($viewPath);
126
        $this->viewEngine = $engine;
127
    }
128
129
    public function setViewEngine(ViewEngine $engine)
130
    {
131
        $this->viewEngine = $engine;
132
    }
133
134
    /**
135
     * @return PDO
136
     */
137
    public function getDbAdapter()
138
    {
139
        if(!$this->db)
140
        {
141
            $this->setDB();
142
        }
143
        return $this->db->getConnection();
144
    }
145
146
    /**
147
     *  For loadin' th' cannon, so t' speak
148
     *
149
     * @return array
150
     */
151
    public function getHeaders()
152
    {
153
        return $this->headers;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function hasLayoutEnabled()
160
    {
161
        return ($this->layoutEnabled === true);
162
    }
163
164
    /**
165
     * Enables the layout
166
     */
167
    public function enableLayout()
168
    {
169
        $this->layoutEnabled = true;
170
    }
171
172
    /**
173
     * Disables the layout
174
     */
175
    public function disableLayout()
176
    {
177
        $this->layoutEnabled = false;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    public function hasViewEnabled()
184
    {
185
        return ($this->viewEnabled === true);
186
    }
187
188
    /**
189
     * Enables the view
190
     */
191
    public function enableView()
192
    {
193
        $this->viewEnabled = true;
194
    }
195
196
    /**
197
     * Disables the view
198
     */
199
    public function disableView()
200
    {
201
        $this->viewEnabled = false;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getBody()
208
    {
209
        return $this->body;
210
    }
211
212
    /**
213
     *  Only used if Layout & View disabled
214
     *
215
     * @param $body
216
     */
217
    public function setBody($body)
218
    {
219
        $this->body = $body;
220
    }
221
222
    /**
223
     * @param string $key
224
     * @param string $value
225
     * @return $this
226
     */
227
    public function setHeader($key, $value)
228
    {
229
        $this->headers[$key] = $value;
230
        return $this;
231
    }
232
233
    /**
234
     * @param array $headers
235
     */
236
    public function setHeaders(array $headers)
237
    {
238
        $this->headers = $headers;
239
    }
240
241
    /**
242
     * @param int $statusCode
243
     */
244
    public function setStatusCode($statusCode)
245
    {
246
        $this->statusCode = $statusCode;
247
    }
248
249
    /**
250
     * @return int
251
     */
252
    public function getStatusCode()
253
    {
254
        return $this->statusCode;
255
    }
256
257
258
    /**
259
     * @param $key
260
     * @return string|null
261
     */
262
    public function getHeader($key)
263
    {
264
        return $this->headers[$key] ? $this->headers[$key] : null;
265
    }
266
267
    /**
268
     * @return ServerRequestInterface
269
     */
270
    public function getRequest()
271
    {
272
        return $this->request;
273
    }
274
275
    /**
276
     * @param ServerRequestInterface $request
277
     * @return Controller
278
     */
279
    public function setRequest(ServerRequestInterface $request)
280
    {
281
        $this->request = $request;
282
        return $this;
283
    }
284
285
    /**
286
     * @param array $data
287
     * @param int $statusCode
288
     */
289
    public function sendJsonResponse(array $data, $statusCode = 200)
290
    {
291
        $this->disableLayout();
292
        $this->disableView();
293
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
294
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
295
        $this->setHeader('Content-Type','application/json');
296
        $json = json_encode($data);
297
        $this->setBody($json);
298
        $this->setStatusCode($statusCode);
299
    }
300
301
    /**
302
     * @param null $key
303
     * @param string $default
304
     * @return array|string|null
305
     */
306
    public function getPost($key = null, $default = null)
307
    {
308
        if ($key) {
309
            return array_key_exists($key, $this->post) ? $this->post[$key] : $default;
310
        }
311
312
        return $this->post;
313
    }
314
315
    /**
316
     * @return Environment
317
     */
318
    public function getServerEnvironment(): Environment
319
    {
320
        return $this->serverEnvironment;
321
    }
322
323
    /**
324
     * @param Environment $serverEnvironment
325
     */
326
    public function setServerEnvironment(Environment $serverEnvironment)
327
    {
328
        $this->serverEnvironment = $serverEnvironment;
329
    }
330
331
    /**
332
     * @return ViewEngine
333
     */
334
    public function getViewEngine()
335
    {
336
        return $this->viewEngine;
337
    }
338
339
}