CookieConsentExtension::appendLabelTranslations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Indigo\Bundle\CookieConsentBundle\Twig\Extension;
4
5
use Symfony\Component\Translation\TranslatorInterface;
6
7
/**
8
 * @author Márk Sági-Kazár <[email protected]>
9
 */
10
class CookieConsentExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var TranslatorInterface
14
     */
15
    private $translator;
16
17
    /**
18
     * Configuration values and options passed to the template.
19
     *
20
     * @var array
21
     */
22
    private $config = [];
23
24
    /**
25
     * List of label options and trans keys.
26
     *
27
     * @var array
28
     */
29
    private $labels = [
30
        'message'   => 'message',
31
        'dismiss'   => 'dismiss',
32
        'learnMore' => 'learn_more',
33
    ];
34
35
    /**
36
     * @param TranslatorInterface $translator
37
     */
38
    public function __construct(TranslatorInterface $translator)
39
    {
40
        $this->translator = $translator;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getFunctions()
47
    {
48
        return array(
49
            new \Twig_SimpleFunction(
50
                'cookie_consent_render',
51
                [
52
                    $this,
53
                    'renderCookieConsent',
54
                ],
55
                [
56
                    'is_safe' => ['html'],
57
                    'needs_environment' => true,
58
                ]
59
            ),
60
        );
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getName()
67
    {
68
        return 'cookie_consent';
69
    }
70
71
    /**
72
     * @param array $config
73
     */
74
    public function setConfig(array $config)
75
    {
76
        $this->config = $config;
77
    }
78
79
    /**
80
     * Render cookie consent.
81
     *
82
     * @param \Twig_Environment $twigEnvironment
83
     *
84
     * @return string
85
     */
86
    public function renderCookieConsent(\Twig_Environment $twigEnvironment)
87
    {
88
        $config = $this->config;
89
90
        $config['options'] = $this->appendLabelTranslations($config['options']);
91
92
        return $twigEnvironment->render(
93
            'IndigoCookieConsentBundle::cookie_consent.html.twig',
94
            $config
95
        );
96
    }
97
98
    /**
99
     * Translate label options.
100
     *
101
     * @param array $options
102
     *
103
     * @return array
104
     */
105
    private function appendLabelTranslations(array $options)
106
    {
107
        foreach ($this->labels as $label => $transKey) {
108
            $options[$label] = $this->translator->trans($transKey, [], 'IndigoCookieConsentBundle');
109
        }
110
111
        return $options;
112
    }
113
}
114