Completed
Push — master ( 0b4911...4851fa )
by Valentyn
04:19 queued 11s
created

Rating   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 42
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2
ccs 19
cts 20
cp 0.95
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 34 7
A isValid() 0 4 2
1
<?php
2
3
namespace App\Filters\Movie;
4
5
use App\Filters\Filter;
6
use Doctrine\ORM\QueryBuilder;
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
9
/**
10
 * /api/movies?rf=5&rt=6 => Will display all movies with average tmdb rating
11
 */
12
class Rating implements Filter
13
{
14 31
    public function handle(ParameterBag $params, QueryBuilder $qb): QueryBuilder
15
    {
16 31
        $rating = (int)$params->get('r');
17 31
        $ratingFrom = (int)$params->get('rf');
18 31
        $ratingTo = (int)$params->get('rt');
19
20 31
        if ($ratingFrom > $ratingTo) {
21 2
            return $qb;
22
        }
23
24 29
        if ($ratingFrom && $ratingFrom === $ratingTo) {
25
            $rating = $ratingFrom;
26
        }
27
28 29
        if ($this->isValid($rating)) {
29
            return $qb
30 1
                ->andWhere('m.tmdb.voteAverage = :filter_rating')
31 1
                ->setParameter('filter_rating', $rating);
32
        }
33
34 28
        if ($this->isValid($ratingFrom)) {
35
            $qb
36 2
                ->andWhere('m.tmdb.voteAverage >= :filter_rating_from')
37 2
                ->setParameter('filter_rating_from', $ratingFrom);
38
        }
39
40 28
        if ($this->isValid($ratingTo)) {
41
            $qb
42 3
                ->andWhere('m.tmdb.voteAverage <= :filter_rating_to')
43 3
                ->setParameter('filter_rating_to', $ratingTo);
44
        }
45
46 28
        return $qb;
47
    }
48
49 29
    private function isValid(int $rating): bool
50
    {
51 29
        return !($rating < 1 || $rating > 10);
52
    }
53
}