Passed
Push — master ( 795926...7012fc )
by Tomáš
11:11
created

Team::removeSecond()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
1
<?php
2
3
namespace TournamentGenerator;
4
5
use Exception;
6
use InvalidArgumentException;
7
8
/**
9
 * Class that identifies a team and holds the scores from the whole tournament.
10
 *
11
 * @author  Tomáš Vojík <[email protected]>
12
 *
13
 * @since   0.1
14
 * @package TournamentGenerator
15
 */
16
class Team extends Base
17
{
18
19
	/**
20
	 * @var string     $name The name of the team
21
	 * @var string|int $id   The unique identifier of the team
22
	 */
23
24
	/**
25
	 * @var array[] Associative array of results in each group
26
	 * @details [
27
	 * * groupId => [
28
	 * * * "group"  => Group, # GROUP OBJECT
29
	 * * * "points" => int 0, # NUMBER OF POINTS ACQUIRED
30
	 * * * "score"  => int 0, # SUM OF SCORE ACQUIRED
31
	 * * * "wins"   => int 0, # NUMBER OF WINS
32
	 * * * "draws"  => int 0, # NUMBER OF DRAWS
33
	 * * * "losses" => int 0, # NUMBER OF LOSSES
34
	 * * * "second" => int 0, # NUMBER OF TIMES BEING SECOND (ONLY FOR INGAME OPTION OF 3 OR 4)
35
	 * * * "third"  => int 0  # NUMBER OF TIMES BEING THIRD  (ONLY FOR INGAME OPTION OF 4)
36
	 * * ]
37
	 * ]
38
	 */
39
	public array $groupResults = [];
40
	/** @var Game[] $games A list of games played by this team */
41
	protected array $games = [];
42
	/** @var array $gamesWith Multi-dimensional associative array of number of games together with other teams */
43
	protected array $gamesWith = [];
44
	/** @var int $sumPoints Sum of all points acquired through the whole tournament */
45
	protected int $sumPoints = 0;
46
	/** @var int $sumScore Sum of all score acquired through the whole tournament */
47
	protected int $sumScore = 0;
48
49
	/**
50
	 * Initiates a team class
51
	 *
52
	 * @param string     $name Name of the team
53
	 * @param string|int $id   Unique identifier of the team
54
	 *
55
	 * @throws InvalidArgumentException if the provided argument id is not of type 'null' or 'string' or 'int'
56
	 */
57 131
	public function __construct(string $name = 'team', $id = null) {
58 131
		$this->setName($name);
59 131
		$this->setId($id ?? uniqid('', false));
60 131
	}
61
62
	/**
63
	 * Gets team statistics from the given group without the group object
64
	 *
65
	 * @param string|int $groupId Unique identifier of the group to get its results
66
	 *
67
	 * @return array  All the statistics including points, score, wins, draws, losses, times being second, times being third
68
	 * @throws Exception if the group with given groupId doesn't exist
69
	 *
70
	 */
71 1
	public function getGamesInfo($groupId) : array {
72 1
		return array_filter($this->getGroupResults($groupId), static function($k) {
73 1
			return $k !== 'group';
74 1
		}, ARRAY_FILTER_USE_KEY);
75
	}
76
77
	/**
78
	 * Gets team statistics from the given group
79
	 *
80
	 * @param string|int|null $groupId Unique identifier of the group to get its results
81
	 *
82
	 * @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
83
	 * @throws Exception if the group with given groupId doesn't exist
84
	 *
85
	 */
86 7
	public function getGroupResults($groupId = null) : array {
87 7
		if (isset($groupId)) {
88 5
			if (!isset($this->groupResults[$groupId])) {
89
				throw new Exception('Trying to get nonexistent group results ('.$groupId.')');
90
			}
91 5
			return $this->groupResults[$groupId];
92
		}
93 2
		return $this->groupResults;
94
	}
95
96
	/**
97
	 * Creates a new data-array to store statistics for a new group
98
	 *
99
	 * Resets the statistics if the group was already added
100
	 *
101
	 * @param Group $group A group object to add its results
102
	 *
103
	 * @return $this
104
	 */
105 106
	public function addGroupResults(Group $group) : Team {
106 106
		$this->groupResults[$group->getId()] = [
107 106
			'group'  => $group,
108 106
			'points' => 0,
109 106
			'score'  => 0,
110 106
			'wins'   => 0,
111 106
			'draws'  => 0,
112 106
			'losses' => 0,
113 106
			'second' => 0,
114 106
			'third'  => 0
115
		];
116 106
		return $this;
117
	}
118
119
	/**
120
	 * Adds a record of a game with another team in a group
121
	 *
122
	 * @param Team  $team  A team that played with this team
123
	 * @param Group $group A group that the teams were playing in
124
	 *
125
	 * @return $this
126
	 */
127 99
	public function addGameWith(Team $team, Group $group) : Team {
128 99
		if (!isset($this->gamesWith[$group->getId()][$team->getId()])) {
129 99
			$this->gamesWith[$group->getId()][$team->getId()] = 0;
130
		}
131 99
		$this->gamesWith[$group->getId()][$team->getId()]++;
132 99
		return $this;
133
	}
134
135
	/**
136
	 * Gets a record of a game with another team or teams
137
	 *
138
	 * @param Team|null  $team  A team to get the games with
139
	 * @param Group|null $group A group from where to get the games
140
	 *
141
	 * @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
142
	 */
143 3
	public function getGameWith(Team $team = null, Group $group = null) {
144 3
		if (isset($group)) {
145 3
			if (isset($team)) {
146 3
				return $this->gamesWith[$group->getId()][$team->getId()];
147
			}
148 3
			return $this->gamesWith[$group->getId()];
149
		}
150 2
		if (isset($team)) {
151 1
			$return = [];
152 1
			foreach ($this->gamesWith as $id => $games) {
153 1
				$filter = array_filter($games, static function($key) use ($team) {
154 1
					return $key === $team->getId();
155 1
				}, ARRAY_FILTER_USE_KEY);
156 1
				if (count($filter) > 0) {
157 1
					$return[$id] = $filter;
158
				}
159
			}
160 1
			return $return;
161
		}
162 1
		return $this->gamesWith;
163
	}
164
165
	/**
166
	 * Adds a group to a team and creates an array for all games to be played
167
	 *
168
	 * @param Group $group A group to add
169
	 *
170
	 * @return $this
171
	 */
172 1
	public function addGroup(Group $group) : Team {
173 1
		if (!isset($this->games[$group->getId()])) {
174 1
			$this->games[$group->getId()] = [];
175
		}
176 1
		return $this;
177
	}
178
179
	/**
180
	 * Adds a game to this team
181
	 *
182
	 * @param Game $game A game to add
183
	 *
184
	 * @return $this
185
	 */
186 99
	public function addGame(Game $game) : Team {
187 99
		$group = $game->getGroup();
188 99
		if (!isset($this->games[$group->getId()])) {
189 99
			$this->games[$group->getId()] = [];
190
		}
191 99
		$this->games[$group->getId()][] = $game;
192 99
		return $this;
193
	}
194
195
	/**
196
	 * Gets all game from given group
197
	 *
198
	 * @param Group|null      $group   A group to get its game from
199
	 * @param string|int|null $groupId An id of group to get its game from
200
	 *
201
	 * @return array Games from a group or all games if both arguments are null
202
	 */
203 4
	public function getGames(?Group $group = null, $groupId = null) {
204 4
		if (!is_null($group) && isset($this->games[$group->getId()])) {
205 1
			return $this->games[$group->getId()];
206
		}
207 4
		if (isset($groupId, $this->games[$groupId])) {
208 3
			return $this->games[$groupId];
209
		}
210 1
		return $this->games;
211
	}
212
213
	/**
214
	 * Gets all points that the team has acquired through the tournament
215
	 *
216
	 * @return int Sum of the points acquired
217
	 */
218 5
	public function getSumPoints() : int {
219 5
		return $this->sumPoints;
220
	}
221
222
	/**
223
	 * Gets all score that the team has acquired through the tournament
224
	 *
225
	 * @return int Sum of the score acquired
226
	 */
227 5
	public function getSumScore() : int {
228 5
		return $this->sumScore;
229
	}
230
231
	/**
232
	 * Adds a win to the team
233
	 *
234
	 * @param string|int|null $groupId An id of group to add the results from
235
	 *
236
	 * @return $this
237
	 * @throws Exception if the group results have not been added
238
	 *
239
	 * @uses Group::getWinPoints() to retrieve the points to add
240
	 *
241
	 */
242 72
	public function addWin(string $groupId = '') : Team {
243 72
		if (!isset($this->groupResults[$groupId])) {
244
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
245
		}
246 72
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->getWinPoints();
247 72
		$this->sumPoints += $this->groupResults[$groupId]['group']->getWinPoints();
248 72
		$this->groupResults[$groupId]['wins']++;
249 72
		return $this;
250
	}
251
252
	/**
253
	 * Remove a win from the team
254
	 *
255
	 * @param string|int|null $groupId An id of group to add the results from
256
	 *
257
	 * @return $this
258
	 * @throws Exception if the group results have not been added
259
	 *
260
	 * @uses Group::getWinPoints() to retrieve the points to add
261
	 *
262
	 */
263 19
	public function removeWin(string $groupId = '') : Team {
264 19
		if (!isset($this->groupResults[$groupId])) {
265
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
266
		}
267 19
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->getWinPoints();
268 19
		$this->sumPoints -= $this->groupResults[$groupId]['group']->getWinPoints();
269 19
		$this->groupResults[$groupId]['wins']--;
270 19
		return $this;
271
	}
272
273
	/**
274
	 * Adds a draw to the team
275
	 *
276
	 * @param string|int|null $groupId An id of group to add the results from
277
	 *
278
	 * @return $this
279
	 * @throws Exception if the group results have not been added
280
	 *
281
	 * @uses Group::getDrawPoints() to retrieve the points to add
282
	 *
283
	 */
284 14
	public function addDraw(string $groupId = '') : Team {
285 14
		if (!isset($this->groupResults[$groupId])) {
286
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
287
		}
288 14
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->getDrawPoints();
289 14
		$this->sumPoints += $this->groupResults[$groupId]['group']->getDrawPoints();
290 14
		$this->groupResults[$groupId]['draws']++;
291 14
		return $this;
292
	}
293
294
	/**
295
	 * Remove a draw from the team
296
	 *
297
	 * @param string|int|null $groupId An id of group to add the results from
298
	 *
299
	 * @return $this
300
	 * @throws Exception if the group results have not been added
301
	 *
302
	 * @uses Group::getDrawPoints() to retrieve the points to add
303
	 *
304
	 */
305 1
	public function removeDraw(string $groupId = '') : Team {
306 1
		if (!isset($this->groupResults[$groupId])) {
307
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
308
		}
309 1
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->getDrawPoints();
310 1
		$this->sumPoints -= $this->groupResults[$groupId]['group']->getDrawPoints();
311 1
		$this->groupResults[$groupId]['draws']--;
312 1
		return $this;
313
	}
314
315
	/**
316
	 * Adds a loss to the team
317
	 *
318
	 * @param string|int|null $groupId An id of group to add the results from
319
	 *
320
	 * @return $this
321
	 * @throws Exception if the group results have not been added
322
	 *
323
	 * @uses Group::getLossPoints() to retrieve the points to add
324
	 *
325
	 */
326 72
	public function addLoss(string $groupId = '') : Team {
327 72
		if (!isset($this->groupResults[$groupId])) {
328
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
329
		}
330 72
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->getLostPoints();
331 72
		$this->sumPoints += $this->groupResults[$groupId]['group']->getLostPoints();
332 72
		$this->groupResults[$groupId]['losses']++;
333 72
		return $this;
334
	}
335
336
	/**
337
	 * Remove a loss from the team
338
	 *
339
	 * @param string|int|null $groupId An id of group to add the results from
340
	 *
341
	 * @return $this
342
	 * @throws Exception if the group results have not been added
343
	 *
344
	 * @uses Group::getLossPoints() to retrieve the points to add
345
	 *
346
	 */
347 19
	public function removeLoss(string $groupId = '') : Team {
348 19
		if (!isset($this->groupResults[$groupId])) {
349
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
350
		}
351 19
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->getLostPoints();
352 19
		$this->sumPoints -= $this->groupResults[$groupId]['group']->getLostPoints();
353 19
		$this->groupResults[$groupId]['losses']--;
354 19
		return $this;
355
	}
356
357
	/**
358
	 * Add points for being second to the team
359
	 *
360
	 * @param string|int|null $groupId An id of group to add the results from
361
	 *
362
	 * @return $this
363
	 * @throws Exception if the group results have not been added
364
	 *
365
	 * @uses Group::getSecondPoints() to retrieve the points to add
366
	 *
367
	 */
368 10
	public function addSecond(string $groupId = '') : Team {
369 10
		if (!isset($this->groupResults[$groupId])) {
370
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
371
		}
372 10
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->getSecondPoints();
373 10
		$this->sumPoints += $this->groupResults[$groupId]['group']->getSecondPoints();
374 10
		$this->groupResults[$groupId]['second']++;
375 10
		return $this;
376
	}
377
378
	/**
379
	 * Remove points for being second from the team
380
	 *
381
	 * @param string|int|null $groupId An id of group to add the results from
382
	 *
383
	 * @return $this
384
	 * @throws Exception if the group results have not been added
385
	 *
386
	 * @uses Group::getSecondPoints() to retrieve the points to add
387
	 *
388
	 */
389 2
	public function removeSecond(string $groupId = '') : Team {
390 2
		if (!isset($this->groupResults[$groupId])) {
391
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
392
		}
393 2
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->getSecondPoints();
394 2
		$this->sumPoints -= $this->groupResults[$groupId]['group']->getSecondPoints();
395 2
		$this->groupResults[$groupId]['second']--;
396 2
		return $this;
397
	}
398
399
	/**
400
	 * Add points for being third to the team
401
	 *
402
	 * @param string|int|null $groupId An id of group to add the results from
403
	 *
404
	 * @return $this
405
	 * @throws Exception if the group results have not been added
406
	 *
407
	 * @uses Group::getThirdPoints() to retrieve the points to add
408
	 *
409
	 */
410 9
	public function addThird(string $groupId = '') : Team {
411 9
		if (!isset($this->groupResults[$groupId])) {
412
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
413
		}
414 9
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->getThirdPoints();
415 9
		$this->sumPoints += $this->groupResults[$groupId]['group']->getThirdPoints();
416 9
		$this->groupResults[$groupId]['third']++;
417 9
		return $this;
418
	}
419
420
	/**
421
	 * Remove points for being third from the team
422
	 *
423
	 * @param string|int|null $groupId An id of group to add the results from
424
	 *
425
	 * @return $this
426
	 * @throws Exception if the group results have not been added
427
	 *
428
	 * @uses Group::getThirdPoints() to retrieve the points to add
429
	 *
430
	 */
431 1
	public function removeThird(string $groupId = '') : Team {
432 1
		if (!isset($this->groupResults[$groupId])) {
433
			throw new Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
434
		}
435 1
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->getThirdPoints();
436 1
		$this->sumPoints -= $this->groupResults[$groupId]['group']->getThirdPoints();
437 1
		$this->groupResults[$groupId]['third']--;
438 1
		return $this;
439
	}
440
441
	/**
442
	 * Calculate all the points acquired from given group ids
443
	 *
444
	 * @param array $groupIds Array of group ids
445
	 *
446
	 * @return int Sum of the points or sum of all points if argument is empty
447
	 */
448 47
	public function sumPoints(array $groupIds = []) : int {
449 47
		if (count($groupIds) === 0) {
450
			return $this->sumPoints;
451
		}
452 47
		$sum = 0;
453 47
		foreach ($groupIds as $gid) {
454 47
			$sum += $this->groupResults[$gid]['points'] ?? 0;
455
		}
456 47
		return $sum;
457
	}
458
459
	/**
460
	 * Calculate all score acquired from given group ids
461
	 *
462
	 * @param array $groupIds Array of group ids
463
	 *
464
	 * @return int Sum of score or sum of all score if argument is empty
465
	 */
466 28
	public function sumScore(array $groupIds = []) : int {
467 28
		if (count($groupIds) === 0) {
468
			return $this->sumScore;
469
		}
470 28
		$sum = 0;
471 28
		foreach ($groupIds as $gid) {
472 28
			$sum += $this->groupResults[$gid]['score'] ?? 0;
473
		}
474 28
		return $sum;
475
	}
476
477
	/**
478
	 * Adds score to the total sum
479
	 *
480
	 * @param int $score Score to add
481
	 *
482
	 * @return $this
483
	 */
484 74
	public function addScore(int $score) : Team {
485 74
		$this->sumScore += $score;
486 74
		return $this;
487
	}
488
489
	/**
490
	 * Removes score to the total sum
491
	 *
492
	 * @param int $score Score to add
493
	 *
494
	 * @return $this
495
	 */
496 21
	public function removeScore(int $score) : Team {
497 21
		$this->sumScore -= $score;
498 21
		return $this;
499
	}
500
501
	/**
502
	 * Adds points to the total sum
503
	 *
504
	 * @param int $points Points to add
505
	 *
506
	 * @return $this
507
	 */
508 17
	public function addPoints(int $points) : Team {
509 17
		$this->sumPoints += $points;
510 17
		return $this;
511
	}
512
513
	/**
514
	 * Removes points to the total sum
515
	 *
516
	 * @param int $points Points to remove
517
	 *
518
	 * @return $this
519
	 */
520 1
	public function removePoints(int $points) : Team {
521 1
		$this->sumPoints -= $points;
522 1
		return $this;
523
	}
524
}
525