SetupExporter::getCategoriesData()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TournamentGenerator\Export\Hierarchy;
5
6
7
use Exception;
8
use TournamentGenerator\Category;
9
use TournamentGenerator\Export\ExporterInterface;
10
use TournamentGenerator\Export\ExporterBase;
11
use TournamentGenerator\Group;
12
use TournamentGenerator\Interfaces\WithCategories;
13
use TournamentGenerator\Interfaces\WithGames;
14
use TournamentGenerator\Interfaces\WithGroups;
15
use TournamentGenerator\Interfaces\WithId;
16
use TournamentGenerator\Interfaces\WithRounds;
17
use TournamentGenerator\Interfaces\WithTeams;
18
use TournamentGenerator\Preset\Preset;
19
use TournamentGenerator\Progression;
20
use TournamentGenerator\Round;
21
use TournamentGenerator\TeamFilter;
22
use TournamentGenerator\Tournament;
23
24
/**
25
 * Class SetupExporter
26
 *
27
 * @package TournamentGenerator\Export
28
 * @author  Tomáš Vojík <[email protected]>
29
 * @since   0.5
30
 */
31
class SetupExporter extends ExporterBase
32
{
33
34
	/**
35
	 * @inheritDoc
36
	 */
37 2
	public static function export(WithId $object) : array {
38 2
		return self::start($object)->get();
39
	}
40
41
	/**
42
	 * @inheritDoc
43
	 */
44 6
	public static function start(WithId $object) : ExporterInterface {
45 6
		return new self($object);
46
	}
47
48
	/**
49
	 * Finish the export query -> get the result
50
	 *
51
	 * @return array The query result
52
	 * @throws Exception
53
	 */
54 6
	public function get() : array {
55 6
		$data = $this->getBasic();
56 6
		$this->applyModifiers($data);
57 6
		return $data;
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 * @throws Exception
63
	 */
64 6
	public function getBasic() : array {
65 6
		$data = [];
66 6
		$this->getTournamentData($data);
67 6
		$this->getCategoriesData($data);
68 6
		$this->getRoundsData($data);
69 6
		$this->getGroupsData($data);
70 6
		return $data;
71
	}
72
73
	/**
74
	 * Get all setup information from a Tournament class
75
	 *
76
	 * @param array $data
77
	 *
78
	 * @throws Exception
79
	 */
80 6
	protected function getTournamentData(array &$data) : void {
81 6
		if (!$this->object instanceof Tournament) {
82 2
			return;
83
		}
84 5
		$data['tournament'] = (object) [
85 5
			'type'       => $this->object instanceof Preset ? get_class($this->object) : 'general',
86 5
			'name'       => $this->object->getName(),
0 ignored issues
show
Bug introduced by
The method getName() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Base. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
			'name'       => $this->object->/** @scrutinizer ignore-call */ getName(),
Loading history...
87 5
			'skip'       => $this->object->getSkip(),
0 ignored issues
show
Bug introduced by
The method getSkip() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Round or TournamentGenerator\Group or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
			'skip'       => $this->object->/** @scrutinizer ignore-call */ getSkip(),
Loading history...
88
			'timing'     => (object) [
89 5
				'play'         => $this->object->getPlay(),
0 ignored issues
show
Bug introduced by
The method getPlay() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
				'play'         => $this->object->/** @scrutinizer ignore-call */ getPlay(),
Loading history...
90 5
				'gameWait'     => $this->object->getGameWait(),
0 ignored issues
show
Bug introduced by
The method getGameWait() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
				'gameWait'     => $this->object->/** @scrutinizer ignore-call */ getGameWait(),
Loading history...
91 5
				'categoryWait' => $this->object->getCategoryWait(),
0 ignored issues
show
Bug introduced by
The method getCategoryWait() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
				'categoryWait' => $this->object->/** @scrutinizer ignore-call */ getCategoryWait(),
Loading history...
92 5
				'roundWait'    => $this->object->getRoundWait(),
0 ignored issues
show
Bug introduced by
The method getRoundWait() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
				'roundWait'    => $this->object->/** @scrutinizer ignore-call */ getRoundWait(),
Loading history...
93 5
				'expectedTime' => $this->object->getTournamentTime(),
0 ignored issues
show
Bug introduced by
The method getTournamentTime() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
				'expectedTime' => $this->object->/** @scrutinizer ignore-call */ getTournamentTime(),
Loading history...
94
			],
95 5
			'categories' => $this->object instanceof WithCategories ? $this->object->queryCategories()->ids()->get() : [],
0 ignored issues
show
Bug introduced by
The method queryCategories() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
			'categories' => $this->object instanceof WithCategories ? $this->object->/** @scrutinizer ignore-call */ queryCategories()->ids()->get() : [],
Loading history...
96 5
			'rounds'     => $this->object instanceof WithRounds ? $this->object->queryRounds()->ids()->get() : [],
0 ignored issues
show
Bug introduced by
The method queryRounds() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
			'rounds'     => $this->object instanceof WithRounds ? $this->object->/** @scrutinizer ignore-call */ queryRounds()->ids()->get() : [],
Loading history...
97 5
			'groups'     => $this->object instanceof WithGroups ? $this->object->queryGroups()->ids()->get() : [],
0 ignored issues
show
Bug introduced by
The method queryGroups() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Round or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
			'groups'     => $this->object instanceof WithGroups ? $this->object->/** @scrutinizer ignore-call */ queryGroups()->ids()->get() : [],
Loading history...
98 5
			'teams'      => $this->object instanceof WithTeams ? $this->object->getTeamContainer()->ids()->unique()->get() : [],
0 ignored issues
show
Bug introduced by
The method getTeamContainer() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Round or TournamentGenerator\Group or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
			'teams'      => $this->object instanceof WithTeams ? $this->object->/** @scrutinizer ignore-call */ getTeamContainer()->ids()->unique()->get() : [],
Loading history...
99 5
			'games'      => $this->object instanceof WithGames ? $this->object->getGameContainer()->ids()->get() : [],
0 ignored issues
show
Bug introduced by
The method getGameContainer() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Round or TournamentGenerator\Group or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
			'games'      => $this->object instanceof WithGames ? $this->object->/** @scrutinizer ignore-call */ getGameContainer()->ids()->get() : [],
Loading history...
100
		];
101 5
	}
102
103
	/**
104
	 * Get all setup information for categories
105
	 *
106
	 * @param array $data
107
	 *
108
	 * @throws Exception
109
	 */
110 6
	protected function getCategoriesData(array &$data) : void {
111 6
		if ($this->object instanceof Category) {
112 1
			$data['categories'] = [
113 1
				$this->object->getId() => $this->getCategoryData($this->object),
114
			];
115
		}
116 6
		elseif ($this->object instanceof WithCategories) {
117 5
			$data['categories'] = [];
118 5
			foreach ($this->object->getCategories() as $category) {
0 ignored issues
show
Bug introduced by
The method getCategories() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
			foreach ($this->object->/** @scrutinizer ignore-call */ getCategories() as $category) {
Loading history...
119 1
				$data['categories'][$category->getId()] = $this->getCategoryData($category);
120
			}
121
		}
122 6
	}
123
124
	/**
125
	 * Get all setup information from a Category class
126
	 *
127
	 * @param Category $category Category class to export
128
	 *
129
	 * @return object
130
	 * @throws Exception
131
	 */
132 1
	protected function getCategoryData(Category $category) : object {
133
		return (object) [
134 1
			'id'     => $category->getId(),
135 1
			'name'   => $category->getName(),
136 1
			'skip'   => $category->getSkip(),
137 1
			'rounds' => $category instanceof WithRounds ? $category->queryRounds()->ids()->get() : [],
0 ignored issues
show
introduced by
$category is always a sub-type of TournamentGenerator\Interfaces\WithRounds.
Loading history...
138 1
			'groups' => $category instanceof WithGroups ? $category->queryGroups()->ids()->get() : [],
0 ignored issues
show
introduced by
$category is always a sub-type of TournamentGenerator\Interfaces\WithGroups.
Loading history...
139 1
			'teams'  => $category instanceof WithTeams ? $category->getTeamContainer()->ids()->unique()->get() : [],
0 ignored issues
show
introduced by
$category is always a sub-type of TournamentGenerator\Interfaces\WithTeams.
Loading history...
140 1
			'games'  => $category instanceof WithGames ? $category->getGameContainer()->ids()->get() : [],
0 ignored issues
show
introduced by
$category is always a sub-type of TournamentGenerator\Interfaces\WithGames.
Loading history...
141
		];
142
	}
143
144
	/**
145
	 * Get all setup information for rounds
146
	 *
147
	 * @param array $data
148
	 *
149
	 * @throws Exception
150
	 */
151 6
	protected function getRoundsData(array &$data) : void {
152 6
		if ($this->object instanceof Round) {
153 2
			$data['rounds'] = [
154 2
				$this->object->getId() => $this->getRoundData($this->object),
155
			];
156
		}
157 6
		elseif ($this->object instanceof WithRounds) {
158 5
			$data['rounds'] = [];
159 5
			foreach ($this->object->getRounds() as $round) {
0 ignored issues
show
Bug introduced by
The method getRounds() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
			foreach ($this->object->/** @scrutinizer ignore-call */ getRounds() as $round) {
Loading history...
160 5
				$data['rounds'][$round->getId()] = $this->getRoundData($round);
161
			}
162
		}
163 6
	}
164
165
	/**
166
	 * Get all setup information from a Round class
167
	 *
168
	 * @param Round $round Round class to export
169
	 *
170
	 * @return object
171
	 * @throws Exception
172
	 */
173 6
	protected function getRoundData(Round $round) : object {
174
		return (object) [
175 6
			'id'     => $round->getId(),
176 6
			'name'   => $round->getName(),
177 6
			'skip'   => $round->getSkip(),
178 6
			'played' => $round->isPlayed(),
179 6
			'groups' => $round instanceof WithGroups ? $round->queryGroups()->ids()->get() : [],
0 ignored issues
show
introduced by
$round is always a sub-type of TournamentGenerator\Interfaces\WithGroups.
Loading history...
180 6
			'teams'  => $round instanceof WithTeams ? $round->getTeamContainer()->ids()->unique()->get() : [],
0 ignored issues
show
introduced by
$round is always a sub-type of TournamentGenerator\Interfaces\WithTeams.
Loading history...
181 6
			'games'  => $round instanceof WithGames ? $round->getGameContainer()->ids()->get() : [],
0 ignored issues
show
introduced by
$round is always a sub-type of TournamentGenerator\Interfaces\WithGames.
Loading history...
182
		];
183
	}
184
185
	/**
186
	 * Get all setup information for groups and progressions
187
	 *
188
	 * @param array $data
189
	 *
190
	 * @throws Exception
191
	 */
192 6
	protected function getGroupsData(array &$data) : void {
193 6
		$data['groups'] = [];
194 6
		$data['progressions'] = [];
195 6
		if ($this->object instanceof Group) {
196 2
			$data['groups'][$this->object->getId()] = $this->getGroupData($this->object);
197 2
			foreach ($this->object->getProgressions() as $progression) {
0 ignored issues
show
Bug introduced by
The method getProgressions() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Group. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
			foreach ($this->object->/** @scrutinizer ignore-call */ getProgressions() as $progression) {
Loading history...
198 1
				$data['progressions'][] = $this->getProgressionData($progression);
199
			}
200
		}
201 6
		elseif ($this->object instanceof WithGroups) {
202 6
			foreach ($this->object->getGroups() as $group) {
0 ignored issues
show
Bug introduced by
The method getGroups() does not exist on TournamentGenerator\Interfaces\WithId. It seems like you code against a sub-type of TournamentGenerator\Interfaces\WithId such as TournamentGenerator\Tournament or TournamentGenerator\Round or TournamentGenerator\Category. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

202
			foreach ($this->object->/** @scrutinizer ignore-call */ getGroups() as $group) {
Loading history...
203 6
				$data['groups'][$group->getId()] = $this->getGroupData($group);
204 6
				foreach ($group->getProgressions() as $progression) {
205 5
					$data['progressions'][] = $this->getProgressionData($progression);
206
				}
207
			}
208
		}
209 6
	}
210
211
	/**
212
	 * Get all setup information from a Group class
213
	 *
214
	 * @param Group $group Group class to export
215
	 *
216
	 * @return object
217
	 * @throws Exception
218
	 */
219 6
	protected function getGroupData(Group $group) : object {
220
		return (object) [
221 6
			'id'      => $group->getId(),
222 6
			'name'    => $group->getName(),
223 6
			'type'    => $group->getType(),
224 6
			'skip'    => $group->getSkip(),
225
			'points'  => (object) [
226 6
				'win'         => $group->getWinPoints(),
227 6
				'loss'        => $group->getLostPoints(),
228 6
				'draw'        => $group->getDrawPoints(),
229 6
				'second'      => $group->getSecondPoints(),
230 6
				'third'       => $group->getThirdPoints(),
231 6
				'progression' => $group->getProgressPoints(),
232
			],
233 6
			'played'  => $group->isPlayed(),
234 6
			'inGame'  => $group->getInGame(),
235 6
			'maxSize' => $group->getMaxSize(),
236 6
			'teams'   => $group instanceof WithTeams ? $group->getTeamContainer()->ids()->unique()->get() : [],
0 ignored issues
show
introduced by
$group is always a sub-type of TournamentGenerator\Interfaces\WithTeams.
Loading history...
237 6
			'games'   => $group instanceof WithGames ? $group->getGameContainer()->ids()->get() : [],
0 ignored issues
show
introduced by
$group is always a sub-type of TournamentGenerator\Interfaces\WithGames.
Loading history...
238
		];
239
	}
240
241
	/**
242
	 * Get all setup information from a Progression class
243
	 *
244
	 * @param Progression $progression Progression class to export
245
	 *
246
	 * @return object
247
	 */
248 5
	protected function getProgressionData(Progression $progression) : object {
249
		return (object) [
250 5
			'from'       => $progression->getFrom()->getId(),
251 5
			'to'         => $progression->getTo()->getId(),
252 5
			'offset'     => $progression->getStart(),
253 5
			'length'     => $progression->getLen(),
254 5
			'progressed' => $progression->isProgressed(),
255 5
			'filters'    => array_map([$this, 'getTeamFilterData'], $progression->getFilters()),
256
		];
257
	}
258
259
	/**
260
	 * Get all setup information from a TeamFilter class
261
	 *
262
	 * @param TeamFilter $filter TeamFilter class to export
263
	 *
264
	 * @return object
265
	 */
266 1
	protected function getTeamFilterData(TeamFilter $filter) : object {
267
		return (object) [
268 1
			'what'   => $filter->getWhat(),
269 1
			'how'    => $filter->getHow(),
270 1
			'val'    => $filter->getVal(),
271 1
			'groups' => $filter->getGroups(),
272
		];
273
	}
274
}