LeagueFixtureGenerator::generate()   B
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 24
rs 8.6845
cc 4
eloc 18
nc 5
nop 1
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
}