Test Failed
Branch develop (7f369c)
by Nicolas
04:26
created

ArticlesListController::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Repository\ArticleRepository;
6
use FOS\RestBundle\Controller\Annotations\QueryParam;
7
use FOS\RestBundle\Request\ParamFetcher;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Symfony\Component\Routing\Annotation\Route;
10
11
/**
12
 * ArticlesListController.
13
 *
14
 * @Route("/api/articles", name="api_articles_list")
15
 * @Method("GET")
16
 * @QueryParam(name="tag", requirements="[A-Za-z]+", nullable=true)
17
 * @QueryParam(name="author", requirements="[A-Za-z0-9]+", nullable=true)
18
 * @QueryParam(name="favorited", requirements="[A-Za-z0-9]+", nullable=true)
19
 * @QueryParam(name="limit", requirements="\d+", default="20")
20
 * @QueryParam(name="offset", requirements="\d+", default="0")
21
 */
22
class ArticlesListController
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 ParamFetcher $paramFetcher
39
     *
40
     * @return array
41
     */
42
    public function __invoke(ParamFetcher $paramFetcher)
43
    {
44
        $articles = $this->repository->getArticles(
45
            (int) $paramFetcher->get('offset'),
46
            (int) $paramFetcher->get('limit'),
47
            $paramFetcher->get('tag'),
48
            $paramFetcher->get('author'),
49
            $paramFetcher->get('favorited')
50
        );
51
52
        $articlesCount = count($articles);
53
54
        return [
55
            'articles' => $articles,
56
            'articlesCount' => $articlesCount,
57
        ];
58
    }
59
}
60