Completed
Push — dev-master ( 8ed987...1e9198 )
by Derek Stephen
04:46
created

AbstractController::initViewEngine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
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 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 array $params */
45
    public $params;
46
47
    /** @var array $post */
48
    protected $post = [];
49
50
    /**
51
     * Controller constructor.
52
     * @param ServerRequestInterface $request
53
     * @throws \Exception
54
     */
55 33
    public function __construct(ServerRequestInterface $request)
56 33
    {
57 33
        $this->request = $request;
58 33
        $this->headers = [];
59 33
        $this->params = $this->request->getQueryParams();
60
61 33
        if ($this->request->getMethod() == 'POST') {
62 21
            $body = $this->request->getParsedBody();
63 21
            $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...
64
        }
65
66 33
        $this->initViewEngine();
67 33
        $this->view = new stdClass();
68 33
        $this->layoutEnabled = true;
69 33
        $this->viewEnabled = true;
70 33
    }
71
72 1
    public function getParams()
73 1
    {
74 1
        return array_merge($this->params, $this->post);
75
    }
76
77
    /**
78
     * @param $param
79
     * @param null $default
80
     * @return mixed|null|string
81
     */
82 1
    public function getParam($param, $default = null)
83 1
    {
84 1
        $set = isset($this->params[$param]);
85 1
        if ($set && is_string($this->params[$param])) {
86 1
            return urldecode($this->params[$param]);
87 1
        } elseif ($set) {
88 1
            return $this->params[$param];
89
        }
90 1
        return $default;
91
    }
92
93
    /**
94
     * @param $key
95
     * @param $val
96
     * @return $this
97
     */
98 22
    public function setParam($key, $val)
99 22
    {
100 22
        $this->params[$key] = $val;
101 22
        return $this;
102
    }
103
104
    /**
105
     * @return void
106
     */
107 1
    protected function setDB()
108 1
    {
109 1
        $config = Registry::ahoy()->get('db');
110 1
        $this->_db = new MySQL($config);
0 ignored issues
show
Bug introduced by
The property _db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
111 1
    }
112
113
    /**
114
     * @return void
115
     */
116 33
    protected function initViewEngine()
117 33
    {
118 33
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
119 33
        $engine = new PlatesEngine($viewPath);
120 33
        $this->viewEngine = $engine;
121 33
    }
122
123 1
    public function setViewEngine(ViewEngine $engine)
124 1
    {
125 1
        $this->viewEngine = $engine;
126 1
    }
127
128
    /**
129
     * @return PDO
130
     */
131 1
    public function getDbAdapter()
132 1
    {
133 1
        if(!$this->_db)
134
        {
135 1
            $this->setDB();
136
        }
137 1
        return $this->_db->getConnection();
138
    }
139
140
    /**
141
     *  For loadin' th' cannon, so t' speak
142
     *
143
     * @return array
144
     */
145 6
    public function getHeaders()
146 6
    {
147 6
        return $this->headers;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153 4
    public function hasLayoutEnabled()
154 4
    {
155 4
        return ($this->layoutEnabled === true);
156
    }
157
158
    /**
159
     * Enables the layout
160
     */
161 1
    public function enableLayout()
162 1
    {
163 1
        $this->layoutEnabled = true;
164 1
    }
165
166
    /**
167
     * Disables the layout
168
     */
169 5
    public function disableLayout()
170 5
    {
171 5
        $this->layoutEnabled = false;
172 5
    }
173
174
    /**
175
     * @return bool
176
     */
177 4
    public function hasViewEnabled()
178 4
    {
179 4
        return ($this->viewEnabled === true);
180
    }
181
182
    /**
183
     * Enables the view
184
     */
185 1
    public function enableView()
186 1
    {
187 1
        $this->viewEnabled = true;
188 1
    }
189
190
    /**
191
     * Disables the view
192
     */
193 5
    public function disableView()
194 5
    {
195 5
        $this->viewEnabled = false;
196 5
    }
197
198
    /**
199
     * @return string
200
     */
201 6
    public function getBody()
202 6
    {
203 6
        return $this->body;
204
    }
205
206
    /**
207
     *  Only used if Layout & View disabled
208
     *
209
     * @param $body
210
     */
211 5
    public function setBody($body)
212 5
    {
213 5
        $this->body = $body;
214 5
    }
215
216
    /**
217
     * @param string $key
218
     * @param string $value
219
     * @return $this
220
     */
221 3
    public function setHeader($key, $value)
222 3
    {
223 3
        $this->headers[$key] = $value;
224 3
        return $this;
225
    }
226
227
    /**
228
     * @param array $headers
229
     */
230 2
    public function setHeaders(array $headers)
231 2
    {
232 2
        $this->headers = $headers;
233 2
    }
234
235
    /**
236
     * @param int $statusCode
237
     */
238 2
    public function setStatusCode($statusCode)
239 2
    {
240 2
        $this->statusCode = $statusCode;
241 2
    }
242
243
    /**
244
     * @return int
245
     */
246 4
    public function getStatusCode()
247 4
    {
248 4
        return $this->statusCode;
249
    }
250
251
252
    /**
253
     * @param $key
254
     * @return string|null
255
     */
256 3
    public function getHeader($key)
257 3
    {
258 3
        return $this->headers[$key] ? $this->headers[$key] : null;
259
    }
260
261
    /**
262
     * @return ServerRequestInterface
263
     */
264 1
    public function getRequest()
265 1
    {
266 1
        return $this->request;
267
    }
268
269
    /**
270
     * @param ServerRequestInterface $request
271
     * @return Controller
272
     */
273 1
    public function setRequest(ServerRequestInterface $request)
274 1
    {
275 1
        $this->request = $request;
276 1
        return $this;
277
    }
278
279
    /**
280
     * @param array $data
281
     * @param int $statusCode
282
     */
283 1
    public function sendJsonResponse(array $data, $statusCode = 200)
284 1
    {
285 1
        $this->disableLayout();
286 1
        $this->disableView();
287 1
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
288 1
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
289 1
        $this->setHeader('Content-Type','application/json');
290 1
        $json = json_encode($data);
291 1
        $this->setBody($json);
292 1
        $this->setStatusCode($statusCode);
293 1
    }
294
295
    /**
296
     * @param null $key
297
     * @param string $default
298
     * @return array|string|null
299
     */
300 1
    public function getPost($key = null, $default = null)
301 1
    {
302 1
        if ($key) {
303 1
            return array_key_exists($key, $this->post) ? $this->post[$key] : $default;
304
        }
305
306 1
        return $this->post;
307
    }
308
309
    /**
310
     * @return Environment
311
     */
312
    public function getServerEnvironment(): Environment
313
    {
314
        return $this->serverEnvironment;
0 ignored issues
show
Bug introduced by
The property serverEnvironment does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
315
    }
316
317
    /**
318
     * @param Environment $serverEnvironment
319
     */
320 3
    public function setServerEnvironment(Environment $serverEnvironment)
321 3
    {
322 3
        $this->serverEnvironment = $serverEnvironment;
323 3
    }
324
325
    /**
326
     * @return ViewEngine
327
     */
328 15
    public function getViewEngine()
329 15
    {
330 15
        return $this->viewEngine;
331
    }
332
333
}