Passed
Push — develop ( f1c6f2...a62e64 )
by BENARD
06:18 queued 02:00
created

PlayerGameStatsProvider::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\DataProvider;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\Intl\Locale;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Intl\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class PlayerGameStatsProvider
9
{
10
    private EntityManagerInterface $em;
11
12
    public function __construct(EntityManagerInterface $em)
13
    {
14
        $this->em = $em;
15
    }
16
17
    /**
18
     * @param $mixed
19
     * @return array
20
     */
21
    public function load($mixed): array
22
    {
23
        $playerGames = $this->getPlayerGameStats($mixed);
24
        $stats = $this->getStatusPerGame($mixed);
25
26
        foreach ($playerGames as $playerGame) {
27
            if (isset($stats[$playerGame->getGame()->getId()])) {
28
                $playerGame->setStatuses($stats[$playerGame->getGame()->getId()]);
29
            }
30
        }
31
        return $playerGames;
32
    }
33
34
    /**
35
     * Return data from player with game and platforms
36
     *
37
     * @param $player
38
     * @return array
39
     */
40
    private function getPlayerGameStats($player): array
41
    {
42
        $qb = $this->em->createQueryBuilder()
43
            ->select('pg')
44
            ->from('VideoGamesRecords\CoreBundle\Entity\PlayerGame', 'pg')
45
            ->join('pg.game', 'g')
46
            ->addSelect('g')
47
            ->join('g.platforms', 'p')
48
            ->addSelect('p')
49
            ->where('pg.player = :player')
50
            ->setParameter('player', $player)
51
            ->orderBy('g.' . (Locale::getDefault() == 'fr' ? 'libGameFr' : 'libGameEn'), 'ASC');
52
53
        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...
54
    }
55
56
    /**
57
     * @param $player
58
     * @return array
59
     */
60
    private function getStatusPerGame($player): array
61
    {
62
        $qb = $this->em->createQueryBuilder()
63
            ->select('gam.id')
64
            ->from('VideoGamesRecords\CoreBundle\Entity\Game', 'gam')
65
            ->addSelect('status.id as idStatus')
66
            ->addSelect('COUNT(pc) as nb')
67
            ->innerJoin('gam.groups', 'grp')
68
            ->innerJoin('grp.charts', 'chr')
69
            ->innerJoin('chr.playerCharts', 'pc')
70
            ->innerJoin('pc.status', 'status')
71
            ->where('pc.player = :player')
72
            ->setParameter('player', $player)
73
            ->groupBy('gam.id')
74
            ->addGroupBy('status.id')
75
            ->orderBy('gam.id', 'ASC')
76
            ->addOrderBy('status.id', 'ASC');
77
78
        $list = $qb->getQuery()->getResult(2);
79
80
        $games = [];
81
        foreach ($list as $row) {
82
            $idGame = $row['id'];
83
            if (!array_key_exists($idGame, $games)) {
84
                $games[$idGame] = [];
85
            }
86
            $games[$idGame][] = [
87
                'status' => $this->em->find('VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus', $row['idStatus']),
88
                'nb' => $row['nb'],
89
            ];
90
        }
91
        return $games;
92
    }
93
}
94