Completed
Push — master ( c3f0f6...0c6e4d )
by Tomáš
02:30
created

TeamSorter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 27
c 1
b 0
f 0
dl 0
loc 75
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A sortTeamsByPoints() 0 9 6
A sortTeamsByScore() 0 6 3
A sort() 0 11 3
1
<?php
2
3
namespace TournamentGenerator\Helpers\Sorter;
4
5
use Exception;
6
use InvalidArgumentException;
7
use TournamentGenerator\Constants;
8
use TournamentGenerator\Containers\BaseContainer;
9
use TournamentGenerator\Team;
10
11
/**
12
 * TournamentGenerator sorter for teams
13
 *
14
 * @author  Tomáš Vojík <[email protected]>
15
 *
16
 * @package TournamentGenerator\Helpers\Sorter
17
 * @since   0.3
18
 */
19
class TeamSorter implements BaseSorter
20
{
21
	/** @var int[]|string[] Array of Group ids */
22
	protected static array $ids;
23
24
	/** @var string What to sort by */
25
	protected string $ordering;
26
	/** @var BaseContainer Container that contains the data to sort */
27
	protected BaseContainer $container;
28
29
	/**
30
	 * TeamSorter constructor.
31
	 *
32
	 * @param BaseContainer $container
33
	 * @param string        $ordering What to order by (\TournamentGenerator\Constants::POINTS / \TournamentGenerator\Constants::SCORE)
34
	 *
35
	 * @throws InvalidArgumentException
36
	 */
37 49
	public function __construct(BaseContainer $container, string $ordering = Constants::POINTS) {
38 49
		if (!in_array($ordering, Constants::OrderingTypes, true)) {
39 1
			throw new InvalidArgumentException('Unknown ordering type `'.$ordering.'`');
40
		}
41 48
		$this->container = $container;
42 48
		$this->ordering = $ordering;
43 48
	}
44
45
	/**
46
	 * Sorter function for usort by points
47
	 *
48
	 * @param Team $a First team
49
	 * @param Team $b Second team
50
	 */
51 44
	protected static function sortTeamsByPoints(Team $a, Team $b) : int {
52 44
		$groupsIds = self::$ids;
53 44
		if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds) && $a->sumScore($groupsIds) === $b->sumScore($groupsIds)) {
54 3
			return 0;
55
		}
56 41
		if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds)) {
57 20
			return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1);
58
		}
59 41
		return ($a->sumPoints($groupsIds) > $b->sumPoints($groupsIds) ? -1 : 1);
60
	}
61
62
	/**
63
	 * Sorter function for usort by score
64
	 *
65
	 * @param Team $a First team
66
	 * @param Team $b Second team
67
	 */
68 4
	protected static function sortTeamsByScore(Team $a, Team $b) : int {
69 4
		$groupsIds = self::$ids;
70 4
		if ($a->sumScore($groupsIds) === $b->sumScore($groupsIds)) {
71 1
			return 0;
72
		}
73 4
		return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1);
74
	}
75
76
	/**
77
	 * Sort function to call
78
	 *
79
	 * @param array $data
80
	 *
81
	 * @return array
82
	 */
83 48
	public function sort(array $data) : array {
84 48
		$this::$ids = $this->container->getLeafIds();
85 48
		switch ($this->ordering) {
86
			case Constants::POINTS:
87 44
				usort($data, [__CLASS__, 'sortTeamsByPoints']);
88 44
				break;
89
			case Constants::SCORE:
90 4
				usort($data, [__CLASS__, 'sortTeamsByScore']);
91 4
				break;
92
		}
93 48
		return $data;
94
	}
95
}
96