TopGameStrategy::getTop()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 70
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 41
nc 24
nop 5
dl 0
loc 70
rs 8.6417
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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