Completed
Push — master ( a743c4...8e8354 )
by Tomáš
02:35
created

SetupExporter::export()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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

80
			'skip'       => $this->object->/** @scrutinizer ignore-call */ getSkip(),
Loading history...
81
			'timing'     => (object) [
82 3
				'play'         => $this->object->getPlay(),
0 ignored issues
show
Bug introduced by
The method getPlay() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

82
				'play'         => $this->object->/** @scrutinizer ignore-call */ getPlay(),
Loading history...
83 3
				'gameWait'     => $this->object->getGameWait(),
0 ignored issues
show
Bug introduced by
The method getGameWait() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

83
				'gameWait'     => $this->object->/** @scrutinizer ignore-call */ getGameWait(),
Loading history...
84 3
				'categoryWait' => $this->object->getCategoryWait(),
0 ignored issues
show
Bug introduced by
The method getCategoryWait() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

84
				'categoryWait' => $this->object->/** @scrutinizer ignore-call */ getCategoryWait(),
Loading history...
85 3
				'roundWait'    => $this->object->getRoundWait(),
0 ignored issues
show
Bug introduced by
The method getRoundWait() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

85
				'roundWait'    => $this->object->/** @scrutinizer ignore-call */ getRoundWait(),
Loading history...
86 3
				'expectedTime' => $this->object->getTournamentTime(),
0 ignored issues
show
Bug introduced by
The method getTournamentTime() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

86
				'expectedTime' => $this->object->/** @scrutinizer ignore-call */ getTournamentTime(),
Loading history...
87
			],
88 3
			'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\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

88
			'categories' => $this->object instanceof WithCategories ? $this->object->/** @scrutinizer ignore-call */ queryCategories()->ids()->get() : [],
Loading history...
89 3
			'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\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

89
			'rounds'     => $this->object instanceof WithRounds ? $this->object->/** @scrutinizer ignore-call */ queryRounds()->ids()->get() : [],
Loading history...
90 3
			'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\HierarchyBase. It seems like you code against a sub-type of said class. However, the method does not exist in TournamentGenerator\Group. Are you sure you never get one of those? ( Ignorable by Annotation )

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

90
			'groups'     => $this->object instanceof WithGroups ? $this->object->/** @scrutinizer ignore-call */ queryGroups()->ids()->get() : [],
Loading history...
91 3
			'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\HierarchyBase. Since it exists in all sub-types, consider adding an abstract or default implementation to TournamentGenerator\HierarchyBase. ( Ignorable by Annotation )

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

91
			'teams'      => $this->object instanceof WithTeams ? $this->object->/** @scrutinizer ignore-call */ getTeamContainer()->ids()->unique()->get() : [],
Loading history...
92 3
			'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\HierarchyBase. Since it exists in all sub-types, consider adding an abstract or default implementation to TournamentGenerator\HierarchyBase. ( Ignorable by Annotation )

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

92
			'games'      => $this->object instanceof WithGames ? $this->object->/** @scrutinizer ignore-call */ getGameContainer()->ids()->get() : [],
Loading history...
93
		];
94 3
	}
95
96
	/**
97
	 * Get all setup information for categories
98
	 *
99
	 * @param array $data
100
	 */
101 4
	protected function getCategoriesData(array &$data) : void {
102 4
		if ($this->object instanceof Category) {
103 1
			$data['categories'] = [
104 1
				$this->object->getId() => $this->getCategoryData($this->object),
105
			];
106
		}
107 4
		elseif ($this->object instanceof WithCategories) {
108 3
			$data['categories'] = [];
109 3
			foreach ($this->object->getCategories() as $category) {
0 ignored issues
show
Bug introduced by
The method getCategories() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

109
			foreach ($this->object->/** @scrutinizer ignore-call */ getCategories() as $category) {
Loading history...
110 1
				$data['categories'][$category->getId()] = $this->getCategoryData($category);
111
			}
112
		}
113 4
	}
114
115
	/**
116
	 * Get all setup information from a Category class
117
	 *
118
	 * @param Category $category Category class to export
119
	 *
120
	 * @return object
121
	 */
122 1
	protected function getCategoryData(Category $category) : object {
123
		return (object) [
124 1
			'id'     => $category->getId(),
125 1
			'name'   => $category->getName(),
126 1
			'skip'   => $category->getSkip(),
127 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...
128 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...
129 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...
130 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...
131
		];
132
	}
133
134
	/**
135
	 * Get all setup information for rounds
136
	 *
137
	 * @param array $data
138
	 */
139 4
	protected function getRoundsData(array &$data) : void {
140 4
		if ($this->object instanceof Round) {
141 2
			$data['rounds'] = [
142 2
				$this->object->getId() => $this->getRoundData($this->object),
143
			];
144
		}
145 4
		elseif ($this->object instanceof WithRounds) {
146 3
			$data['rounds'] = [];
147 3
			foreach ($this->object->getRounds() as $round) {
0 ignored issues
show
Bug introduced by
The method getRounds() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

147
			foreach ($this->object->/** @scrutinizer ignore-call */ getRounds() as $round) {
Loading history...
148 3
				$data['rounds'][$round->getId()] = $this->getRoundData($round);
149
			}
150
		}
151 4
	}
152
153
	/**
154
	 * Get all setup information from a Round class
155
	 *
156
	 * @param Round $round Round class to export
157
	 *
158
	 * @return object
159
	 */
160 4
	protected function getRoundData(Round $round) : object {
161
		return (object) [
162 4
			'id'     => $round->getId(),
163 4
			'name'   => $round->getName(),
164 4
			'skip'   => $round->getSkip(),
165 4
			'played' => $round->isPlayed(),
166 4
			'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...
167 4
			'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...
168 4
			'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...
169
		];
170
	}
171
172
	/**
173
	 * Get all setup information for groups and progressions
174
	 *
175
	 * @param array $data
176
	 */
177 4
	protected function getGroupsData(array &$data) : void {
178 4
		$data['groups'] = [];
179 4
		$data['progressions'] = [];
180 4
		if ($this->object instanceof Group) {
181 2
			$data['groups'][$this->object->getId()] = $this->getGroupData($this->object);
182 2
			foreach ($this->object->getProgressions() as $progression) {
0 ignored issues
show
Bug introduced by
The method getProgressions() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of TournamentGenerator\HierarchyBase 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

182
			foreach ($this->object->/** @scrutinizer ignore-call */ getProgressions() as $progression) {
Loading history...
183 1
				$data['progressions'][] = $this->getProgressionData($progression);
184
			}
185
		}
186 4
	elseif ($this->object instanceof WithGroups) {
187 4
			foreach ($this->object->getGroups() as $group) {
0 ignored issues
show
Bug introduced by
The method getGroups() does not exist on TournamentGenerator\HierarchyBase. It seems like you code against a sub-type of said class. However, the method does not exist in TournamentGenerator\Group. Are you sure you never get one of those? ( Ignorable by Annotation )

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

187
			foreach ($this->object->/** @scrutinizer ignore-call */ getGroups() as $group) {
Loading history...
188 4
				$data['groups'][$group->getId()] = $this->getGroupData($group);
189 4
				foreach ($group->getProgressions() as $progression) {
190 3
					$data['progressions'][] = $this->getProgressionData($progression);
191
				}
192
			}
193
		}
194 4
	}
195
196
	/**
197
	 * Get all setup information from a Group class
198
	 *
199
	 * @param Group $group Group class to export
200
	 *
201
	 * @return object
202
	 */
203 4
	protected function getGroupData(Group $group) : object {
204
		return (object) [
205 4
			'id'     => $group->getId(),
206 4
			'name'   => $group->getName(),
207 4
			'type' => $group->getType(),
208 4
			'skip'   => $group->getSkip(),
209
			'points' => (object) [
210 4
				'win' => $group->getWinPoints(),
211 4
				'loss' => $group->getLostPoints(),
212 4
				'draw' => $group->getDrawPoints(),
213 4
				'second' => $group->getSecondPoints(),
214 4
				'third' => $group->getThirdPoints(),
215 4
				'progression' => $group->getProgressPoints(),
216
			],
217 4
			'played' => $group->isPlayed(),
218 4
			'inGame' => $group->getInGame(),
219 4
			'maxSize' => $group->getMaxSize(),
220 4
			'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...
221 4
			'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...
222
		];
223
	}
224
225
	/**
226
	 * Get all setup information from a Progression class
227
	 *
228
	 * @param Progression $progression Progression class to export
229
	 *
230
	 * @return object
231
	 */
232 3
	protected function getProgressionData(Progression $progression) : object {
233
		return (object) [
234 3
			'from'     => $progression->getFrom()->getId(),
235 3
			'to'     => $progression->getTo()->getId(),
236 3
			'offset' => $progression->getStart(),
237 3
			'length' => $progression->getLen(),
238 3
			'progressed' => $progression->isProgressed(),
239 3
			'filters' => array_map([$this, 'getTeamFilterData'], $progression->getFilters()),
240
		];
241
	}
242
243
	/**
244
	 * Get all setup information from a TeamFilter class
245
	 *
246
	 * @param TeamFilter $filter TeamFilter class to export
247
	 *
248
	 * @return object
249
	 */
250 1
	protected function getTeamFilterData(TeamFilter $filter) : object {
251
		return (object) [
252 1
			'what'     => $filter->getWhat(),
253 1
			'how'     => $filter->getHow(),
254 1
			'val' => $filter->getVal(),
255 1
			'groups' => $filter->getGroups(),
256
		];
257
	}
258
}