Passed
Push — master ( b9aa7a...67756f )
by Tomáš
02:47
created

Teams   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 22

2 Methods

Rating   Name   Duplication   Size   Complexity  
B sortGroup() 0 20 11
B sortRound() 0 22 11
1
<?php
2
3
namespace TournamentGenerator\Utilis\Sorter;
4
5
/**
6
 * TournamentGenerator sorter for teams
7
 */
8
class Teams
9
{
10
11
	public static function sortGroup(array &$teams, \TournamentGenerator\Group $group, string $ordering = POINTS) {
12
		if (!in_array($ordering, orderingTypes)) throw new \Exception('Unknown ordering type `'.$ordering.'`');
13
14
		switch ($ordering) {
15
			case \POINTS:{
16
				uasort($teams, function($a, $b) use ($group) {
17
					if ($a->groupResults[$group->id]["points"] === $b->groupResults[$group->id]["points"] && $a->groupResults[$group->id]["score"] === $b->groupResults[$group->id]["score"]) return 0;
18
					if ($a->groupResults[$group->id]["points"] === $b->groupResults[$group->id]["points"]) return ($a->groupResults[$group->id]["score"] > $b->groupResults[$group->id]["score"] ? -1 : 1);
19
					return ($a->groupResults[$group->id]["points"] > $b->groupResults[$group->id]["points"] ? -1 : 1);
20
				});
21
				break;}
22
			case \SCORE:{
23
				uasort($teams, function($a, $b) use ($group) {
24
					if ($a->groupResults[$group->id]["score"] === $b->groupResults[$group->id]["score"]) return 0;
25
					return ($a->groupResults[$group->id]["score"] > $b->groupResults[$group->id]["score"] ? -1 : 1);
26
				});
27
				break;}
28
		}
29
30
		return $teams;
31
	}
32
	public static function sortRound(array &$teams, \TournamentGenerator\Round $round, string $ordering = POINTS) {
33
		if (!in_array($ordering, orderingTypes)) throw new \Exception('Unknown ordering type `'.$ordering.'`');
34
35
		$groupsIds = $round->getGroupsIds();
36
37
		switch ($ordering) {
38
			case \POINTS:{
39
				uasort($teams, function($a, $b) use ($groupsIds) {
40
					if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds) && $a->sumScore($groupsIds) === $b->sumScore($groupsIds)) return 0;
41
					if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds)) return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1);
42
					return ($a->sumPoints($groupsIds) > $b->sumPoints($groupsIds) ? -1 : 1);
43
				});
44
				break;}
45
			case \SCORE:{
46
				uasort($teams, function($a, $b) use ($groupsIds) {
47
					if ($a->sumScore($groupsIds) === $b->sumScore($groupsIds)) return 0;
48
					return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1);
49
				});
50
				break;}
51
		}
52
53
		return $teams;
54
	}
55
56
}
57