Test Failed
Push — master ( 2fbbce...c5a571 )
by Tomáš
02:30
created

Importer::import()   F

Complexity

Conditions 52
Paths 320

Size

Total Lines 181
Code Lines 120

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 52
eloc 120
c 1
b 0
f 0
nc 320
nop 1
dl 0
loc 181
rs 1.4665

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace TournamentGenerator\Import;
5
6
use Exception;
7
use JsonException;
8
use TournamentGenerator\Base;
9
use TournamentGenerator\Category;
10
use TournamentGenerator\Group;
11
use TournamentGenerator\Interfaces\WithSkipSetters;
12
use TournamentGenerator\Round;
13
use TournamentGenerator\Team;
14
use TournamentGenerator\TeamFilter;
15
use TournamentGenerator\Tournament;
16
17
/**
18
 * Basic importer
19
 *
20
 * Importer uses exported data and creates a new tournament objects from it.
21
 *
22
 * @package TournamentGenerator\Import
23
 * @author  Tomáš Vojík <[email protected]>
24
 * @since   0.5
25
 */
26
class Importer
27
{
28
29
	/**
30
	 * Processes a JSON input and creates necessary objects from it
31
	 *
32
	 * @param string $data
33
	 *
34
	 * @return Base
35
	 * @throws JsonException
36
	 * @throws InvalidImportDataException
37
	 * @see Importer::import()
38
	 *
39
	 */
40
	public static function importJson(string $data) : Base {
41
		return self::import(json_decode($data, true, 512, JSON_THROW_ON_ERROR));
42
	}
43
44
	/**
45
	 * Processes an input array of data and creates necessary objects from it
46
	 *
47
	 * @param array $data
48
	 *
49
	 * @return Base
50
	 * @throws InvalidImportDataException
51
	 */
52
	public static function import(array $data) : Base {
53
		// Validate data
54
		ImportValidator::validate($data);
55
56
		$categories = [];
57
		$rounds = [];
58
		$groups = [];
59
		$teams = [];
60
		$games = [];
61
62
		$root = null;
63
		if (isset($data['tournament'])) {
64
			$setting = (array) $data['tournament'];
65
			if (empty($setting['type']) || $setting['type'] === 'general') {
66
				$tournament = new Tournament();
67
			}
68
			else {
69
				$tournament = new $setting['type'];
70
			}
71
			$root = $tournament;
72
73
			self::setTournament($tournament, $setting);
74
			foreach ($setting['categories'] ?? [] as $id) {
75
				$categories[$id] = $tournament;
76
			}
77
			foreach ($setting['rounds'] ?? [] as $id) {
78
				$rounds[$id] = $tournament;
79
			}
80
			foreach ($setting['groups'] ?? [] as $id) {
81
				$groups[$id] = $tournament;
82
			}
83
			foreach ($setting['teams'] ?? [] as $id) {
84
				$teams[$id] = $tournament;
85
			}
86
			if (isset($setting['games'])) {
87
				$tournament->getGameContainer()->setAutoIncrement(min($setting['games']));
88
			}
89
			foreach ($setting['games'] ?? [] as $id) {
90
				$games[$id] = $tournament;
91
			}
92
		}
93
94
		if (isset($data['categories'])) {
95
			foreach ($data['categories'] as $setting) {
96
				$setting = (array) $setting;
97
				$category = new Category($setting['name'] ?? '', $setting['id'] ?? null);
98
				if (!isset($root)) {
99
					$root = $category;
100
				}
101
				self::setSkip($category, $setting);
102
				if (isset($categories[$setting['id'] ?? $category->getId()])) {
103
					$categories[$setting['id'] ?? $category->getId()]->addCategory($category);
104
				}
105
				foreach ($setting['rounds'] ?? [] as $id) {
106
					$rounds[$id] = $category;
107
				}
108
				foreach ($setting['groups'] ?? [] as $id) {
109
					$groups[$id] = $category;
110
				}
111
				foreach ($setting['teams'] ?? [] as $id) {
112
					$teams[$id] = $category;
113
				}
114
				if (isset($setting['games']) && $root === $category) {
115
					$category->getGameContainer()->setAutoIncrement(min($setting['games']));
116
				}
117
				foreach ($setting['games'] ?? [] as $id) {
118
					$games[$id] = $category;
119
				}
120
			}
121
		}
122
123
		if (isset($data['rounds'])) {
124
			foreach ($data['rounds'] as $setting) {
125
				$setting = (array) $setting;
126
				$round = new Round($setting['name'] ?? '', $setting['id'] ?? null);
127
				if (!isset($root)) {
128
					$root = $round;
129
				}
130
				self::setSkip($round, $setting);
131
				if (isset($rounds[$setting['id'] ?? $round->getId()])) {
132
					$rounds[$setting['id'] ?? $round->getId()]->addRound($round);
133
				}
134
				foreach ($setting['groups'] ?? [] as $id) {
135
					$groups[$id] = $round;
136
				}
137
				foreach ($setting['teams'] ?? [] as $id) {
138
					$teams[$id] = $round;
139
				}
140
				if (isset($setting['games']) && $root === $round) {
141
					$round->getGameContainer()->setAutoIncrement(min($setting['games']));
142
				}
143
				foreach ($setting['games'] ?? [] as $id) {
144
					$games[$id] = $round;
145
				}
146
			}
147
		}
148
149
		$allGroups = [];
150
		if (isset($data['groups'])) {
151
			foreach ($data['groups'] as $setting) {
152
				$setting = (array) $setting;
153
				$group = new Group($setting['name'] ?? '', $setting['id'] ?? null);
154
				$allGroups[$group->getId()] = $group;
155
				if (!isset($root)) {
156
					$root = $group;
157
				}
158
				self::setSkip($group, $setting);
159
				self::setGroup($group, $setting);
160
				if (isset($groups[$setting['id'] ?? $group->getId()])) {
161
					$groups[$setting['id'] ?? $group->getId()]->addGroup($group);
162
				}
163
				foreach ($setting['teams'] ?? [] as $id) {
164
					$teams[$id] = $group;
165
				}
166
				if (isset($setting['games']) && $root === $group) {
167
					$group->getGameContainer()->setAutoIncrement(min($setting['games']));
168
				}
169
				foreach ($setting['games'] ?? [] as $id) {
170
					$games[$id] = $group;
171
				}
172
			}
173
		}
174
175
		if (isset($data['progressions'])) {
176
			foreach ($data['progressions'] as $setting) {
177
				$setting = (array) $setting;
178
179
				if (isset($setting['from'], $setting['to'], $allGroups[$setting['from']], $allGroups[$setting['to']])) {
180
					$progression = $allGroups[$setting['from']]->progression($allGroups[$setting['to']], $setting['offset'] ?? 0, $setting['length'] ?? null);
181
					if (isset($setting['filters'])) {
182
						foreach ($setting['filters'] as $filterSetting) {
183
							$filterSetting = (array) $filterSetting;
184
							$groups = array_map(static function($groupId) use ($allGroups) {
185
								return $allGroups[$groupId] ?? null;
186
							}, $filterSetting['groups']);
187
							$filter = new TeamFilter($filterSetting['what'], $filterSetting['how'], $filterSetting['val'], $groups);
188
							$progression->addFilter($filter);
189
						}
190
					}
191
					if (isset($setting['progressed'])) {
192
						$progression->setProgressed($setting['progressed']);
193
					}
194
				}
195
			}
196
		}
197
198
		$allTeams = [];
199
		if (isset($data['teams'])) {
200
			foreach ($data['teams'] as $setting) {
201
				$setting = (array) $setting;
202
				$team = new Team($setting['name'] ?? '', $setting['id'] ?? null);
203
				if (!isset($root)) {
204
					$root = $team;
205
				}
206
				$allTeams[$team->getId()] = $team;
207
				if (isset($teams[$setting['id'] ?? $team->getId()])) {
208
					$teams[$setting['id'] ?? $team->getId()]->addTeam($team);
209
				}
210
			}
211
		}
212
213
		if (isset($data['games'])) {
214
			foreach ($data['games'] as $setting) {
215
				$setting = (array) $setting;
216
				$gameTeams = [];
217
				foreach ($setting['teams'] ?? [] as $teamId) {
218
					$gameTeams[] = $allTeams[$teamId];
219
				}
220
				if (isset($setting['id'], $games[$setting['id']])) {
221
					$game = $games[$setting['id']]->game($gameTeams);
222
					if (isset($setting['scores'])) {
223
						$scores = array_map(static function($info) {
224
							return ((array) $info)['score'] ?? 0;
225
						}, $setting['scores']);
226
						$game->setResults($scores);
227
					}
228
				}
229
			}
230
		}
231
232
		return $root;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $root could return the type null which is incompatible with the type-hinted return TournamentGenerator\Base. Consider adding an additional type-check to rule them out.
Loading history...
233
	}
234
235
	/**
236
	 * @param Tournament $tournament
237
	 * @param array      $setting
238
	 */
239
	protected static function setTournament(Tournament $tournament, array $setting) : void {
240
		foreach ($setting as $key => $value) {
241
			switch ($key) {
242
				case 'name':
243
					$tournament->setName($value);
244
					break;
245
				case 'skip':
246
					$tournament->setSkip($value);
247
					break;
248
				case 'timing':
249
					self::setTiming($tournament, (array) $value);
250
					break;
251
			}
252
		}
253
	}
254
255
	/**
256
	 * @param Tournament $object
257
	 * @param array      $setting
258
	 */
259
	protected static function setTiming(Tournament $object, array $setting) : void {
260
		foreach ($setting as $key2 => $value2) {
261
			switch ($key2) {
262
				case 'play':
263
					$object->setPlay($value2);
264
					break;
265
				case 'gameWait':
266
					$object->setGameWait($value2);
267
					break;
268
				case 'categoryWait':
269
					$object->setCategoryWait($value2);
270
					break;
271
				case 'roundWait':
272
					$object->setRoundWait($value2);
273
					break;
274
			}
275
		}
276
	}
277
278
	protected static function setSkip(WithSkipSetters $category, array $setting) : void {
279
		if (isset($setting['skip'])) {
280
			$category->setSkip($setting['skip']);
281
		}
282
	}
283
284
	/**
285
	 * @param Group $group
286
	 * @param array $setting
287
	 *
288
	 * @throws Exception
289
	 */
290
	protected static function setGroup(Group $group, array $setting) : void {
291
		foreach ($setting as $key => $value) {
292
			switch ($key) {
293
				case 'type':
294
					$group->setType($value);
295
					break;
296
				case 'points':
297
					self::setPoints($group, (array) $value);
298
					break;
299
				case 'inGame':
300
					$group->setInGame($value);
301
					break;
302
				case 'maxSize':
303
					$group->setMaxSize($value);
304
					break;
305
			}
306
		}
307
	}
308
309
	protected static function setPoints(Group $object, array $setting) : void {
310
		foreach ($setting as $key2 => $value2) {
311
			switch ($key2) {
312
				case 'win':
313
					$object->setWinPoints($value2);
314
					break;
315
				case 'loss':
316
					$object->setLostPoints($value2);
317
					break;
318
				case 'draw':
319
					$object->setDrawPoints($value2);
320
					break;
321
				case 'second':
322
					$object->setSecondPoints($value2);
323
					break;
324
				case 'third':
325
					$object->setThirdPoints($value2);
326
					break;
327
				case 'progression':
328
					$object->setProgressPoints($value2);
329
					break;
330
			}
331
		}
332
	}
333
334
}