CreateCommentFormAction   A
last analyzed

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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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
Bug introduced by
It seems like $post defined by $this->postManager->find...array('id' => $postId)) on line 69 can also be of type null; however, Sonata\NewsBundle\Action...ction::getCommentForm() does only seem to accept object<Sonata\NewsBundle\Model\PostInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

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