StandingsSerializer::collection()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.8571
cc 2
eloc 22
nc 1
nop 1
1
<?php
2
3
namespace App\Serializers\Tournament;
4
5
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class StandingsSerializer
10
 * @package App\Serializers\Tournament
11
 */
12
class StandingsSerializer
13
{
14
    /**
15
     * @name collection
16
     * @param EloquentCollection $matches
17
     * @return Collection
18
     */
19
    public function collection(EloquentCollection $matches)
20
    {
21
        $standings = new Collection();
22
23
        $matches = $matches->sortBy('id');
24
25
        $matches->map(function ($match) use ($standings) {
26
27
            $pairId = [$match->homeTournamentTeam->id, $match->awayTournamentTeam->id];
28
            sort($pairId);
29
            $pairId = implode('-', $pairId);
30
31
            $pair = $standings->pull($pairId);
32
33
            if (!$pair) {
34
                $pair = [
35
                    'id' => $pairId,
36
                    'tournamentId' => $match->tournamentId,
37
                    'round' => $match->round,
38
                    'homeTeamId' => $match->homeTournamentTeam->id,
39
                    'homeTeamName' => $match->homeTournamentTeam->team->name,
40
                    'awayTeamId' => $match->awayTournamentTeam->id,
41
                    'awayTeamName' => $match->awayTournamentTeam->team->name,
42
                    'matches' => []
43
                ];
44
            }
45
46
            $pair['matches'][] = $match['id'];
47
48
            sort($pair['matches']);
49
50
            $standings->put($pairId, $pair);
51
        });
52
53
        return $standings;
54
    }
55
}
56