Completed
Push — dev-master ( 2e700a...a98bb1 )
by Derek Stephen
12s
created

Controller::setServerEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Bone\Server\Environment;
9
use Bone\Service\MailService;
10
use PDO;
11
use Psr\Http\Message\ServerRequestInterface;
12
use stdClass;
13
use Zend\Diactoros\Response\TextResponse;
14
use Zend\Mail\Transport\Smtp;
15
use Zend\Mail\Transport\SmtpOptions;
16
17
class Controller
18
{
19
    /** @var ServerRequestInterface */
20
    protected $request;
21
22
    /** @var ViewEngine $plates */
23
    protected $viewEngine;
24
25
    /** @var string $controller */
26
    protected $controller;
27
28
    /** @var string $action  */
29
    protected $action;
30
31
    /** @var stdClass $view */
32
    public $view;
33
34
    /** @var string $body */
35
    private $body;
36
37
    /** @var bool */
38
    private $layoutEnabled;
39
40
    /** @var bool */
41
    private $viewEnabled;
42
43
    /** @var array $headers */
44
    private $headers;
45
46
    /** @var int $statusCode */
47
    private $statusCode = 200;
48
49
    /** @var MailService $mailService */
50
    private $mailService;
51
52
    /** @var array $params */
53
    public $params;
54
55
    /** @var array $post */
56
    protected $post = [];
57
58
    /** @var MySQL */
59
    protected $_db;
60
61
    /** @var Environment $serverEnvironment */
62
    protected $serverEnvironment;
63
64
    /**
65
     * Controller constructor.
66
     * @param ServerRequestInterface $request
67
     */
68 27
    public function __construct(ServerRequestInterface $request)
69 27
    {
70 27
        $this->request = $request;
71 27
        $this->headers = [];
72 27
        $this->params = $this->request->getQueryParams();
73
74 27
        if ($this->request->getMethod() == 'POST') {
75 15
            $body = $this->request->getParsedBody();
76 15
            $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...
77
        }
78
79 27
        $this->initViewEngine();
80 27
        $this->view = new stdClass();
81 27
        $this->layoutEnabled = true;
82 27
        $this->viewEnabled = true;
83 27
    }
84
85
    /**
86
     * @return void
87
     */
88 1
    protected function setDB()
89 1
    {
90 1
        $config = Registry::ahoy()->get('db');
91 1
        $this->_db = new MySQL($config);
92 1
    }
93
94
    /**
95
     * @return void
96
     */
97 27
    protected function initViewEngine()
98 27
    {
99 27
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
100 27
        $engine = new PlatesEngine($viewPath);
101 27
        $this->viewEngine = $engine;
102 27
    }
103
104 1
    public function setViewEngine(ViewEngine $engine)
105 1
    {
106 1
        $this->viewEngine = $engine;
107 1
    }
108
109
    /**
110
     * @return PDO
111
     */
112 1
    public function getDbAdapter()
113 1
    {
114 1
        if(!$this->_db)
115
        {
116 1
            $this->setDB();
117
        }
118 1
        return $this->_db->getConnection();
119
    }
120
121
    /**
122
     * @return MailService
123
     */
124 1
    public function getMailService()
125 1
    {
126 1
        if (!$this->mailService instanceof MailService) {
127 1
            $this->initMailService();
128
        }
129
130 1
        return $this->mailService;
131
    }
132
133 1
    private function initMailService()
134 1
    {
135 1
        $this->mailService = new MailService();
136 1
        $options = Registry::ahoy()->get('mail');
137 1
        if (isset($options['name']) && isset($options['host']) && isset($options['port']) ) {
138 1
            $transport = new Smtp();
139 1
            $options   = new SmtpOptions($options);
140 1
            $transport->setOptions($options);
141 1
            $this->mailService->setTransport($transport);
142
        }
143 1
    }
144
145
    /**
146
     * @return ViewEngine
147
     */
148 4
    public function getViewEngine()
149 4
    {
150 4
        return $this->viewEngine;
151
    }
152
153
154
    /**
155
     *  runs before th' controller action
156
     */
157 18
    public function init()
158 18
    {
159
        // extend this t' initialise th' controller
160 18
    }
161
162 1
    public function getParams()
163 1
    {
164 1
        return array_merge($this->params, $this->post);
165
    }
166
167
    /**
168
     * @param $param
169
     * @return mixed
170
     */
171 1
    public function getParam($param, $default = null)
172 1
    {
173 1
        $set = isset($this->params[$param]);
174 1
        if ($set && is_string($this->params[$param])) {
175 1
            return urldecode($this->params[$param]);
176 1
        } elseif ($set) {
177 1
            return $this->params[$param];
178
        }
179 1
        return $default;
180
    }
181
182
    /**
183
     * @param $key
184
     * @param $val
185
     * @return $this
186
     */
187 16
    public function setParam($key, $val)
188 16
    {
189 16
        $this->params[$key] = $val;
190 16
        return $this;
191
    }
192
193
    /**
194
     *  runs after yer work is done
195
     */
196 18
    public function postDispatch()
197 18
    {
198
        // extend this t' run code after yer controller is finished
199 18
    }
200
201
    /**
202
     *  For loadin' th' cannon, so t' speak
203
     *
204
     * @return array
205
     */
206 6
    public function getHeaders()
207 6
    {
208 6
        return $this->headers;
209
    }
210
211
    /**
212
     * @return bool
213
     */
214 4
    public function hasLayoutEnabled()
215 4
    {
216 4
        return ($this->layoutEnabled === true);
217
    }
218
219
    /**
220
     * Enables the layout
221
     */
222 1
    public function enableLayout()
223 1
    {
224 1
        $this->layoutEnabled = true;
225 1
    }
226
227
    /**
228
     * Disables the layout
229
     */
230 5
    public function disableLayout()
231 5
    {
232 5
        $this->layoutEnabled = false;
233 5
    }
234
235
    /**
236
     * @return bool
237
     */
238 4
    public function hasViewEnabled()
239 4
    {
240 4
        return ($this->viewEnabled === true);
241
    }
242
243
    /**
244
     * Enables the view
245
     */
246 1
    public function enableView()
247 1
    {
248 1
        $this->viewEnabled = true;
249 1
    }
250
251
    /**
252
     * Disables the view
253
     */
254 5
    public function disableView()
255 5
    {
256 5
        $this->viewEnabled = false;
257 5
    }
258
259
    /**
260
     * @return string
261
     */
262 5
    public function getBody()
263 5
    {
264 5
        return $this->body;
265
    }
266
267
    /**
268
     *  Only used if Layout & View disabled
269
     *
270
     * @param $body
271
     */
272 2
    public function setBody($body)
273 2
    {
274 2
        $this->body = $body;
275 2
    }
276
277
    /**
278
     * @return array
279
     */
280 1
    public function indexAction()
281 1
    {
282 1
        return ['message' => 'Override this method'];
283
    }
284
285 2
    public function errorAction()
286 2
    {
287 2
        $this->disableView();
288 2
        $this->disableLayout();
289 2
        $this->body = '500 Page Error.';
290 2
        return new TextResponse($this->body, 500);
291
    }
292
293 1
    public function notFoundAction()
294 1
    {
295 1
        $this->disableView();
296 1
        $this->disableLayout();
297 1
        $this->body = '404 Page Not Found.';
298 1
    }
299
300
    /**
301
     * @return ServerRequestInterface
302
     */
303 1
    public function getRequest()
304 1
    {
305 1
        return $this->request;
306
    }
307
308
    /**
309
     * @param ServerRequestInterface $request
310
     * @return Controller
311
     */
312 1
    public function setRequest(ServerRequestInterface $request)
313 1
    {
314 1
        $this->request = $request;
315 1
        return $this;
316
    }
317
318
    /**
319
     * @param string $key
320
     * @param string $value
321
     * @return $this
322
     */
323 3
    public function setHeader($key, $value)
324 3
    {
325 3
        $this->headers[$key] = $value;
326 3
        return $this;
327
    }
328
329
    /**
330
     * @param array $headers
331
     */
332 2
    public function setHeaders(array $headers)
333 2
    {
334 2
        $this->headers = $headers;
335 2
    }
336
337
    /**
338
     * @param int $statusCode
339
     */
340 2
    public function setStatusCode($statusCode)
341 2
    {
342 2
        $this->statusCode = $statusCode;
343 2
    }
344
345
    /**
346
     * @return int
347
     */
348 4
    public function getStatusCode()
349 4
    {
350 4
        return $this->statusCode;
351
    }
352
353
354
355
    /**
356
     * @param $key
357
     * @return string|null
358
     */
359 3
    public function getHeader($key)
360 3
    {
361 3
        return $this->headers[$key] ? $this->headers[$key] : null;
362
    }
363
364
    /**
365
     * @param array $data
366
     * @param int $statusCode
367
     */
368 1
    public function sendJsonResponse(array $data, $statusCode = 200)
369 1
    {
370 1
        $this->disableLayout();
371 1
        $this->disableView();
372 1
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
373 1
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
374 1
        $this->setHeader('Content-Type','application/json');
375 1
        $json = json_encode($data);
376 1
        $this->setBody($json);
377 1
        $this->setStatusCode($statusCode);
378 1
    }
379
380
    /**
381
     * @param null $key
382
     * @param string $default
383
     * @return array|string|null
384
     */
385 1
    public function getPost($key = null, $default = null)
386 1
    {
387 1
        if ($key) {
388 1
            return array_key_exists($key, $this->post) ? $this->post[$key] : $default;
389
        }
390
391 1
        return $this->post;
392
    }
393
394
    /**
395
     * @return Environment
396
     */
397
    public function getServerEnvironment(): Environment
398
    {
399
        return $this->serverEnvironment;
400
    }
401
402
    /**
403
     * @param Environment $serverEnvironment
404
     */
405 3
    public function setServerEnvironment(Environment $serverEnvironment)
406 3
    {
407 3
        $this->serverEnvironment = $serverEnvironment;
408 3
    }
409
}
410