Completed
Push — master ( df80f0...840f28 )
by Rafal
11s
created

UserRating::sortUserByScore()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
4
namespace App\GameRating\Business\UserPoint;
5
6
7
use App\GameRating\Business\UserPoint\Info\UserScore;
8
use App\GameRating\Persistence\DataProvider\UserScoreWithPosition;
9
10
11
class UserRating implements UserRatingInterface
12
{
13
    /**
14
     * @param UserScore[] $usersScores
15
     * @return UserScoreWithPosition[]
16
     */
17
    public function sortUserByScore(array $usersScores): array
18
    {
19
        usort($usersScores, function ($a, $b) {
20
            return ($a->getScoreSumme() <=> $b->getScoreSumme()) * -1;
21
        });
22
23
        $usersPositionAndScore = [];
24
        $position = 0;
25
        $lastPoint = -1;
26
        $positionForFrontend = 0;
27
28
        /** @var UserScore $usersScore */
29
        foreach ($usersScores as $usersScore) {
30
            ++$position;
31
            if ($usersScore->getScoreSumme() !== $lastPoint) {
32
                $positionForFrontend = $position;
33
            }
34
            $usersPositionAndScore[] = new UserScoreWithPosition(
35
                $positionForFrontend,
36
                $usersScore
37
            );
38
            $lastPoint = $usersScore->getScoreSumme();
39
        }
40
41
        return $usersPositionAndScore;
42
    }
43
}