DefaultEditor::renderContent()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 2
crap 3
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\Editor;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Model\Content;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author Ivo Azirjans <[email protected]>
20
 */
21
class DefaultEditor implements EditorInterface
22
{
23
    /**
24
     * @var ContainerInterface
25
     */
26
    private $container;
27
28
    /**
29
     * @var \Twig_Environment
30
     */
31
    private $twig;
32
33
    /**
34
     * @var array
35
     */
36
    private $separatelyEditableContents;
37
38
    /**
39
     * @param ContainerInterface $container
40
     */
41 4
    public function __construct(ContainerInterface $container)
42
    {
43 4
        $this->container = $container;
44 4
        $this->separatelyEditableContents = [];
45 4
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4
    public function renderContent(Content $content, array $options)
51
    {
52 4
        $text = $content->getText();
53
54 4
        if (isset($options['separately']) && $options['separately']) {
55 2
            $this->separatelyEditableContents[$content->getId()] = $content;
56
57 2
            return $text;
58
        }
59
60 3
        return sprintf(
61 3
            '<span class="ivoaz-content-editable" data-ivoaz-content-editable-id="%s">%s</span>',
62 3
            $content->getId(),
63
            $text
64 3
        );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    public function renderEditor(Response $response)
71
    {
72 2
        if (!$this->twig) {
73 2
            $this->twig = $this->container->get('twig');
74 2
        };
75
76 2
        if (!empty($this->separatelyEditableContents)) {
77 1
            $html = $this->twig->render(
78 1
                '@IvoazContentEditable/separately_editable_contents.html.twig',
79 1
                ['contents' => array_values($this->separatelyEditableContents)]
80 1
            );
81 1
        } else {
82 1
            $html = '';
83
        }
84
85 2
        $html .= $this->twig->render('@IvoazContentEditable/editor.html.twig');
86
87 2
        return $html;
88
    }
89
}
90