TopPlayerStrategy::getTop()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 71
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 42
nc 24
nop 5
dl 0
loc 71
rs 8.6257
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\PlayerRepository as CorePlayerRepository;
11
use VideoGamesRecords\CoreBundle\Tools\Ranking as ToolsRanking;
12
use VideoGamesRecords\DwhBundle\Contracts\Strategy\TopStrategyInterface;
13
use VideoGamesRecords\DwhBundle\Repository\PlayerRepository as DwhPlayerRepository;
14
15
class TopPlayerStrategy extends AbstractTopProvider implements TopStrategyInterface
16
{
17
    public function supports(string $name): bool
18
    {
19
        return $name == self::TYPE_PLAYER;
20
    }
21
22
    /**
23
     * @param DateTime $date1Begin
24
     * @param DateTime $date1End
25
     * @param DateTime $date2Begin
26
     * @param DateTime $date2End
27
     * @param integer  $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 DwhPlayerRepository $dwhPlayerRepository */
38
        $dwhPlayerRepository = $this->dwhEntityManager->getRepository('VideoGamesRecords\DwhBundle\Entity\Player');
39
40
        /** @var CorePlayerRepository $dwhPlayerRepository */
41
        $corePlayerRepository = $this->defaultEntityManager->getRepository(
42
            'VideoGamesRecords\CoreBundle\Entity\Player'
43
        );
44
45
        $playerList1 = $dwhPlayerRepository->getTop(
46
            $date1Begin,
47
            $date1End,
48
            $limit
49
        );
50
        $playerList2 = $dwhPlayerRepository->getTop(
51
            $date2Begin,
52
            $date2End,
53
            $limit
54
        );
55
56
        // Get old rank
57
        $oldRank = array();
58
        foreach ($playerList2 as $key => $row) {
59
            $oldRank[$row['id']] = $key + 1;
60
        }
61
62
        $nbPostFromList = 0;
63
        for ($i = 0, $nb = count($playerList1) - 1; $i <= $nb; ++$i) {
64
            $idPlayer = $playerList1[$i]['id'];
65
            if (isset($oldRank[$idPlayer])) {
66
                $playerList1[$i]['oldRank'] = $oldRank[$idPlayer];
67
            } else {
68
                $playerList1[$i]['oldRank'] = null;
69
            }
70
            $player = $corePlayerRepository->find($idPlayer);
71
            $playerList1[$i]['player'] = $player;
72
            $nbPostFromList += $playerList1[$i]['nb'];
73
        }
74
75
        $nbPlayer = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbPlayer is dead and can be removed.
Loading history...
76
        try {
77
            $nbPlayer = $dwhPlayerRepository->getTotalNbPlayer($date1Begin, $date1End);
78
        } catch (NoResultException | NonUniqueResultException $e) {
79
            // OK
80
        }
81
82
        $nbTotalPost = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbTotalPost is dead and can be removed.
Loading history...
83
        try {
84
            $nbTotalPost = $dwhPlayerRepository->getTotalNbPostDay($date1Begin, $date1End);
85
        } catch (NoResultException | NonUniqueResultException $e) {
86
            // OK
87
        }
88
89
        $playerList = ToolsRanking::addRank(
90
            $playerList1,
91
            'rank',
92
            ['nb'],
93
            true
94
        );
95
96
        return array(
97
            'list' => $playerList,
98
            'nbPostFromList' => $nbPostFromList,
99
            'nb' => $nbPlayer,
100
            'nbTotalPost' => $nbTotalPost,
101
        );
102
    }
103
}
104