Completed
Push — master ( 312245...cd6b50 )
by
unknown
02:35
created

TwigActionTranslationFilter::actionTranslation()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 20
nc 6
nop 1

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
 * This file is part of the Admin Bundle.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\Lin3sAdminBundle\Twig;
13
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
/**
17
 * Twig action translation filter.
18
 *
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class TwigActionTranslationFilter extends \Twig_Extension
22
{
23
    /**
24
     * The translator.
25
     *
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param TranslatorInterface $translator The translator
34
     */
35
    public function __construct(TranslatorInterface $translator)
36
    {
37
        $this->translator = $translator;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFilters()
44
    {
45
        return [
46
            'action_translation' => new \Twig_Filter_Method($this, 'actionTranslation'),
47
        ];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getName()
54
    {
55
        return 'cms_kernel_admin_bridge_twig_action_translation';
56
    }
57
58
    /**
59
     * Callback of action translation Twig filter that returns resultant array.
60
     *
61
     * @param string $options The action options
62
     *
63
     * @return array|string
64
     */
65
    public function actionTranslation($options)
66
    {
67
        if (!is_array($options)) {
68
            return $this->translator->trans($options, [], 'CmsKernelAdminBridge');
69
        }
70
71
        $resultOptions = [];
72
        foreach ($options as $optionKey => $option) {
73
            $option = null === json_decode($option, true) ? $option : json_decode($option, true);
74
75
            if (is_array($option)) {
76
                foreach ($option as $iterationKey => $iteration) {
77
                    if (is_array($iteration)) {
78
                        foreach ($iteration as $iKey => $i) {
79
                            if (is_array($i)) {
80
                                foreach ($i as $iiKey => $ii) {
81
                                    $resultOptions[$optionKey][$iterationKey][$iKey][$iiKey] = $this->translator->trans($ii);
82
                                }
83
                            } else {
84
                                $resultOptions[$optionKey][$iterationKey][$iKey] = $this->translator->trans($i);
85
                            }
86
                        }
87
                    } else {
88
                        $resultOptions[$optionKey][$iterationKey] = $this->translator->trans($iteration);
89
                    }
90
                }
91
            } else {
92
                $resultOptions[$optionKey] = $this->translator->trans($option);
93
            }
94
        }
95
96
        return $resultOptions;
97
    }
98
}
99