Passed
Pull Request — master (#163)
by Matt
04:28
created

ImageController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 26
dl 0
loc 71
rs 10
c 1
b 1
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 8 1
A index() 0 18 1
A rated() 0 22 1
1
<?php
2
3
namespace App\Controller\Image;
4
5
use App\Entity\Image;
6
use App\Repository\ImageRepository;
7
use Knp\Component\Pager\PaginatorInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
 * @Route("/image", name="image_")
15
 */
16
class ImageController extends AbstractController
17
{
18
    /**
19
     * @Route("/{id}", name="show", methods={"GET"})
20
     */
21
    public function show(Image $image, ImageRepository $imageRepository): Response
22
    {
23
        $prev = $imageRepository->findPrev($image);
24
        $next = $imageRepository->findNext($image);
25
        return $this->render('image/show.html.twig', [
26
            'image' => $image,
27
            'prev' => $prev,
28
            'next' => $next
29
        ]);
30
    }
31
32
    /**
33
     * @Route("", name="index", methods={"GET"})
34
     */
35
    public function index(
36
        Request $request,
37
        ImageRepository $imageRepository,
38
        PaginatorInterface $paginator
39
    ): Response
40
    {
41
        $qb = $imageRepository->getPaginatorQueryBuilder();
42
        // TODO: Add many interesting parameters, etc.
43
        $query = $qb->getQuery();
44
45
        $pagination = $paginator->paginate(
46
            $query,
47
            $request->query->getInt('page', 1),
48
            20
49
        );
50
51
        return $this->render('image/index.html.twig', [
52
            'image_pagination' => $pagination
53
        ]);
54
55
    }
56
57
    /**
58
     * @Route(
59
     *  "/rated/{rating}",
60
     *  name="rated",
61
     *  methods={"GET"},
62
     *  requirements={"rating"="\d+"}
63
     * )
64
     */
65
    public function rated(
66
        Request $request,
67
        ImageRepository $imageRepository,
68
        PaginatorInterface $paginator,
69
        int $rating
70
    ): Response
71
    {
72
        $qb = $imageRepository->getPaginatorQueryBuilder();
73
        $qb
74
            ->andWhere('i.rating = :rating')
75
            ->setParameter('rating', $rating);
76
77
        $query = $qb->getQuery();
78
79
        $pagination = $paginator->paginate(
80
            $query,
81
            $request->query->getInt('page', 1),
82
            20
83
        );
84
85
        return $this->render('image/index.html.twig', [
86
            'image_pagination' => $pagination
87
        ]);
88
89
    }
90
}
91