Passed
Branch develop (6d6783)
by BENARD
03:42
created

TopGameProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getTop() 0 62 6
1
<?php
2
namespace VideoGamesRecords\DwhBundle\Service;
3
4
use DateTime;
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\NonUniqueResultException;
7
use Doctrine\ORM\NoResultException;
8
use VideoGamesRecords\CoreBundle\Tools\Ranking as ToolsRanking;
9
use VideoGamesRecords\DwhBundle\Repository\GameRepository as DwhGameRepository;
10
use VideoGamesRecords\CoreBundle\Repository\GameRepository as CoreGameRepository;
11
12
class TopGameProvider
13
{
14
    private EntityManagerInterface $dwhEntityManager;
15
    private EntityManagerInterface $defaultEntityManager;
16
17
    public function __construct(EntityManagerInterface $dwhEntityManager, EntityManagerInterface $defaultEntityManager)
18
    {
19
        $this->dwhEntityManager = $dwhEntityManager;
20
        $this->defaultEntityManager = $defaultEntityManager;
21
    }
22
23
    /**
24
     * @param DateTime $date1Begin
25
     * @param DateTime $date1End
26
     * @param DateTime $date2Begin
27
     * @param DateTime $date2End
28
     * @param int      $limit
29
     * @return array
30
     */
31
    public function getTop(DateTime $date1Begin, DateTime $date1End, DateTime $date2Begin, DateTime $date2End, int $limit = 20): array
32
    {
33
         /** @var DwhGameRepository $dwhGameRepository */
34
        $dwhGameRepository = $this->dwhEntityManager->getRepository('VideoGamesRecords\DwhBundle\Entity\Game');
35
36
        /** @var CoreGameRepository $coreGameRepository */
37
        $coreGameRepository = $this->defaultEntityManager->getRepository('VideoGamesRecords\CoreBundle\Entity\Game');
38
39
        $gameList1 = $dwhGameRepository->getTop(
40
            $date1Begin,
41
            $date1End,
42
            $limit
43
        );
44
        $gameList2 = $dwhGameRepository->getTop(
45
            $date2Begin,
46
            $date2End,
47
            $limit
48
        );
49
50
        // Get old rank
51
        $oldRank = array();
52
        foreach ($gameList2 as $key => $row) {
53
            $oldRank[$row['id']] = $key + 1;
54
        }
55
56
        $nbPostFromList = 0;
57
        for ($i=0, $nb=count($gameList1) - 1; $i <= $nb; ++$i) {
58
            $idGame = $gameList1[$i]['id'];
59
            if (isset($oldRank[$idGame])) {
60
                $gameList1[$i]['oldRank'] = $oldRank[$idGame];
61
            } else {
62
                $gameList1[$i]['oldRank'] = null;
63
            }
64
65
            $game = $coreGameRepository->find($idGame);
66
            $gameList1[$i]['game'] = $game;
67
            $nbPostFromList += $gameList1[$i]['nb'];
68
        }
69
70
        $nbGame = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbGame is dead and can be removed.
Loading history...
71
        try {
72
            $nbGame = $dwhGameRepository->getTotalNbGame($date1Begin, $date1End);
73
        } catch (NoResultException|NonUniqueResultException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
74
        }
75
        $nbTotalPost = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbTotalPost is dead and can be removed.
Loading history...
76
        try {
77
            $nbTotalPost = $dwhGameRepository->getTotalNbPostDay($date1Begin, $date1End);
78
        } catch (NoResultException|NonUniqueResultException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
79
        }
80
81
        $gameList = ToolsRanking::addRank(
82
            $gameList1,
83
            'rank',
84
            ['nb'],
85
            true
86
        );
87
88
        return array(
89
            'list' => $gameList,
90
            'nbPostFromList' => $nbPostFromList,
91
            'nbItem' => $nbGame,
92
            'nbTotalPost' => $nbTotalPost,
93
        );
94
    }
95
}
96