Completed
Pull Request — master (#32)
by
unknown
29:38
created

CommentFormFactoryTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) Sulu GmbH
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 Sulu\Bundle\CommentBundle\Twig;
13
14
use Sulu\Bundle\CommentBundle\Form\Type\CommentType;
15
use Symfony\Component\Form\FormFactoryInterface;
16
use Symfony\Component\Form\FormView;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFunction;
19
20
class CommentFormFactoryTwigExtension extends AbstractExtension
21
{
22
    /**
23
     * @var FormFactoryInterface
24
     */
25
    private $formFactory;
26
27
    /**
28
     * @var string
29
     */
30
    private $commentClass;
31
32
    public function __construct(FormFactoryInterface $formFactory, string $commentClass)
33
    {
34
        $this->formFactory = $formFactory;
35
        $this->commentClass = $commentClass;
36
    }
37
38
    public function getFunctions()
39
    {
40
        return [
41
            new TwigFunction('sulu_create_comment_form', [$this, 'createCommentForm']),
42
        ];
43
    }
44
45
    public function createCommentForm(string $threadId, ?string $referrer = null, ?int $parent = null): FormView
46
    {
47
        $form = $this->formFactory->create(
48
            CommentType::class,
49
            null,
50
            [
51
                'data_class' => $this->commentClass,
52
                'threadId' => $threadId,
53
                'referrer' => $referrer,
54
                'parent' => $parent,
55
            ]
56
        );
57
58
        return $form->createView();
59
    }
60
}
61