Test Failed
Push — master ( 8af420...1fd71c )
by Nicolas
03:42
created

GetArticlesListController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 19 1
1
<?php
2
3
namespace App\Controller\Article;
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
 * @Route("/api/articles", name="api_articles_list")
13
 * @Method("GET")
14
 *
15
 * @QueryParam(name="tag", requirements="[A-Za-z]+", nullable=true)
16
 * @QueryParam(name="author", requirements="[A-Za-z0-9]+", nullable=true)
17
 * @QueryParam(name="favorited", requirements="[A-Za-z0-9]+", nullable=true)
18
 * @QueryParam(name="limit", requirements="\d+", default="20")
19
 * @QueryParam(name="offset", requirements="\d+", default="0")
20
 */
21
final class GetArticlesListController
22
{
23
    /**
24
     * @var ArticleRepository
25
     */
26
    private $articleRepository;
27
28
    /**
29
     * @param ArticleRepository $repository
30
     */
31 6
    public function __construct(ArticleRepository $repository)
32
    {
33 6
        $this->articleRepository = $repository;
34 6
    }
35
36
    /**
37
     * @param ParamFetcher $paramFetcher
38
     *
39
     * @return array
40
     */
41 6
    public function __invoke(ParamFetcher $paramFetcher)
42
    {
43 6
        $articlesCount = $this->articleRepository->getArticlesListCount(
44 6
            $paramFetcher->get('tag'),
45 6
            $paramFetcher->get('author'),
46 6
            $paramFetcher->get('favorited')
47
        );
48
49 6
        $articles = $this->articleRepository->getArticlesList(
50 6
            (int) $paramFetcher->get('offset'),
51 6
            (int) $paramFetcher->get('limit'),
52 6
            $paramFetcher->get('tag'),
53 6
            $paramFetcher->get('author'),
54 6
            $paramFetcher->get('favorited')
55
        );
56
57
        return [
58 6
            'articles' => $articles,
59 6
            'articlesCount' => $articlesCount,
60
        ];
61
    }
62
}
63