AbstractView   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 2
cbo 0
dl 0
loc 208
ccs 40
cts 44
cp 0.9091
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
getOrder() 0 1 ?
render() 0 1 ?
B getViewSettings() 0 26 6
A matchController() 0 6 3
A matchAction() 0 6 3
A matchBoth() 0 4 2
A getViewSimpleName() 0 4 1
A registerControl() 0 4 1
A createControl() 0 5 1
A hasControl() 0 4 1
A setCssFiles() 0 5 1
A setJsFiles() 0 5 1
A getJsFiles() 0 4 1
A getCssFiles() 0 4 1
1
<?php
2
namespace Nkey\Caribu\Mvc\View;
3
4
use \Nkey\Caribu\Mvc\Controller\Response;
5
use \Nkey\Caribu\Mvc\Controller\Request;
6
use \Nkey\Caribu\Mvc\View\View;
7
8
/**
9
 * Abstract view class
10
 *
11
 * This class provides some basic functions which are needed by every
12
 * concrete view class. It initializes the view by reading the
13
 * annotations from elements and provide them as settings.
14
 *
15
 * It also has the ability to check whether the view is responsible
16
 * for a particular request.
17
 *
18
 * All basic functions are final and cannot be overriden.
19
 *
20
 * @author Maik Greubel <[email protected]>
21
 *
22
 *         This file is part of Caribu MVC package
23
 */
24
abstract class AbstractView implements View
25
{
26
27
    /**
28
     * The name of view
29
     *
30
     * @var string
31
     */
32
    private $viewName;
33
34
    /**
35
     * List of allowed controllers
36
     *
37
     * @var array
38
     */
39
    private $controllers = array();
40
41
    /**
42
     * List of allowed actions
43
     *
44
     * @var array
45
     */
46
    private $actions = array();
47
48
    /**
49
     * List of view controls
50
     *
51
     * @var array
52
     */
53
    private $controls = array();
54
55
    /**
56
     * List of additional css files
57
     *
58
     * @var array
59
     */
60
    private $cssFiles = array();
61
62
    /**
63
     * List of additional javascript files
64
     *
65
     * @var array
66
     */
67
    private $jsFiles = array();
68
69
    /**
70
     * (non-PHPdoc)
71
     *
72
     * @see \Nkey\Caribu\Mvc\View\View::getOrder()
73
     */
74
    abstract public function getOrder();
75
76
    /**
77
     * (non-PHPdoc)
78
     *
79
     * @see \Nkey\Caribu\Mvc\View\View::render()
80
     */
81
    abstract public function render(Response &$response, Request $request, $parameters = array());
82
83
    /**
84
     * Retrieve the settings from view
85
     *
86
     * @return \Nkey\Caribu\Mvc\View\View
87
     */
88 29
    final public function getViewSettings()
89
    {
90 29
        $rf = new \ReflectionClass($this);
91
        
92 29
        $this->viewName = str_replace('View', '', $rf->getShortName());
93
        
94 29
        $matches = array();
95 29
        if (preg_match("#@applyTo\((.*)\)#", $rf->getDocComment(), $matches)) {
96 29
            $params = array();
97 29
            parse_str(str_replace(',', '&', $matches[1]), $params);
98
            
99 29
            if (is_array($params)) {
100 29
                foreach ($params as $param => $value) {
101 29
                    if ($param == 'controller') {
102 29
                        $this->controllers = explode('|', $value);
103
                    }
104
                    
105 29
                    if ($param == 'action') {
106 29
                        $this->actions = explode('|', $value);
107
                    }
108
                }
109
            }
110
        }
111
        
112 29
        return $this;
113
    }
114
115
    /**
116
     * (non-PHPdoc)
117
     *
118
     * @see \Nkey\Caribu\Mvc\View\View::matchController()
119
     */
120 20
    final public function matchController($controller)
121
    {
122 20
        return (in_array($controller, $this->controllers) ||
123 20
				in_array('any', $this->controllers) ||
124 20
				count($this->controllers) == 0);
125
    }
126
127
    /**
128
     * (non-PHPdoc)
129
     *
130
     * @see \Nkey\Caribu\Mvc\View\View::matchAction()
131
     */
132 20
    final public function matchAction($action)
133
    {
134 20
        return (in_array($action, $this->actions) ||
135 20
        		in_array('any', $this->actions) ||
136 20
        		count($this->actions) == 0);
137
    }
138
139
    /**
140
     * (non-PHPdoc)
141
     *
142
     * @see \Nkey\Caribu\Mvc\View\View::matchBoth()
143
     */
144 20
    final public function matchBoth($controller, $action)
145
    {
146 20
        return $this->matchController($controller) && $this->matchAction($action);
147
    }
148
149
    /**
150
     * Get name of view
151
     *
152
     * @return string The name of view
153
     */
154 29
    final public function getViewSimpleName()
155
    {
156 29
        return $this->viewName;
157
    }
158
159
    /**
160
     * (non-PHPdoc)
161
     *
162
     * @see \Nkey\Caribu\Mvc\View\View::registerControl()
163
     */
164 4
    final public function registerControl($controlClass, $controlIdentifier)
165
    {
166 4
        $this->controls[$controlIdentifier] = $controlClass;
167 4
    }
168
169
    /**
170
     * (non-PHPdoc)
171
     *
172
     * @see \Nkey\Caribu\Mvc\View\View::createControl()
173
     */
174 1
    final public function createControl($controlIdentifier)
175
    {
176 1
        $rf = new \ReflectionClass($this->controls[$controlIdentifier]);
177 1
        return $rf->newInstance();
178
    }
179
180
    /**
181
     * (non-PHPdoc)
182
     *
183
     * @see \Nkey\Caribu\Mvc\View\View::hasControl()
184
     */
185 2
    final public function hasControl($controlIdentifier)
186
    {
187 2
        return isset($this->controls[$controlIdentifier]);
188
    }
189
190
    /**
191
     * (non-PHPdoc)
192
     *
193
     * @see \Nkey\Caribu\Mvc\View\View::setCssFiles()
194
     */
195 20
    final public function setCssFiles(array $files)
196
    {
197 20
        $this->cssFiles = $files;
198 20
        return $this;
199
    }
200
201
    /**
202
     * (non-PHPdoc)
203
     *
204
     * @see \Nkey\Caribu\Mvc\View\View::setJsFiles()
205
     */
206 20
    final public function setJsFiles(array $files)
207
    {
208 20
        $this->jsFiles = $files;
209 20
        return $this;
210
    }
211
212
    /**
213
     * Retrieve all js files
214
     *
215
     * @return array
216
     */
217
    final protected function getJsFiles()
218
    {
219
        return $this->jsFiles;
220
    }
221
222
    /**
223
     * Retrieve all css files
224
     *
225
     * @return array
226
     */
227
    final protected function getCssFiles()
228
    {
229
        return $this->cssFiles;
230
    }
231
}
232