Passed
Push — master ( 9907da...28a731 )
by Nicolas
05:26
created

CreateCommentController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Comment;
6
7
use App\Controller\AbstractController;
8
use App\Entity\Article;
9
use App\Entity\Comment;
10
use App\Form\CommentType;
11
use FOS\RestBundle\Controller\Annotations\View;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * @Route("/api/articles/{slug}/comments", methods={"POST"}, name="api_comment_post")
18
 *
19
 * @View(statusCode=201)
20
 *
21
 * @Security("is_granted('ROLE_USER')")
22
 */
23
final class CreateCommentController extends AbstractController
24
{
25 3
    public function __invoke(Request $request, Article $article): array
26
    {
27 3
        $user = $this->getCurrentUser();
28
29 3
        $comment = new Comment();
30 3
        $comment->setAuthor($user);
31 3
        $comment->setArticle($article);
32
33 3
        $form = $this->createNamedForm('comment', CommentType::class, $comment);
34 3
        $form->submit($request->request->get('comment'));
35
36 3
        if ($form->isValid()) {
37 1
            $this->getDoctrine()->getManager()->persist($comment);
38 1
            $this->getDoctrine()->getManager()->flush();
39
40 1
            return ['comment' => $comment];
41
        }
42
43 2
        return ['form' => $form];
44
    }
45
}
46