Passed
Branch develop (4ef38d)
by BENARD
03:50
created

TopGameStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 41
dl 0
loc 79
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getTop() 0 63 6
A supports() 0 3 1
1
<?php
2
namespace VideoGamesRecords\DwhBundle\DataProvider\Strategy\Top;
3
4
use DateTime;
5
use Doctrine\ORM\NonUniqueResultException;
6
use Doctrine\ORM\NoResultException;
7
use VideoGamesRecords\CoreBundle\Repository\GameRepository as CoreGameRepository;
8
use VideoGamesRecords\CoreBundle\Tools\Ranking as ToolsRanking;
9
use VideoGamesRecords\DwhBundle\Contracts\Strategy\TopStrategyInterface;
10
use VideoGamesRecords\DwhBundle\Repository\GameRepository as DwhGameRepository;
11
12
class TopGameStrategy extends AbstractTopProvider implements TopStrategyInterface
13
{
14
15
    public function supports(string $name): bool
16
    {
17
        return $name == self::TYPE_GAME;
18
    }
19
20
    /**
21
     * @param DateTime $date1Begin
22
     * @param DateTime $date1End
23
     * @param DateTime $date2Begin
24
     * @param DateTime $date2End
25
     * @param int      $limit
26
     * @return array
27
     */
28
    public function getTop(DateTime $date1Begin, DateTime $date1End, DateTime $date2Begin, DateTime $date2End, int $limit = 20): array
29
    {
30
        /** @var DwhGameRepository $dwhGameRepository */
31
        $dwhGameRepository = $this->dwhEntityManager->getRepository('VideoGamesRecords\DwhBundle\Entity\Game');
32
33
        /** @var CoreGameRepository $coreGameRepository */
34
        $coreGameRepository = $this->defaultEntityManager->getRepository('VideoGamesRecords\CoreBundle\Entity\Game');
35
36
        $gameList1 = $dwhGameRepository->getTop(
37
            $date1Begin,
38
            $date1End,
39
            $limit
40
        );
41
        $gameList2 = $dwhGameRepository->getTop(
42
            $date2Begin,
43
            $date2End,
44
            $limit
45
        );
46
47
        // Get old rank
48
        $oldRank = array();
49
        foreach ($gameList2 as $key => $row) {
50
            $oldRank[$row['id']] = $key + 1;
51
        }
52
53
        $nbPostFromList = 0;
54
        for ($i = 0, $nb = count($gameList1) - 1; $i <= $nb; ++$i) {
55
            $idGame = $gameList1[$i]['id'];
56
            if (isset($oldRank[$idGame])) {
57
                $gameList1[$i]['oldRank'] = $oldRank[$idGame];
58
            } else {
59
                $gameList1[$i]['oldRank'] = null;
60
            }
61
62
            $game = $coreGameRepository->find($idGame);
63
            $gameList1[$i]['game'] = $game;
64
            $nbPostFromList += $gameList1[$i]['nb'];
65
        }
66
67
        try {
68
            $nbGame = $dwhGameRepository->getTotalNbGame($date1Begin, $date1End);
69
        } catch (NoResultException | NonUniqueResultException $e) {
70
            // OK
71
        }
72
73
        try {
74
            $nbTotalPost = $dwhGameRepository->getTotalNbPostDay($date1Begin, $date1End);
75
        } catch (NoResultException | NonUniqueResultException $e) {
76
            // OK
77
        }
78
79
        $gameList = ToolsRanking::addRank(
80
            $gameList1,
81
            'rank',
82
            ['nb'],
83
            true
84
        );
85
86
        return array(
87
            'list' => $gameList,
88
            'nbPostFromList' => $nbPostFromList,
89
            'nbItem' => $nbGame,
90
            'nbTotalPost' => $nbTotalPost,
91
        );
92
    }
93
}
94