Completed
Push — master ( 620fe4...c35a3d )
by Derek Stephen
02:47
created

Controller::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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 1
nop 0
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 7.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Bone\Mvc;
4
5
use Bone\Db\Adapter\MySQL;
6
use PDO;
7
use Psr\Http\Message\RequestInterface;;
8
use Twig_Loader_Filesystem;
9
use Twig_Environment;
10
use Twig_Extension_Debug;
11
use stdClass;
12
use Zend\Diactoros\Response;
13
14
class Controller
15
{
16
    /**
17
     * @var RequestInterface
18
     */
19
    protected $request;
20
21
    protected $twig;
22
23
    protected $controller;
24
25
    protected $action;
26
27
    public $view;
28
29
    private $body;
30
31
    /**
32
     * @var bool
33
     */
34
    private $layout_enabled;
35
36
    /**
37
     * @var bool
38
     */
39
    private $view_enabled;
40
41
42
43
    /**
44
     * @var \Bone\Db\Adapter\MySQL
45
     */
46
    protected $_db;
47
48 19
    public function __construct(RequestInterface $request)
49 19
    {
50 19
        $this->request = $request;
51 19
        $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...
52
53 19
        $this->setTwig();
54 19
        $this->view = new stdClass();
55 19
        $this->layout_enabled = true;
56 19
        $this->view_enabled = true;
57 19
    }
58
59
    /**
60
     * @return void
61
     */
62 1
    protected function setDB()
63 1
    {
64 1
        $config = Registry::ahoy()->get('db');
65 1
        $this->_db = new MySQL($config);
66 1
    }
67
68
    /**
69
     * @return void
70
     */
71 19
    protected function setTwig()
72 19
    {
73 18
        $view_path = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ;
74 18
        $loader = new Twig_Loader_Filesystem($view_path);
75 18
        $this->twig = new Twig_Environment($loader,array('debug' => true));
76 18
        $this->twig->addExtension(new Twig_Extension_Debug());
77 18
    }
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 Twig_Environment
93
     */
94 4
    public function getTwig()
95 4
    {
96 4
        return $this->twig;
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 11
    public function setParam($key, $val)
129 11
    {
130 11
        $this->params->$key = $val;
131 11
        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->request->getHeaders();
150
    }
151
152 4
    public function hasLayoutEnabled()
153 4
    {
154 4
        return ($this->layout_enabled === true);
155
    }
156
157 1
    public function enableLayout()
158 1
    {
159 1
        $this->layout_enabled = true;
160 1
    }
161
162 3
    public function disableLayout()
163 3
    {
164 3
        $this->layout_enabled = false;
165 3
    }
166
167 4
    public function hasViewEnabled()
168 4
    {
169 4
        return ($this->view_enabled === true);
170
    }
171
172 1
    public function enableView()
173 1
    {
174 1
        $this->view_enabled = true;
175 1
    }
176
177 3
    public function disableView()
178 3
    {
179 3
        $this->view_enabled = false;
180 3
    }
181
182
    /**
183
     * @return string
184
     */
185 4
    public function getBody()
186 4
    {
187 4
        return $this->body;
188
    }
189
190
    /**
191
     *  Only used if Layout & View disabled
192
     *
193
     * @param $body
194
     */
195 1
    public function setBody($body)
196 1
    {
197 1
        $this->body = $body;
198 1
    }
199
200 1
    private function errorAction()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
201 1
    {
202 1
        $this->disableView();
203 1
        $this->disableLayout();
204 1
        $this->body = '500 Page Error.';
205 1
    }
206
207 1
    private function notFoundAction()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
208 1
    {
209 1
        $this->disableView();
210 1
        $this->disableLayout();
211 1
        $this->body = '404 Page Not Found.';
212
    }
213
}