Passed
Push — develop ( 1358ff...4e8999 )
by BENARD
12:33
created

TeamSerieRankingProvider::getRankingMedals()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 39
rs 8.8817
cc 6
nc 13
nop 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Team;
4
5
use Doctrine\ORM\Exception\ORMException;
6
use VideoGamesRecords\CoreBundle\Entity\Team;
7
use VideoGamesRecords\CoreBundle\Entity\TeamSerie;
8
use VideoGamesRecords\CoreBundle\Entity\Serie;
9
use VideoGamesRecords\CoreBundle\Ranking\Provider\AbstractRankingProvider;
10
11
class TeamSerieRankingProvider extends AbstractRankingProvider
12
{
13
    /**
14
     * @param int|null $id
15
     * @param array $options
16
     * @return array
17
     * @throws ORMException
18
     */
19
    public function getRankingPoints(int $id = null, array $options = []): array
20
    {
21
        $serie = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->find($id);
22
        if (null === $serie) {
23
            return [];
24
        }
25
26
        $maxRank = $options['maxRank'] ?? null;
27
        $limit = $options['limit'] ?? null;
28
29
        $query = $this->em->createQueryBuilder()
30
            ->select('ts')
31
            ->from('VideoGamesRecords\CoreBundle\Entity\TeamSerie', 'ts')
32
            ->join('ts.team', 't')
33
            ->addSelect('t')
34
            ->orderBy('ts.rankPointChart');
35
36
        $query->where('ts.serie = :serie')
37
            ->setParameter('serie', $serie);
38
39
        if (null !== $maxRank) {
40
            $query->andWhere('ts.rankPointChart <= :maxRank');
41
            $query->setParameter('maxRank', $maxRank);
42
        }
43
44
        if (null !== $limit) {
45
            $query->setMaxResults($limit);
46
        }
47
48
        return $query->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
49
    }
50
51
    /**
52
     * @param int|null $id
53
     * @param array $options
54
     * @return array
55
     * @throws ORMException
56
     */
57
    public function getRankingMedals(int $id = null, array $options = []): array
58
    {
59
        $serie = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->find($id);
60
        if (null === $serie) {
61
            return [];
62
        }
63
64
        $maxRank = $options['maxRank'] ?? null;
65
        $player = $this->getPlayer();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $player is correct as $this->getPlayer() targeting VideoGamesRecords\CoreBu...ngProvider::getPlayer() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
        $limit = $options['limit'] ?? null;
67
68
        $query = $this->em->createQueryBuilder()
69
            ->select('ps')
70
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerSerie', 'ps')
71
            ->join('ps.player', 'p')
72
            ->addSelect('p')
73
            ->orderBy('ps.rankMedal');
74
75
        $query->where('ps.serie = :serie')
76
            ->setParameter('serie', $serie);
77
78
        $row = (null !== $player) ? $this->getRow($serie, $player) : null;
0 ignored issues
show
introduced by
The condition null !== $player is always false.
Loading history...
79
80
        if (null !== $maxRank) {
81
            if (null !== $row) {
0 ignored issues
show
introduced by
The condition null !== $row is always false.
Loading history...
82
                $query->andWhere('(ps.rankMedal <= :maxRank OR ps.rankMedal BETWEEN :min AND :max)')
83
                    ->setParameter('min', $row->getRankPoint() - 5)
84
                    ->setParameter('max', $row->getRankPoint() + 5);
85
            } else {
86
                $query->andWhere('ps.rankMedal <= :maxRank');
87
            }
88
            $query->setParameter('maxRank', $maxRank);
89
        }
90
91
        if (null !== $limit) {
92
            $query->setMaxResults($limit);
93
        }
94
95
        return $query->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
96
    }
97
98
    private function getRow(Serie $serie, Team $team)
99
    {
100
        return $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\TeamSerie')->findOneBy(
101
            [
102
                'serie' => $serie,
103
                'team' => $team,
104
            ]
105
        );
106
    }
107
}
108