StandingsSerializer   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B collection() 0 36 2
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