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

ArticlesFavoriteController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 1
A __construct() 0 3 1
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Entity\Article;
6
use App\Entity\User;
7
use Doctrine\ORM\EntityManagerInterface;
8
use FOS\RestBundle\Controller\Annotations\View;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use Symfony\Component\Routing\Annotation\Route;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
/**
14
 * ArticlesFavoriteController.
15
 *
16
 * @Route("/api/articles/{slug}/favorites", name="api_article_favorite")
17
 * @Method("POST")
18
 * @View(statusCode=200)
19
 */
20
class ArticlesFavoriteController
21
{
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    protected $manager;
26
27
    /**
28
     * @param EntityManagerInterface $manager
29
     */
30 1
    public function __construct(EntityManagerInterface $manager)
31
    {
32 1
        $this->manager = $manager;
33 1
    }
34
35
    /**
36
     * @param UserInterface $user
37
     * @param Article       $article
38
     *
39
     * @return array
40
     */
41 1
    public function __invoke(UserInterface $user, Article $article)
42
    {
43
        /* @var User $user */
44
45 1
        $user->addToFavorites($article);
46 1
        $this->manager->flush();
47
48 1
        return ['article' => $article];
49
    }
50
}
51