Passed
Branch develop (7f369c)
by Nicolas
05:00
created

ArticlesFeedController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 14 1
A __construct() 0 3 1
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Entity\User;
6
use App\Repository\ArticleRepository;
7
use FOS\RestBundle\Controller\Annotations\QueryParam;
8
use FOS\RestBundle\Request\ParamFetcher;
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
 * ArticlesFeedController.
15
 *
16
 * @Route("/api/articles/feed", name="api_articles_feed")
17
 * @Method("GET")
18
 *
19
 * @QueryParam(name="limit", requirements="\d+", default="20")
20
 * @QueryParam(name="offset", requirements="\d+", default="0")
21
 */
22
class ArticlesFeedController
23
{
24
    /**
25
     * @var ArticleRepository
26
     */
27
    private $repository;
28
29
    /**
30
     * @param ArticleRepository $repository
31
     */
32
    public function __construct(ArticleRepository $repository)
33
    {
34
        $this->repository = $repository;
35
    }
36
37
    /**
38
     * @param UserInterface $user
39
     * @param ParamFetcher  $paramFetcher
40
     *
41
     * @return array
42
     */
43
    public function __invoke(UserInterface $user, ParamFetcher $paramFetcher)
44
    {
45
        /** @var User $user */
46
        $articles = $this->repository->getFollowedUsersArticles(
47
            $user,
48
            (int) $paramFetcher->get('offset'),
49
            (int) $paramFetcher->get('limit')
50
        );
51
52
        $articlesCount = count($articles);
53
54
        return [
55
            'articles' => $articles,
56
            'articlesCount' => $articlesCount,
57
        ];
58
    }
59
}
60