Completed
Push — dev-master ( 82c8e5...74b82e )
by Derek Stephen
03:46
created

Controller::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
nc 1
cc 1
eloc 2
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
42
43
    /**
44
     * @var MySQL
45
     */
46
    protected $_db;
47
48 20
    public function __construct(ServerRequestInterface $request)
49 20
    {
50 20
        $this->request = $request;
51 20
        $this->headers = [];
52 20
        $this->params = (object) $this->request->getQueryParams();
0 ignored issues
show
Bug introduced by
The property params 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...
53
54 20
        $this->initViewEngine();
55 20
        $this->view = new stdClass();
56 20
        $this->layoutEnabled = true;
57 20
        $this->viewEnabled = true;
58 20
    }
59
60
    /**
61
     * @return void
62
     */
63 1
    protected function setDB()
64 1
    {
65 1
        $config = Registry::ahoy()->get('db');
66 1
        $this->_db = new MySQL($config);
67 1
    }
68
69
    /**
70
     * @return void
71
     */
72 20
    protected function initViewEngine()
73 20
    {
74 20
        $viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
75 20
        $engine = new PlatesEngine($viewPath);
76 20
        $this->viewEngine = $engine;
77 20
    }
78
79
    /**
80
     * @return PDO
81
     */
82 1
    public function getDbAdapter()
83 1
    {
84 1
        if(!$this->_db)
85
        {
86 1
            $this->setDB();
87
        }
88 1
        return $this->_db->getConnection();
89
    }
90
91
    /**
92
     * @return ViewEngine
93
     */
94 4
    public function getViewEngine()
95 4
    {
96 4
        return $this->viewEngine;
97
    }
98
99
100
    /**
101
     *  runs before th' controller action
102
     */
103 3
    public function init()
104 3
    {
105
        // extend this t' initialise th' controller
106 3
    }
107
108 2
    public function getParams()
109 2
    {
110 2
        return (object) $this->params;
111
    }
112
113
    /**
114
     * @param $param
115
     * @return mixed
116
     */
117 1
    public function getParam($param)
118 1
    {
119 1
        $params = $this->getParams();
120 1
        return isset($params->$param) ? $params->$param : null;
121
    }
122
123
    /**
124
     * @param $key
125
     * @param $val
126
     * @return $this
127
     */
128 14
    public function setParam($key, $val)
129 14
    {
130 14
        $this->params->$key = $val;
131 14
        return $this;
132
    }
133
134
    /**
135
     *  runs after yer work is done
136
     */
137 3
    public function postDispatch()
138 3
    {
139
        // extend this t' run code after yer controller is finished
140 3
    }
141
142
    /**
143
     *  For loadin' th' cannon, so t' speak
144
     *
145
     * @return array
146
     */
147 2
    public function getHeaders()
148 2
    {
149 2
        return $this->headers;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155 4
    public function hasLayoutEnabled()
156 4
    {
157 4
        return ($this->layoutEnabled === true);
158
    }
159
160
    /**
161
     * Enables the layout
162
     */
163 1
    public function enableLayout()
164 1
    {
165 1
        $this->layoutEnabled = true;
166 1
    }
167
168
    /**
169
     * Disables the layout
170
     */
171 3
    public function disableLayout()
172 3
    {
173 3
        $this->layoutEnabled = false;
174 3
    }
175
176
    /**
177
     * @return bool
178
     */
179 4
    public function hasViewEnabled()
180 4
    {
181 4
        return ($this->viewEnabled === true);
182
    }
183
184
    /**
185
     * Enables the view
186
     */
187 1
    public function enableView()
188 1
    {
189 1
        $this->viewEnabled = true;
190 1
    }
191
192
    /**
193
     * Disables the view
194
     */
195 3
    public function disableView()
196 3
    {
197 3
        $this->viewEnabled = false;
198 3
    }
199
200
    /**
201
     * @return string
202
     */
203 4
    public function getBody()
204 4
    {
205 4
        return $this->body;
206
    }
207
208
    /**
209
     *  Only used if Layout & View disabled
210
     *
211
     * @param $body
212
     */
213 1
    public function setBody($body)
214 1
    {
215 1
        $this->body = $body;
216 1
    }
217
218
    /**
219
     * @return array
220
     */
221 1
    public function indexAction()
222 1
    {
223 1
        return ['message' => 'Override this method'];
224
    }
225
226 1
    private function errorAction()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
227 1
    {
228 1
        $this->disableView();
229 1
        $this->disableLayout();
230 1
        $this->body = '500 Page Error.';
231 1
    }
232
233 1
    private function notFoundAction()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
234 1
    {
235 1
        $this->disableView();
236 1
        $this->disableLayout();
237 1
        $this->body = '404 Page Not Found.';
238 1
    }
239
240
    /**
241
     * @return ServerRequestInterface
242
     */
243 1
    public function getRequest()
244 1
    {
245 1
        return $this->request;
246
    }
247
248
    /**
249
     * @param ServerRequestInterface $request
250
     * @return Controller
251
     */
252 1
    public function setRequest(ServerRequestInterface $request)
253 1
    {
254 1
        $this->request = $request;
255 1
        return $this;
256
    }
257
258
    /**
259
     * @param $key
260
     * @param $value
261
     * @return $this
262
     */
263 2
    public function setHeader($key, $value)
264 2
    {
265 2
        $this->headers[$key] = $value;
266 2
        return $this;
267
    }
268
269
    /**
270
     * @param array $headers
0 ignored issues
show
Bug introduced by
There is no parameter named $headers. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
271
     * @return Controller
272
     */
273 2
    public function getHeader($key)
274 2
    {
275 2
        return $this->headers[$key] ? $this->headers[$key] : null;
276
    }
277
278
    public function sendJsonResponse(array $data)
279
    {
280
        $this->disableLayout();
281
        $this->disableView();
282
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
283
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
284
        $this->setHeader('Content-Type','application/json');
285
        $json = json_encode($data);
286
        $this->setBody($json);
287
    }
288
}