ViewHelperService::addAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2014, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 *
26
 * @category   Core
27
 * @package    Fwk\Core
28
 * @subpackage Components
29
 * @author     Julien Ballestracci <[email protected]>
30
 * @copyright  2011-2014 Julien Ballestracci <[email protected]>
31
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
32
 * @link       http://www.phpfwk.com
33
 */
34
namespace Fwk\Core\Components\ViewHelper;
35
36
use Fwk\Core\Context;
37
use Fwk\Core\Application;
38
39
/**
40
 * This is the View Helper
41
 *
42
 * @category   Utilities
43
 * @package    Fwk\Core
44
 * @subpackage Components
45
 * @author     Julien Ballestracci <[email protected]>
46
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
47
 * @link       http://www.phpfwk.com
48
 */
49
class ViewHelperService
50
{
51
    /**
52
     * Name of the property where the ViewHelper should be placed
53
     */
54
    const DEFAULT_PROP_NAME = '_helper';
55
56
    /**
57
     * @var array
58
     */
59
    protected $helpers = array();
60
61
    /**
62
     * Should the viewHelper fail silently or throw exceptions?
63
     *
64
     * @var boolean
65
     */
66
    private $throwExceptions = true;
67
68
    /**
69
     * The current context
70
     *
71
     * @var Context
72
     */
73
    protected $context;
74
    
75
    /**
76
     * Property of the ViewHelperService in Action's data
77
     * 
78
     * @var string
79
     */
80
    protected $propName = self::DEFAULT_PROP_NAME;
81
82
    /**
83
     * The running Application
84
     * 
85
     * @var Application
86
     */
87
    protected $application;
88
    
89
    /**
90
     * Constructor
91
     * 
92
     * @param string  $propName        Name of the ViewHelperService property
93
     * @param boolean $throwExceptions Should the Service throw exceptions or fail
94
     * silently
95
     * 
96
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
97
     */
98 12
    public function __construct($propName = self::DEFAULT_PROP_NAME, 
99
        $throwExceptions = true
100
    ) {
101 12
        $this->propName         = $propName;
102 12
        $this->throwExceptions  = $throwExceptions;
103 12
    }
104
105
    /**
106
     *
107
     * @param string $name
108
     * @param ViewHelper $helper
109
     *
110
     * @return ViewHelperService
111
     */
112 6
    public function add($name, ViewHelper $helper)
113
    {
114 6
        $this->helpers[strtolower($name)] = $helper;
115 6
        $helper->setViewHelperService($this);
116
117 6
        return $this;
118
    }
119
120
    /**
121
     *
122
     * @param array $helpers
123
     *
124
     * @return ViewHelperService
125
     */
126 1
    public function addAll(array $helpers)
127
    {
128 1
        foreach ($helpers as $key => $helper) {
129 1
            $this->add($key, $helper);
130 1
        }
131
132 1
        return $this;
133
    }
134
135
    /**
136
     *
137
     * @param string $helperName
138
     *
139
     * @return ViewHelperService
140
     * @throws Exception if helper not registered
141
     */
142 1
    public function remove($helperName)
143
    {
144 1
        if (!isset($this->helpers[$helperName])) {
145 1
            throw new Exception(sprintf("Unregistered helper '%s'", $helperName));
146
        }
147
        
148 1
        unset($this->helpers[strtolower($helperName)]);
149
150 1
        return $this;
151
    }
152
153
    /**
154
     *
155
     * @param string $name
156
     *
157
     * @return ViewHelper
158
     * @throws Exception if helper not registered
159
     */
160 8
    public function helper($name)
161
    {
162 8
        $name = strtolower($name);
163 8
        if (!isset($this->helpers[$name])) {
164 3
            throw new Exception(sprintf("Unregistered helper '%s'", $name));
165
        }
166
167 5
        return $this->helpers[$name];
168
    }
169
170
    /**
171
     *
172
     * @param boolean $bool
173
     *
174
     * @return ViewHelper
0 ignored issues
show
Documentation introduced by
Should the return type not be ViewHelperService?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
175
     */
176 3
    public function throwExceptions($bool)
177
    {
178 3
        $this->throwExceptions = (bool)$bool;
179
180 3
        return $this;
181
    }
182
183
    /**
184
     * Tells if the service throws exceptions or fail silently
185
     * 
186
     * @return boolean
187
     */
188 1
    public function isThrowExceptions()
189
    {
190 1
        return $this->throwExceptions;
191
    }
192
    
193
    /**
194
     *
195
     * @param string $name
196
     * @param mixed $arguments
197
     *
198
     * @return mixed
199
     * @throws Exception (if invalid callback && throwExceptions = true)
200
     */
201 5
    public function __call($name, $arguments)
202
    {
203
        try {
204 5
            $helper = $this->helper($name);
205 5
        } catch(Exception $exc) {
206 2
            if ($this->throwExceptions) {
207 1
                throw $exc;
208
            }
209
210 1
            return false;
211
        }
212
213 3
        $result = false;
214
        try {
215 3
            $result = $helper->execute($arguments);
216 3
        } catch(\Exception $exp) {
217 2
            if ($this->throwExceptions) {
218 1
                throw new Exception(
219 1
                    'ViewHelper '. get_class($helper) .' execution failed', 
220 1
                    $exp->getCode(), 
221
                    $exp
222 1
                );
223
            }
224
        }
225
        
226 2
        return $result;
227
    }
228
229
    /**
230
     *
231
     * @param Context $context
232
     *
233
     * @return ViewHelper
0 ignored issues
show
Documentation introduced by
Should the return type not be ViewHelperService?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
234
     */
235 3
    public function setContext(Context $context)
236
    {
237 3
        $this->context = $context;
238
239 3
        return $this;
240
    }
241
242
    /**
243
     *
244
     * @return Context
245
     */
246
    public function getContext()
247
    {
248
        return $this->context;
249
    }
250
    
251 4
    public function getPropName()
252
    {
253 4
        return $this->propName;
254
    }
255
256
    /**
257
     * 
258
     * @param string $propName
259
     * 
260
     * @return ViewHelperService
261
     */
262 1
    public function setPropName($propName)
263
    {
264 1
        $this->propName = $propName;
265
        
266 1
        return $this;
267
    }
268
    
269
    /**
270
     * 
271
     * @return Application
272
     */
273
    public function getApplication()
274
    {
275
        return $this->application;
276
    }
277
278
    /**
279
     * 
280
     * @param Application $application
281
     * 
282
     * @return ViewHelperService
283
     */
284 3
    public function setApplication(Application $application)
285
    {
286 3
        $this->application = $application;
287
        
288 3
        return $this;
289
    }
290
291
292
}