Completed
Push — master ( a1420e...5c3586 )
by Tomáš
02:42
created

Team::removePoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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