Team   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 210
ccs 68
cts 70
cp 0.9714
rs 10
c 3
b 0
f 0
wmc 26

13 Methods

Rating   Name   Duplication   Size   Complexity  
A addGame() 0 7 2
A getGames() 0 8 4
A __construct() 0 3 1
A addGroup() 0 5 2
A getGroupResults() 0 8 3
A export() 0 2 1
A addGroupResults() 0 12 1
A getSeed() 0 2 1
A getGamesInfo() 0 4 1
A seed() 0 3 1
A addGameWith() 0 6 2
A getGameWith() 0 20 6
A jsonSerialize() 0 2 1
1
<?php
2
3
namespace TournamentGenerator;
4
5
use Exception;
6
use InvalidArgumentException;
7
use TournamentGenerator\Export\ExporterInterface;
8
use TournamentGenerator\Export\Single\TeamExporter;
9
use TournamentGenerator\Interfaces\Exportable;
10
use TournamentGenerator\Traits\HasPositions;
11
use TournamentGenerator\Traits\HasScore;
12
13
/**
14
 * Class that identifies a team and holds the scores from the whole tournament.
15
 *
16
 * @author  Tomáš Vojík <[email protected]>
17
 *
18
 * @since   0.1
19
 * @package TournamentGenerator
20
 */
21
class Team extends Base implements Exportable
22
{
23
	use HasScore;
24
	use HasPositions;
25
26
	/**
27
	 * @var string     $name The name of the team
28
	 * @var string|int $id   The unique identifier of the team
29
	 */
30
31
	/** @var Game[] $games A list of games played by this team */
32
	protected array $games = [];
33
	/** @var array $gamesWith Multi-dimensional associative array of number of games together with other teams */
34
	protected array $gamesWith = [];
35
	/** @var int Seeding score */
36
	protected int $seed = 0;
37
38
	/**
39
	 * Initiates a team class
40
	 *
41
	 * @param string     $name Name of the team
42
	 * @param string|int $id   Unique identifier of the team
43
	 *
44
	 * @throws InvalidArgumentException if the provided argument id is not of type 'null' or 'string' or 'int'
45
	 */
46 285
	public function __construct(string $name = 'team', $id = null) {
47 285
		$this->setName($name);
48 285
		$this->setId($id ?? uniqid('', false));
49 285
	}
50
51 1
	public function seed(int $score) : Team {
52 1
		$this->seed = $score;
53 1
		return $this;
54
	}
55
56
	/**
57
	 * Gets team statistics from the given group without the group object
58
	 *
59
	 * @param string|int $groupId Unique identifier of the group to get its results
60
	 *
61
	 * @return array  All the statistics including points, score, wins, draws, losses, times being second, times being third
62
	 * @throws Exception if the group with given groupId doesn't exist
63
	 *
64
	 */
65 3
	public function getGamesInfo($groupId) : array {
66 3
		return array_filter($this->getGroupResults($groupId), static function($k) {
67 3
			return $k !== 'group';
68 3
		}, ARRAY_FILTER_USE_KEY);
69
	}
70
71
	/**
72
	 * Gets team statistics from the given group
73
	 *
74
	 * @param string|int|null $groupId Unique identifier of the group to get its results
75
	 *
76
	 * @return array  All the statistics including points, score, wins, draws, losses, times being second, times being third if the group id is set or all the statistics
77
	 * @throws Exception if the group with given groupId doesn't exist
78
	 *
79
	 */
80 23
	public function getGroupResults($groupId = null) : array {
81 23
		if (isset($groupId)) {
82 8
			if (!isset($this->groupResults[$groupId])) {
83 1
				throw new Exception('Trying to get nonexistent group results ('.$groupId.')');
84
			}
85 7
			return $this->groupResults[$groupId];
86
		}
87 17
		return $this->groupResults;
88
	}
89
90
	/**
91
	 * Creates a new data-array to store statistics for a new group
92
	 *
93
	 * Resets the statistics if the group was already added
94
	 *
95
	 * @param Group $group A group object to add its results
96
	 *
97
	 * @return $this
98
	 */
99 238
	public function addGroupResults(Group $group) : Team {
100 238
		$this->groupResults[$group->getId()] = [
101 238
			'group'  => $group,
102 238
			'points' => 0,
103 238
			'score'  => 0,
104 238
			'wins'   => 0,
105 238
			'draws'  => 0,
106 238
			'losses' => 0,
107 238
			'second' => 0,
108 238
			'third'  => 0
109
		];
110 238
		return $this;
111
	}
112
113
	/**
114
	 * Adds a record of a game with another team in a group
115
	 *
116
	 * @param Team  $team  A team that played with this team
117
	 * @param Group $group A group that the teams were playing in
118
	 *
119
	 * @return $this
120
	 */
121 226
	public function addGameWith(Team $team, Group $group) : Team {
122 226
		if (!isset($this->gamesWith[$group->getId()][$team->getId()])) {
123 226
			$this->gamesWith[$group->getId()][$team->getId()] = 0;
124
		}
125 226
		$this->gamesWith[$group->getId()][$team->getId()]++;
126 226
		return $this;
127
	}
128
129
	/**
130
	 * Gets a record of a game with another team or teams
131
	 *
132
	 * @param Team|null  $team  A team to get the games with
133
	 * @param Group|null $group A group from where to get the games
134
	 *
135
	 * @return array|int The number of games played with a team in a group if both arguments are given, array of all games with all teams from a group if only group is given, array of games with team from all groups if only a team argument is given or all games with all teams from all groups if no argument is given
136
	 */
137 3
	public function getGameWith(Team $team = null, Group $group = null) {
138 3
		if (isset($group)) {
139 3
			if (isset($team)) {
140 3
				return $this->gamesWith[$group->getId()][$team->getId()];
141
			}
142 3
			return $this->gamesWith[$group->getId()];
143
		}
144 2
		if (isset($team)) {
145 1
			$return = [];
146 1
			foreach ($this->gamesWith as $id => $games) {
147 1
				$filter = array_filter($games, static function($key) use ($team) {
148 1
					return $key === $team->getId();
149 1
				}, ARRAY_FILTER_USE_KEY);
150 1
				if (count($filter) > 0) {
151 1
					$return[$id] = $filter;
152
				}
153
			}
154 1
			return $return;
155
		}
156 1
		return $this->gamesWith;
157
	}
158
159
	/**
160
	 * Adds a group to a team and creates an array for all games to be played
161
	 *
162
	 * @param Group $group A group to add
163
	 *
164
	 * @return $this
165
	 */
166 1
	public function addGroup(Group $group) : Team {
167 1
		if (!isset($this->games[$group->getId()])) {
168 1
			$this->games[$group->getId()] = [];
169
		}
170 1
		return $this;
171
	}
172
173
	/**
174
	 * Adds a game to this team
175
	 *
176
	 * @param Game $game A game to add
177
	 *
178
	 * @return $this
179
	 */
180 226
	public function addGame(Game $game) : Team {
181 226
		$group = $game->getGroup();
182 226
		if (!isset($this->games[$group->getId()])) {
183 226
			$this->games[$group->getId()] = [];
184
		}
185 226
		$this->games[$group->getId()][] = $game;
186 226
		return $this;
187
	}
188
189
	/**
190
	 * Gets all game from given group
191
	 *
192
	 * @param Group|null      $group   A group to get its game from
193
	 * @param string|int|null $groupId An id of group to get its game from
194
	 *
195
	 * @return array Games from a group or all games if both arguments are null
196
	 */
197 4
	public function getGames(?Group $group = null, $groupId = null) {
198 4
		if (!is_null($group) && isset($this->games[$group->getId()])) {
199 1
			return $this->games[$group->getId()];
200
		}
201 4
		if (isset($groupId, $this->games[$groupId])) {
202 3
			return $this->games[$groupId];
203
		}
204 1
		return $this->games;
205
	}
206
207
	/**
208
	 * Prepares an export query for the object
209
	 *
210
	 * @return ExporterInterface Exporter for this class
211
	 */
212 13
	public function export() : ExporterInterface {
213 13
		return TeamExporter::start($this);
214
	}
215
216
	/**
217
	 * Get seeding score
218
	 *
219
	 * @return int
220
	 */
221 57
	public function getSeed() : int {
222 57
		return $this->seed;
223
	}
224
225
	/**
226
	 * @inheritDoc
227
	 * @return array
228
	 */
229
	public function jsonSerialize() : array {
230
		return $this->export()->get();
231
	}
232
}
233