TricksByTagController::showTricksByTag()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 40
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
nop 3
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->correctSlug($tag);
46
        }
47
48
        $page = $request->get('page') ?? 1;
49
50
        /** @var Paginator $tricks */
51
        $tricks = $this->trickRepository->findLatestEditedByTag($page, $tag->getId());
52
53
        $nextPage = $this->pagePagination->nextPage($tricks, $page, Trick::NUMBER_OF_DISPLAYED_TRICKS);
54
55
        if ($request->isXmlHttpRequest()) {
56
            $render = $this->renderView('trick/_trick-card.html.twig', [
57
                'tricks' => $tricks,
58
            ]);
59
            $jsonResponse = array(
60
                'render' => $render,
61
                'nextPage' => $nextPage,
62
                'nextPageUrl' => $this->generateUrl(
63
                    'trick.tag',
64
                    array(
65
                        'page' => $nextPage,
66
                        'id' => $tag->getId(),
67
                        'slug' => $slug,
68
                    )
69
                ),
70
            );
71
72
            return new JsonResponse($jsonResponse);
73
        }
74
75
        return $this->render('trick/tag.html.twig', [
76
            'tag' => $tag,
77
            'tricks' => $tricks,
78
            'tagId' => $tag->getId(),
79
            'slug' => $slug,
80
            'page' => $page,
81
            'nextPage' => $nextPage,
82
        ]);
83
    }
84
85
    /**
86
     * @Route("/trick/tag/{id}", name="trick.tag.id", methods={"GET"}, requirements={"id"="\d+"})
87
     */
88
    public function tagOnlyId(Tag $tag)
89
    {
90
        return $this->correctSlug($tag);
91
    }
92
93
    /**
94
     * Redirecting to the correct url
95
     * @param Tag $tag
96
     * @return RedirectResponse
97
     */
98
    private function correctSlug(Tag $tag)
99
    {
100
        return $this->redirectToRoute('trick.tag', [
101
            'id' => $tag->getId(),
102
            'slug' => $tag->getSlug()
103
        ], 301);
104
    }
105
}