Completed
Push — master ( 0cf74a...7860de )
by Vincenzo
02:41
created

LeagueFixtureGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 4
c 2
b 1
f 1
lcom 0
cbo 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 24 4
1
<?php
2
3
4
namespace App\Lib\DsManager\Helpers;
5
6
class LeagueFixtureGenerator
7
{
8
    public static function generate(array $teams)
9
    {
10
        $numTeams = count($teams);
11
        $numRounds = ($numTeams - 1);
12
        $halfSize = $numTeams / 2;
13
14
        $away = array_splice($teams, $halfSize);
15
        $home = $teams;
16
        $rounds = [];
17
        for ($i = 0; $i < $numRounds; $i++) {
18
            $homeCount = count($home);
19
            for ($j = 0; $j < $homeCount; $j++) {
20
                $rounds[$i][$j]["home_team_id"] = $home[$j]['id'];
21
                $rounds[$i][$j]["away_team_id"] = $away[$j]['id'];
22
            }
23
            if (count($home) + count($away) - 1 > 2) {
24
                $spliced = array_splice($home, 1, 1);
25
                $shifted = array_shift($spliced);
26
                array_unshift($away, $shifted);
27
                array_push($home, array_pop($away));
28
            }
29
        }
30
        return $rounds;
31
    }
32
}