Passed
Branch master (1e4cd6)
by Stone
05:30
created

TricksByTagController::tagOnlyId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Controller\Trick;
4
5
use App\Entity\Tag;
6
use App\Entity\Trick;
7
use App\Pagination\PagePagination;
8
use App\Repository\TrickRepository;
9
use Doctrine\ORM\Tools\Pagination\Paginator;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class TricksByTagController extends AbstractController
18
{
19
20
    /**
21
     * @var TrickRepository
22
     */
23
    private $trickRepository;
24
25
    /**
26
     * @var PagePagination
27
     */
28
    private $pagePagination;
29
30
    public function __construct(TrickRepository $trickRepository, PagePagination $pagePagination)
31
    {
32
        $this->trickRepository = $trickRepository;
33
        $this->pagePagination = $pagePagination;
34
    }
35
36
    /**
37
     * @Route("/trick/tag/{id}-{slug}", name="trick.tag", methods={"GET"})
38
     * @param Tag $tag
39
     * @param string $slug
40
     * @return RedirectResponse|Response
41
     */
42
    public function showTricksByTag(Request $request, Tag $tag, string $slug)
43
    {
44
        if ($tag->getSlug() !== $slug) {
45
            return $this->redirectToRoute('trick.tag', [
46
                'id' => $tag->getId(),
47
                'slug' => $tag->getSlug()
48
            ], 301);
49
        }
50
51
        $page = $request->get('page') ?? 1;
52
53
        /** @var Paginator $tricks */
54
        $tricks = $this->trickRepository->findLatestEditedByTag($page, $tag->getId());
55
56
        $nextPage = $this->pagePagination->nextPage($tricks, $page, Trick::NUMBER_OF_DISPLAYED_TRICKS);
57
58
        if ($request->isXmlHttpRequest()) {
59
            $render = $this->renderView('trick/_trick-card.html.twig', [
60
                'tricks' => $tricks,
61
            ]);
62
            $jsonResponse = array(
63
                'render' => $render,
64
                'nextPage' => $nextPage,
65
                'nextPageUrl' => $this->generateUrl(
66
                    'trick.tag',
67
                    array(
68
                        'page' => $nextPage,
69
                        'id' => $tag->getId(),
70
                        'slug' => $slug,
71
                    )
72
                ),
73
            );
74
75
            return new JsonResponse($jsonResponse);
76
        }
77
78
        return $this->render('trick/tag.html.twig', [
79
            'tag' => $tag,
80
            'tricks' => $tricks,
81
            'tagId' => $tag->getId(),
82
            'slug' => $slug,
83
            'page' => $page,
84
            'nextPage' => $nextPage,
85
        ]);
86
    }
87
88
    /**
89
     * @Route("/trick/tag/{id}", name="trick.tag.id", methods={"GET"}, requirements={"id"="\d+"})
90
     */
91
    public function tagOnlyId(Tag $tag)
92
    {
93
        return $this->redirectToRoute('trick.tag', [
94
            'id' => $tag->getId(),
95
            'slug' => $tag->getSlug()
96
        ], 301);
97
    }
98
}