Passed
Branch develop (7f369c)
by Nicolas
04:24
created

ArticlesPutController::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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