Passed
Pull Request — master (#164)
by Matt
06:03
created

ImageController::filterQueryByRating()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 4
nop 2
dl 0
loc 19
rs 9.4555
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Image;
4
5
use App\Entity\Image;
6
use App\Form\ImageFilterType;
7
use App\Repository\ImageRepository;
8
use Carbon\Carbon;
9
use Doctrine\ORM\QueryBuilder;
10
use Knp\Component\Pager\PaginatorInterface;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\InputBag;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * @Route("/image", name="image_")
19
 */
20
class ImageController extends AbstractController
21
{
22
    /**
23
     * @Route("/{id}", name="show", methods={"GET"})
24
     */
25
    public function show(Image $image, ImageRepository $imageRepository): Response
26
    {
27
        $prev = $imageRepository->findPrev($image);
28
        $next = $imageRepository->findNext($image);
29
        return $this->render('image/show.html.twig', [
30
            'image' => $image,
31
            'prev' => $prev,
32
            'next' => $next
33
        ]);
34
    }
35
36
    /**
37
     * @Route("", name="index", methods={"GET"})
38
     */
39
    public function index(
40
        Request $request,
41
        ImageRepository $imageRepository,
42
        PaginatorInterface $paginator
43
    ): Response {
44
45
        $qb = $imageRepository->getReversePaginatorQueryBuilder();
46
47
        $this->filterQueryByYearAndMonth($request->query, $qb);
48
        $this->filterQueryByRating($request->query, $qb);
49
50
        if ($request->query->has('location')) {
51
            $qb->andWhere('i.location = :location')
52
                ->setParameter('location', $request->query->get('location'));
53
        }
54
55
        $query = $qb->getQuery();
56
57
        $page = $request->query->getInt('page', 1);
58
        $pagination = $paginator->paginate(
59
            $query,
60
            $page,
61
            20
62
        );
63
64
        return $this->render('image/index.html.twig', [
65
            'image_pagination' => $pagination
66
        ]);
67
    }
68
69
    private function filterQueryByYearAndMonth(InputBag $params, QueryBuilder &$qb): QueryBuilder {
70
        if ($params->has('year')) {
71
            /** @var int $year */
72
            $year = $params->getInt('year', (int) date("Y"));
73
            // We can optionally have a month to limit the date range
74
            // even further.
75
            $month = $params->getInt('month', 0);
76
            if ($month < 1 || $month > 12) {
77
                // Just the year
78
                $startDate = Carbon::create($year, 1, 1);
79
                $endDate = Carbon::create($year + 1, 1, 1);
80
            } else {
81
                // We're looking at a particular month of the year
82
                /** @var Carbon $startDate */
83
                $startDate = Carbon::create($year, $month, 1);
84
                $endDate = $startDate->copy()->addMonths(1);
85
            }
86
            $qb->andWhere('i.capturedAt >= :startDate')
87
                ->setParameter('startDate', $startDate)
88
                ->andWhere('i.capturedAt < :endDate')
89
                ->setParameter('endDate', $endDate);
90
        }
91
        return $qb;
92
    }
93
94
    private function filterQueryByRating(InputBag $params, QueryBuilder &$qb): QueryBuilder {
95
        $rating = $params->getInt('rating', -1);
96
        if ($rating >= 0 && $rating <= 5) {
97
            $ratingCompare = $params->get('rating_compare', 'eq');
98
            switch ($ratingCompare) {
99
                case 'lte':
100
                    $qb->andWhere($qb->expr()->lte('i.rating', ':rating'));
101
                    break;
102
                case 'gte':
103
                    $qb->andWhere($qb->expr()->gte('i.rating', ':rating'));
104
                    break;
105
                default:
106
                    // 'eq'
107
                    $qb->andWhere($qb->expr()->eq('i.rating', ':rating'));
108
                    break;
109
            }
110
            $qb->setParameter('rating', $rating);
111
        }
112
        return $qb;
113
    }
114
}
115