GetArticlesListController::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 1
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Article;
6
7
use App\Repository\ArticleRepository;
8
use FOS\RestBundle\Controller\Annotations\QueryParam;
9
use FOS\RestBundle\Request\ParamFetcher;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
 * @Route("/api/articles", methods={"GET"}, name="api_articles_list")
15
 *
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
final class GetArticlesListController extends AbstractController
23
{
24
    private ArticleRepository $articleRepository;
25
26 6
    public function __construct(ArticleRepository $repository)
27
    {
28 6
        $this->articleRepository = $repository;
29
    }
30
31 6
    public function __invoke(ParamFetcher $paramFetcher): array
32
    {
33 6
        $articlesCount = $this->articleRepository->getArticlesListCount(
34 6
            $paramFetcher->get('tag'),
35 6
            $paramFetcher->get('author'),
36 6
            $paramFetcher->get('favorited')
37
        );
38
39 6
        $articles = $this->articleRepository->getArticlesList(
40 6
            (int) $paramFetcher->get('offset'),
41 6
            (int) $paramFetcher->get('limit'),
42 6
            $paramFetcher->get('tag'),
43 6
            $paramFetcher->get('author'),
44 6
            $paramFetcher->get('favorited')
45
        );
46
47
        return [
48 6
            'articlesCount' => $articlesCount,
49
            'articles' => $articles,
50
        ];
51
    }
52
}
53