Completed
Push — master ( 37876c...34238f )
by Marko
9s
created

CreateCommentFormAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 15 2
A getCommentForm() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NewsBundle\Action;
15
16
use Sonata\NewsBundle\Form\Type\CommentType;
17
use Sonata\NewsBundle\Model\CommentManagerInterface;
18
use Sonata\NewsBundle\Model\PostInterface;
19
use Sonata\NewsBundle\Model\PostManagerInterface;
20
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
21
use Symfony\Component\Form\FormFactoryInterface;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Routing\RouterInterface;
25
26
final class CreateCommentFormAction extends Controller
27
{
28
    /**
29
     * @var RouterInterface
30
     */
31
    private $router;
32
33
    /**
34
     * @var PostManagerInterface
35
     */
36
    private $postManager;
37
38
    /**
39
     * @var CommentManagerInterface
40
     */
41
    private $commentManager;
42
43
    /**
44
     * @var FormFactoryInterface
45
     */
46
    private $formFactory;
47
48
    public function __construct(
49
        RouterInterface $router,
50
        PostManagerInterface $postManager,
51
        CommentManagerInterface $commentManager,
52
        FormFactoryInterface $formFactory
53
    ) {
54
        $this->router = $router;
55
        $this->postManager = $postManager;
56
        $this->commentManager = $commentManager;
57
        $this->formFactory = $formFactory;
58
    }
59
60
    /**
61
     * @param string $postId
62
     * @param bool   $form
63
     *
64
     * @return Response
65
     */
66
    public function __invoke($postId, $form = false)
67
    {
68
        if (!$form) {
69
            $post = $this->postManager->findOneBy([
70
                'id' => $postId,
71
            ]);
72
73
            $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...
74
        }
75
76
        return $this->render('@SonataNews/Post/comment_form.html.twig', [
77
            '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...
78
            'post_id' => $postId,
79
        ]);
80
    }
81
82
    /**
83
     * @return FormInterface
84
     */
85
    private function getCommentForm(PostInterface $post)
86
    {
87
        $comment = $this->commentManager->create();
88
        $comment->setPost($post);
89
        $comment->setStatus($post->getCommentsDefaultStatus());
90
91
        return $this->formFactory->createNamed('comment', CommentType::class, $comment, [
92
            'action' => $this->router->generate('sonata_news_add_comment', [
93
                'id' => $post->getId(),
94
            ]),
95
            'method' => 'POST',
96
        ]);
97
    }
98
}
99