Passed
Push — develop ( 398719...d50203 )
by BENARD
05:23
created

GetStats::getPlayerGameStats()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller\Player\Game;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\Intl\Locale;
8
use VideoGamesRecords\CoreBundle\Entity\Player;
9
10
class GetStats extends AbstractController
11
{
12
    protected EntityManagerInterface $em;
13
14
    public function __construct(EntityManagerInterface $em)
15
    {
16
        $this->em = $em;
17
    }
18
19
    /**
20
     * @param Player    $player
21
     * @return array
22
     */
23
    public function __invoke(Player $player): array
24
    {
25
        $playerGames = $this->getPlayerGameStats($player);
26
        $stats = $this->getStatusPerGame($player);
27
28
        foreach ($playerGames as $playerGame) {
29
            if (isset($stats[$playerGame->getGame()->getId()])) {
30
                $playerGame->setStatuses($stats[$playerGame->getGame()->getId()]);
31
            }
32
        }
33
        return $playerGames;
34
    }
35
36
     /**
37
     * Return data from player with game and platforms
38
     *
39
     * @param $player
40
     * @return array
41
     */
42
    private function getPlayerGameStats($player): array
43
    {
44
        $qb = $this->em->createQueryBuilder()
45
            ->select('pg')
46
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerGame', 'pg')
47
            ->join('pg.game', 'g')
48
            ->addSelect('g')
49
            ->join('g.platforms', 'p')
50
            ->addSelect('p')
51
            ->where('pg.player = :player')
52
            ->setParameter('player', $player)
53
            ->orderBy('g.' . (Locale::getDefault() == 'fr' ? 'libGameFr' : 'libGameEn'), 'ASC');
54
55
        return $qb->getQuery()->getResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $qb->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...
56
    }
57
58
    /**
59
     * @param $player
60
     * @return array
61
     */
62
    private function getStatusPerGame($player): array
63
    {
64
        $qb = $this->em->createQueryBuilder()
65
            ->select('gam.id')
66
            ->from('VideoGamesRecords\CoreBundle\Entity\Game', 'gam')
67
            ->addSelect('status.id as idStatus')
68
            ->addSelect('COUNT(pc) as nb')
69
            ->innerJoin('gam.groups', 'grp')
70
            ->innerJoin('grp.charts', 'chr')
71
            ->innerJoin('chr.playerCharts', 'pc')
72
            ->innerJoin('pc.status', 'status')
73
            ->where('pc.player = :player')
74
            ->setParameter('player', $player)
75
            ->groupBy('gam.id')
76
            ->addGroupBy('status.id')
77
            ->orderBy('gam.id', 'ASC')
78
            ->addOrderBy('status.id', 'ASC');
79
80
        $list = $qb->getQuery()->getResult(2);
81
82
        $games = [];
83
        foreach ($list as $row) {
84
            $idGame = $row['id'];
85
            if (!array_key_exists($idGame, $games)) {
86
                $games[$idGame] = [];
87
            }
88
            $games[$idGame][] = [
89
                'status' => $this->em->find('VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus', $row['idStatus']),
90
                'nb' => $row['nb'],
91
            ];
92
        }
93
        return $games;
94
    }
95
}
96