Completed
Push — dev-master ( 0413d5...ea7d5a )
by Derek Stephen
06:04
created

Controller::initMailService()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

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