Passed
Push — master ( 0171d8...f6fed5 )
by Nicolas
05:33
created

UpdateArticleController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Article;
6
7
use App\Entity\Article;
8
use App\Form\ArticleType;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\Form\FormFactoryInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * @Route("/api/articles/{slug}", methods={"PUT"}, name="api_articles_put")
18
 *
19
 * @Security("is_granted('ROLE_USER') and is_granted('AUTHOR', article)")
20
 */
21
final class UpdateArticleController extends AbstractController
22
{
23
    private FormFactoryInterface $formFactory;
24
    private EntityManagerInterface $entityManager;
25
26 4
    public function __construct(EntityManagerInterface $entityManager, FormFactoryInterface $formFactory)
27
    {
28 4
        $this->entityManager = $entityManager;
29 4
        $this->formFactory = $formFactory;
30 4
    }
31
32 2
    public function __invoke(Request $request, Article $article): array
33
    {
34 2
        $form = $this->formFactory->createNamed('article', ArticleType::class, $article);
35 2
        $form->submit($request->request->get('article'), false);
36
37 2
        if ($form->isValid()) {
38 1
            $this->entityManager->flush();
39
40 1
            return ['article' => $article];
41
        }
42
43 1
        return ['form' => $form];
44
    }
45
}
46