HomeController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 20
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 26 2
A __construct() 0 4 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Trick;
6
use App\Pagination\PagePagination;
7
use App\Repository\TrickRepository;
8
use Doctrine\ORM\Tools\Pagination\Paginator;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
class HomeController extends AbstractController
16
{
17
18
    /**
19
     * @var TrickRepository
20
     */
21
    private $repository;
22
    /**
23
     * @var PagePagination
24
     */
25
    private $pagePagination;
26
27
    public function __construct(TrickRepository $repository, PagePagination $pagePagination)
28
    {
29
        $this->repository = $repository;
30
        $this->pagePagination = $pagePagination;
31
    }
32
33
    /**
34
     * @Route("/", name="home")
35
     */
36
    public function index(Request $request)
37
    {
38
        $page = $request->get('page') ?? 1;
39
40
        /** @var Paginator $tricks */
41
        $tricks = $this->repository->findLatestEdited($page);
42
43
        $nextPage = $this->pagePagination->nextPage($tricks, $page, Trick::NUMBER_OF_DISPLAYED_TRICKS);
44
45
        if ($request->isXmlHttpRequest()) {
46
            $render = $this->renderView('trick/_trick-card.html.twig', [
47
                'tricks' => $tricks,
48
            ]);
49
            $jsonResponse = array(
50
                'render' => $render,
51
                'nextPage' => $nextPage,
52
                'nextPageUrl' => $this->generateUrl('home', array('page' => $nextPage)),
53
            );
54
55
            return new JsonResponse($jsonResponse);
56
        }
57
58
        return $this->render('trick/index.html.twig', [
59
            'tricks' => $tricks,
60
            'page' => $page,
61
            'nextPage' => $nextPage,
62
        ]);
63
    }
64
}