TranslateViewHelper::render()   B
last analyzed

Complexity

Conditions 10
Paths 66

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 16
nc 66
nop 0
dl 0
loc 27
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2017
6
 * @package TYPO3
7
 */
8
9
10
namespace Aimeos\Aimeos\ViewHelper;
11
12
13
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
14
15
16
class TranslateViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper
17
{
18
    protected $escapeOutput = false;
19
20
21
    public function initializeArguments()
22
    {
23
        $this->registerArgument('singular', 'string', 'Singular form of the text to translate');
24
        $this->registerArgument('plural', 'string', 'Plural form of the text, used if $number is greater than one', false);
25
        $this->registerArgument('number', 'integer', 'Amount of things relevant for the plural form', false);
26
        $this->registerArgument('escape', 'boolean', 'If translated string should be escaped', false);
27
        $this->registerArgument('domain', 'string', 'Translation domain from core or an extension', false);
28
        $this->registerArgument('arguments', 'array', 'Arguments to be replaced in the resulting string', false);
29
    }
30
31
32
    public function render()
33
    {
34
        $iface = '\Aimeos\Base\View\Iface';
35
        $view = $this->templateVariableContainer->get('_aimeos_view');
36
37
        if (!is_object($view) || !($view instanceof $iface)) {
38
            throw new Exception('Aimeos view object is missing');
39
        }
40
41
        if (!isset($this->arguments['singular'])) {
42
            throw new Exception('Attribute "singular" missing for Aimeos translate view helper');
43
        }
44
45
        $singular = $this->arguments['singular'];
46
        $plural = (isset($this->arguments['plural']) ? $this->arguments['plural'] : '');
47
        $number = (isset($this->arguments['number']) ? $this->arguments['number'] : 1);
48
        $escape = (isset($this->arguments['escape']) ? $this->arguments['escape'] : true);
49
        $domain = (isset($this->arguments['domain']) ? $this->arguments['domain'] : 'client');
50
        $values = (isset($this->arguments['arguments']) ? $this->arguments['arguments'] : []);
51
52
        $string = vsprintf($view->translate($domain, $singular, $plural, $number), (array) $values);
53
54
        if ($escape === false) {
55
            return $string;
56
        }
57
58
        return $view->encoder()->html($string);
59
    }
60
}