Completed
Branch develop (7f369c)
by Nicolas
04:28
created

ArticlesFeedController::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 1
    public function __construct(ArticleRepository $repository)
33
    {
34 1
        $this->repository = $repository;
35 1
    }
36
37
    /**
38
     * @param UserInterface $user
39
     * @param ParamFetcher  $paramFetcher
40
     *
41
     * @return array
42
     */
43 1
    public function __invoke(UserInterface $user, ParamFetcher $paramFetcher)
44
    {
45
        /** @var User $user */
46 1
        $articles = $this->repository->getFollowedUsersArticles(
47 1
            $user,
48 1
            (int) $paramFetcher->get('offset'),
49 1
            (int) $paramFetcher->get('limit')
50
        );
51
52 1
        $articlesCount = count($articles);
53
54
        return [
55 1
            'articles' => $articles,
56 1
            'articlesCount' => $articlesCount,
57
        ];
58
    }
59
}
60