Passed
Push — master ( d346aa...fd2cec )
by Nicolas
04:54
created

UpdateArticleController::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Article;
4
5
use App\Entity\Article;
6
use App\Form\ArticleType;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
10
use Symfony\Component\Form\FormFactoryInterface;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/api/articles/{slug}", name="api_articles_put")
17
 * @Method("PUT")
18
 *
19
 * @Security("is_granted('ROLE_USER') and is_granted('AUTHOR', article)")
20
 */
21
final class UpdateArticleController
22
{
23
    /**
24
     * @var FormFactoryInterface
25
     */
26
    private $formFactory;
27
28
    /**
29
     * @var EntityManagerInterface
30
     */
31
    private $entityManager;
32
33
    /**
34
     * @param FormFactoryInterface   $factory
35
     * @param EntityManagerInterface $manager
36
     */
37 4
    public function __construct(FormFactoryInterface $factory, EntityManagerInterface $manager)
38
    {
39 4
        $this->formFactory = $factory;
40 4
        $this->entityManager = $manager;
41 4
    }
42
43
    /**
44
     * @param Request $request
45
     * @param Article $article
46
     *
47
     * @return array|FormInterface
48
     */
49 2
    public function __invoke(Request $request, Article $article)
50
    {
51 2
        $form = $this->formFactory->createNamed('article', ArticleType::class, $article);
52 2
        $form->submit($request->request->get('article'), false);
53
54 2
        if ($form->isValid()) {
55 1
            $this->entityManager->flush();
56
57 1
            return ['article' => $article];
58
        }
59
60 1
        return $form;
61
    }
62
}
63