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

TopPlayerProvider::getTop()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 62
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 41
nc 24
nop 5
dl 0
loc 62
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
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\PlayerRepository as DwhPlayerRepository;
10
use VideoGamesRecords\CoreBundle\Repository\PlayerRepository as CorePlayerRepository;
11
12
class TopPlayerProvider
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 integer  $limit
29
     * @return array
30
     */
31
    public function getTop(DateTime $date1Begin, DateTime $date1End, DateTime $date2Begin, DateTime $date2End, int $limit = 20): array
32
    {
33
        /** @var DwhPlayerRepository $dwhPlayerRepository */
34
        $dwhPlayerRepository = $this->dwhEntityManager->getRepository('VideoGamesRecords\DwhBundle\Entity\Player');
35
36
        /** @var CorePlayerRepository $dwhPlayerRepository */
37
        $corePlayerRepository = $this->defaultEntityManager->getRepository('VideoGamesRecords\CoreBundle\Entity\Player');
38
39
        $playerList1 = $dwhPlayerRepository->getTop(
40
            $date1Begin,
41
            $date1End,
42
            $limit
43
        );
44
        $playerList2 = $dwhPlayerRepository->getTop(
45
            $date2Begin,
46
            $date2End,
47
            $limit
48
        );
49
50
        // Get old rank
51
        $oldRank = array();
52
        foreach ($playerList2 as $key => $row) {
53
            $oldRank[$row['id']] = $key + 1;
54
        }
55
56
        $nbPostFromList = 0;
57
        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

57
        for ($i=0, $nb=count(/** @scrutinizer ignore-type */ $playerList1) - 1; $i <= $nb; ++$i) {
Loading history...
58
            $idPlayer = $playerList1[$i]['id'];
59
            if (isset($oldRank[$idPlayer])) {
60
                $playerList1[$i]['oldRank'] = $oldRank[$idPlayer];
61
            } else {
62
                $playerList1[$i]['oldRank'] = null;
63
            }
64
            $player = $corePlayerRepository->find($idPlayer);
65
            $playerList1[$i]['player'] = $player;
66
            $nbPostFromList += $playerList1[$i]['nb'];
67
        }
68
69
        $nbPlayer = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $nbPlayer is dead and can be removed.
Loading history...
70
        try {
71
            $nbPlayer = $dwhPlayerRepository->getTotalNbPlayer($date1Begin, $date1End);
72
        } 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...
73
        }
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 = $dwhPlayerRepository->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
        $playerList = ToolsRanking::addRank(
82
            $playerList1,
83
            'rank',
84
            ['nb'],
85
            true
86
        );
87
88
        return array(
89
            'list' => $playerList,
90
            'nbPostFromList' => $nbPostFromList,
91
            'nb' => $nbPlayer,
92
            'nbTotalPost' => $nbTotalPost,
93
        );
94
    }
95
}
96