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

UpdateArticleController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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\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