ContentEditableExtension::getName()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Twig;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Editor\EditorInterface;
15
use Ivoaz\Bundle\ContentEditableBundle\Manager\ContentManagerInterface;
16
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
17
18
/**
19
 * @author Ivo Azirjans <[email protected]>
20
 */
21
class ContentEditableExtension extends \Twig_Extension
22
{
23
    /**
24
     * @var ContentManagerInterface
25
     */
26
    private $manager;
27
28
    /**
29
     * @var AuthorizationCheckerInterface
30
     */
31
    private $authorizationChecker;
32
33
    /**
34
     * @var EditorInterface
35
     */
36
    private $editor;
37
38
    /**
39
     * @param ContentManagerInterface       $manager
40
     * @param AuthorizationCheckerInterface $authorizationChecker
41
     * @param EditorInterface               $editor
42
     */
43 2
    public function __construct(
44
        ContentManagerInterface $manager,
45
        AuthorizationCheckerInterface $authorizationChecker,
46
        EditorInterface $editor
47
    ) {
48 2
        $this->manager = $manager;
49 2
        $this->authorizationChecker = $authorizationChecker;
50 2
        $this->editor = $editor;
51 2
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getFilters()
57
    {
58
        return [
59
            new \Twig_SimpleFilter(
60
                'contenteditable',
61
                [$this, 'render'],
62
                ['pre_escape' => 'html', 'is_safe' => ['html']]
63
            ),
64
        ];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getTokenParsers()
71
    {
72
        return [
73
            new ContentEditableTokenParser(),
74
        ];
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function render($default, $name = null, array $options = [])
81
    {
82 2
        if (null === $name) {
83
            $name = $default;
84
        }
85
86 2
        $locale = isset($options['locale']) ? $options['locale'] : null;
87 2
        $content = $this->manager->get($name, $locale, $default);
88
89 2
        if (!$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
90 1
            return $content->getText();
91
        }
92
93 1
        return $this->editor->renderContent($content, $options);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getName()
100
    {
101
        return 'ivoaz_content_editable';
102
    }
103
}
104