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

ShowTrickController::showOnlyId()   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\Comment;
6
use App\Entity\Trick;
7
use App\Form\CommentFormType;
8
use App\Pagination\PagePagination;
9
use App\Repository\CommentRepository;
10
use Doctrine\ORM\Tools\Pagination\Paginator;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class ShowTrickController extends AbstractController
18
{
19
20
    /**
21
     * @var CommentRepository
22
     */
23
    private $repository;
24
    /**
25
     * @var PagePagination
26
     */
27
    private $pagePagination;
28
29
    public function __construct(CommentRepository $repository, PagePagination $pagePagination)
30
    {
31
        $this->repository = $repository;
32
        $this->pagePagination = $pagePagination;
33
    }
34
35
    /**
36
     * @Route("/trick/{id}-{slug}", name="trick.show", requirements={"slug": "[a-z0-9\-]*"})
37
     */
38
    public function show(Trick $trick, string $slug, Request $request)
39
    {
40
        //Checking if slug is equal to the ID. This is for SEO and external links
41
        if ($trick->getSlug() !== $slug) {
42
            return $this->redirectToRoute('trick.show', [
43
                'id' => $trick->getId(),
44
                'slug' => $trick->getSlug()
45
            ], 301);
46
        }
47
48
        $commentForm = $this->createForm(CommentFormType::class);
49
50
        $page = $request->get('page') ?? 1;
51
52
        /** @var Paginator $tricks */
53
        $comments = $this->repository->findLatestEdited($trick->getId(), $page);
54
55
        $nextPage = $this->pagePagination->nextPage($comments, $page, Comment::NUMBER_OF_DISPLAYED_COMMENTS);
56
57
        if ($request->isXmlHttpRequest()) {
58
            $render = $this->renderView('comment/_comment-line.html.twig', [
59
                'comments' => $comments,
60
            ]);
61
            $jsonResponse = array(
62
                'render' => $render,
63
                'nextPage' => $nextPage,
64
                'nextPageUrl' => $this->generateUrl('trick.show',
65
                    array('page' => $nextPage, 'id' => $trick->getId(), 'slug' => $trick->getSlug())),
66
            );
67
68
            return new JsonResponse($jsonResponse);
69
        }
70
71
72
        return $this->render('trick/show.html.twig', [
73
            'trick' => $trick,
74
            'commentForm' => $commentForm->createView(),
75
            'actionPath' => $this->generateUrl('comment.create', ['id' => $trick->getId()]),
76
            'comments' => $comments,
77
            'nextPage' => $nextPage,
78
        ]);
79
    }
80
81
82
    /**
83
     * Adding a route with only the ID that redirects to the slugged route
84
     * @Route("/trick/{id}", name="trick.show.id", requirements={"id"="\d+"})
85
     * @return RedirectResponse
86
     */
87
    public function showOnlyId(Trick $trick)
88
    {
89
        return $this->redirectToRoute('trick.show', [
90
            'id' => $trick->getId(),
91
            'slug' => $trick->getSlug()
92
        ], 301);
93
    }
94
95
}
96