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

TopPlayerStrategy::getTop()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 62
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 62
rs 8.6737
c 0
b 0
f 0
cc 6
nc 24
nop 5

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
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\PlayerRepository as CorePlayerRepository;
8
use VideoGamesRecords\CoreBundle\Tools\Ranking as ToolsRanking;
9
use VideoGamesRecords\DwhBundle\Contracts\Strategy\TopStrategyInterface;
10
use VideoGamesRecords\DwhBundle\Repository\PlayerRepository as DwhPlayerRepository;
11
12
class TopPlayerStrategy extends AbstractTopProvider implements TopStrategyInterface
13
{
14
    public function supports(string $name): bool
15
    {
16
        return $name == self::TYPE_PLAYER;
17
    }
18
19
    /**
20
     * @param DateTime $date1Begin
21
     * @param DateTime $date1End
22
     * @param DateTime $date2Begin
23
     * @param DateTime $date2End
24
     * @param integer  $limit
25
     * @return array
26
     */
27
    public function getTop(DateTime $date1Begin, DateTime $date1End, DateTime $date2Begin, DateTime $date2End, int $limit = 20): array
28
    {
29
        /** @var DwhPlayerRepository $dwhPlayerRepository */
30
        $dwhPlayerRepository = $this->dwhEntityManager->getRepository('VideoGamesRecords\DwhBundle\Entity\Player');
31
32
        /** @var CorePlayerRepository $dwhPlayerRepository */
33
        $corePlayerRepository = $this->defaultEntityManager->getRepository('VideoGamesRecords\CoreBundle\Entity\Player');
34
35
        $playerList1 = $dwhPlayerRepository->getTop(
36
            $date1Begin,
37
            $date1End,
38
            $limit
39
        );
40
        $playerList2 = $dwhPlayerRepository->getTop(
41
            $date2Begin,
42
            $date2End,
43
            $limit
44
        );
45
46
        // Get old rank
47
        $oldRank = array();
48
        foreach ($playerList2 as $key => $row) {
49
            $oldRank[$row['id']] = $key + 1;
50
        }
51
52
        $nbPostFromList = 0;
53
        for ($i = 0, $nb = count($playerList1) - 1; $i <= $nb; ++$i) {
0 ignored issues
show
Bug introduced by
It seems like $playerList1 can also be of type integer and null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        for ($i = 0, $nb = count(/** @scrutinizer ignore-type */ $playerList1) - 1; $i <= $nb; ++$i) {
Loading history...
54
            $idPlayer = $playerList1[$i]['id'];
55
            if (isset($oldRank[$idPlayer])) {
56
                $playerList1[$i]['oldRank'] = $oldRank[$idPlayer];
57
            } else {
58
                $playerList1[$i]['oldRank'] = null;
59
            }
60
            $player = $corePlayerRepository->find($idPlayer);
61
            $playerList1[$i]['player'] = $player;
62
            $nbPostFromList += $playerList1[$i]['nb'];
63
        }
64
65
        try {
66
            $nbPlayer = $dwhPlayerRepository->getTotalNbPlayer($date1Begin, $date1End);
67
        } catch (NoResultException | NonUniqueResultException $e) {
68
            // OK
69
        }
70
71
        try {
72
            $nbTotalPost = $dwhPlayerRepository->getTotalNbPostDay($date1Begin, $date1End);
73
        } catch (NoResultException | NonUniqueResultException $e) {
74
            // OK
75
        }
76
77
        $playerList = ToolsRanking::addRank(
78
            $playerList1,
79
            'rank',
80
            ['nb'],
81
            true
82
        );
83
84
        return array(
85
            'list' => $playerList,
86
            'nbPostFromList' => $nbPostFromList,
87
            'nb' => $nbPlayer,
88
            'nbTotalPost' => $nbTotalPost,
89
        );
90
    }
91
}
92