Completed
Pull Request — master (#5)
by Márk
02:43
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 1
Bugs 1 Features 1
Metric Value
c 1
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',
31
        'dismiss',
32
        'learnMore',
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 $options
73
     */
74
    public function setOptions(array $options)
75
    {
76
        $this->options = $options;
0 ignored issues
show
Bug introduced by
The property options does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77
    }
78
79
    /**
80
     * @param array $config
81
     */
82
    public function setConfig(array $config)
83
    {
84
        $this->config = $config;
85
    }
86
87
    /**
88
     * Render cookie consent.
89
     *
90
     * @param \Twig_Environment $twigEnvironment
91
     *
92
     * @return string
93
     */
94
    public function renderCookieConsent(\Twig_Environment $twigEnvironment)
95
    {
96
        $config = $this->config;
97
98
        if($config['translation']) {
99
            $config['options'] = $this->translateLabels($config['options']);
100
        }
101
102
        return $twigEnvironment->render(
103
            'IndigoCookieConsentBundle::cookie_consent.html.twig',
104
            $config
105
        );
106
    }
107
108
    /**
109
     * Translate undefined label options.
110
     *
111
     * @param array $options
112
     *
113
     * @return array
114
     */
115
    private function translateLabels(array $options)
116
    {
117
        foreach ($this->labels as $label) {
118
            $options[$label] = $this->translator->trans($options[$label], [], 'IndigoCookieConsentBundle');
119
        }
120
121
        return $options;
122
    }
123
}
124