Completed
Pull Request — master (#465)
by Marko
27:33
created

CreateCommentFormAction::getCommentForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Action;
13
14
use Sonata\NewsBundle\Form\Type\CommentType;
15
use Sonata\NewsBundle\Model\CommentManagerInterface;
16
use Sonata\NewsBundle\Model\PostInterface;
17
use Sonata\NewsBundle\Model\PostManagerInterface;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\RouterInterface;
23
24
final class CreateCommentFormAction extends Controller
25
{
26
    /**
27
     * @var RouterInterface
28
     */
29
    private $router;
30
31
    /**
32
     * @var PostManagerInterface
33
     */
34
    private $postManager;
35
36
    /**
37
     * @var CommentManagerInterface
38
     */
39
    private $commentManager;
40
41
    /**
42
     * @var FormFactoryInterface
43
     */
44
    private $formFactory;
45
46
    public function __construct(
47
        RouterInterface $router,
48
        PostManagerInterface $postManager,
49
        CommentManagerInterface $commentManager,
50
        FormFactoryInterface $formFactory
51
    ) {
52
        $this->router = $router;
53
        $this->postManager = $postManager;
54
        $this->commentManager = $commentManager;
55
        $this->formFactory = $formFactory;
56
    }
57
58
    /**
59
     * @param string $postId
60
     * @param bool   $form
61
     *
62
     * @return Response
63
     */
64
    public function __invoke($postId, $form = false)
65
    {
66
        if (!$form) {
67
            $post = $this->postManager->findOneBy([
68
                'id' => $postId,
69
            ]);
70
71
            $form = $this->getCommentForm($post);
0 ignored issues
show
Documentation introduced by
$post is of type object|null, but the function expects a object<Sonata\NewsBundle\Model\PostInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
        }
73
74
        return $this->render('@SonataNews/Post/comment_form.html.twig', [
75
            'form' => $form->createView(),
0 ignored issues
show
Bug introduced by
It seems like $form is not always an object, but can also be of type boolean. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
76
            'post_id' => $postId,
77
        ]);
78
    }
79
80
    /**
81
     * @return FormInterface
82
     */
83
    private function getCommentForm(PostInterface $post)
84
    {
85
        $comment = $this->commentManager->create();
86
        $comment->setPost($post);
87
        $comment->setStatus($post->getCommentsDefaultStatus());
88
89
        return $this->formFactory->createNamed('comment', CommentType::class, $comment, [
90
            'action' => $this->router->generate('sonata_news_add_comment', [
91
                'id' => $post->getId(),
92
            ]),
93
            'method' => 'POST',
94
        ]);
95
    }
96
}
97