Passed
Push — develop ( e920eb...9cd2fd )
by BENARD
05:00
created

PlayerSerieRankingProvider::getRankingMedals()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 39
rs 8.8817
c 0
b 0
f 0
cc 6
nc 13
nop 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Provider\Player;
4
5
use Doctrine\ORM\Exception\ORMException;
6
use VideoGamesRecords\CoreBundle\Entity\Player;
7
use VideoGamesRecords\CoreBundle\Entity\PlayerSerie;
8
use VideoGamesRecords\CoreBundle\Entity\Serie;
9
use VideoGamesRecords\CoreBundle\Ranking\Provider\AbstractRankingProvider;
10
11
class PlayerSerieRankingProvider 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
        $player = $this->getPlayer();
28
        $limit = $options['limit'] ?? null;
29
30
        $query = $this->em->createQueryBuilder()
31
            ->select('ps')
32
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerSerie', 'ps')
33
            ->join('ps.player', 'p')
34
            ->addSelect('p')
35
            ->orderBy('ps.rankPointChart');
36
37
        $query->where('ps.serie = :serie')
38
            ->setParameter('serie', $serie);
39
40
        /** @var PlayerSerie $row */
41
        $row = (null !== $player) ? $this->getRow($serie, $player) : null;
42
43
        if (null !== $maxRank) {
44
            if (null !== $row) {
45
                $query->andWhere('(ps.rankPointChart <= :maxRank OR ps.rankPointChart BETWEEN :min AND :max)')
46
                    ->setParameter('min', $row->getRankPointChart() - 5)
47
                    ->setParameter('max', $row->getRankPointChart() + 5);
48
            } else {
49
                $query->andWhere('ps.rankPointChart <= :maxRank');
50
            }
51
            $query->setParameter('maxRank', $maxRank);
52
        }
53
54
        if (null !== $limit) {
55
            $query->setMaxResults($limit);
56
        }
57
58
        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...
59
    }
60
61
    /**
62
     * @param int|null $id
63
     * @param array $options
64
     * @return array
65
     * @throws ORMException
66
     */
67
    public function getRankingMedals(int $id = null, array $options = []): array
68
    {
69
        $serie = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Serie')->find($id);
70
        if (null === $serie) {
71
            return [];
72
        }
73
74
        $maxRank = $options['maxRank'] ?? null;
75
        $player = $this->getPlayer();
76
        $limit = $options['limit'] ?? null;
77
78
        $query = $this->em->createQueryBuilder()
79
            ->select('ps')
80
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerSerie', 'ps')
81
            ->join('ps.player', 'p')
82
            ->addSelect('p')
83
            ->orderBy('ps.rankMedal');
84
85
        $query->where('ps.serie = :serie')
86
            ->setParameter('serie', $serie);
87
88
        $row = (null !== $player) ? $this->getRow($serie, $player) : null;
89
90
        if (null !== $maxRank) {
91
            if (null !== $row) {
92
                $query->andWhere('(ps.rankMedal <= :maxRank OR ps.rankMedal BETWEEN :min AND :max)')
93
                    ->setParameter('min', $row->getRankPoint() - 5)
94
                    ->setParameter('max', $row->getRankPoint() + 5);
95
            } else {
96
                $query->andWhere('ps.rankMedal <= :maxRank');
97
            }
98
            $query->setParameter('maxRank', $maxRank);
99
        }
100
101
        if (null !== $limit) {
102
            $query->setMaxResults($limit);
103
        }
104
105
        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...
106
    }
107
108
    private function getRow(Serie $serie, Player $player)
109
    {
110
        return $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\PlayerSerie')->findOneBy(
111
            [
112
                'serie' => $serie,
113
                'player' => $player,
114
            ]
115
        );
116
    }
117
}
118