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

CreateArticleController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Article;
6
7
use App\Controller\AbstractController;
8
use App\Entity\Article;
9
use App\Form\ArticleType;
10
use FOS\RestBundle\Controller\Annotations\View;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/api/articles", methods={"POST"}, name="api_articles_post")
17
 *
18
 * @View(statusCode=201)
19
 *
20
 * @Security("is_granted('ROLE_USER')")
21
 */
22
final class CreateArticleController extends AbstractController
23
{
24 2
    public function __invoke(Request $request): array
25
    {
26 2
        $user = $this->getCurrentUser();
27
28 2
        $article = new Article();
29 2
        $article->setAuthor($user);
30
31 2
        $form = $this->createNamedForm('article', ArticleType::class, $article);
32 2
        $form->submit($request->request->get('article'));
33
34 2
        if ($form->isValid()) {
35 1
            $this->getDoctrine()->getManager()->persist($article);
36 1
            $this->getDoctrine()->getManager()->flush();
37
38 1
            return ['article' => $article];
39
        }
40
41 1
        return ['form' => $form];
42
    }
43
}
44