Completed
Push — master ( 778961...e63dfa )
by Márk
05:24
created

CookieConsentExtension::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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