Total Complexity | 53 |
Total Lines | 323 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like RuleConfigBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RuleConfigBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class RuleConfigBuilder extends RuleConfigResolver implements RuleBuilderInterface |
||
24 | { |
||
25 | /** @var ArrayCollection */ |
||
26 | private $injuries; |
||
27 | /** @var ArrayCollection|Skill[] */ |
||
28 | private $skills; |
||
29 | /** @var ArrayCollection|InducementType[] */ |
||
30 | private $inducementTypes; |
||
31 | /** @var ArrayCollection */ |
||
32 | private $sppLevels; |
||
33 | /** @var ArrayCollection|InducementInterface[] */ |
||
34 | private $inducements; |
||
35 | /** @var ArrayCollection|RosterInterface[] */ |
||
36 | private $rosters; |
||
37 | |||
38 | protected function build(string $ruleKey, array $rule) |
||
39 | { |
||
40 | $treeResolver = new ConfigResolver($this); |
||
41 | $rule = $treeResolver->resolve($rule); |
||
42 | $this->prepareInjuriesTable($ruleKey, $rule); |
||
43 | $this->prepareSppTable($ruleKey, $rule); |
||
44 | $this->prepareSkillsTable($ruleKey, $rule); |
||
45 | $this->prepareInducementTypes($ruleKey, $rule); |
||
46 | $this->prepareInducementTable($ruleKey, $rule); |
||
47 | $this->prepareRosterTable($ruleKey, $rule); |
||
48 | } |
||
49 | |||
50 | public function getInjuries():ArrayCollection |
||
53 | } |
||
54 | |||
55 | public function setInjuries(ArrayCollection $injuries):self |
||
59 | } |
||
60 | |||
61 | public function getInducementTypes():ArrayCollection |
||
64 | } |
||
65 | |||
66 | public function setInducementTypes(ArrayCollection $inducementTypes):self |
||
70 | } |
||
71 | |||
72 | public function getInducementType(string $type):InducementType |
||
73 | { |
||
74 | return $this->getInducementTypes()->get($type); |
||
|
|||
75 | } |
||
76 | |||
77 | public function getSppLevels():ArrayCollection |
||
78 | { |
||
79 | return $this->sppLevels; |
||
80 | } |
||
81 | |||
82 | public function setSppLevels(ArrayCollection $sppLevels):self |
||
83 | { |
||
84 | $this->sppLevels = $sppLevels; |
||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | public function getInducementTable():ArrayCollection |
||
89 | { |
||
90 | return $this->inducements; |
||
91 | } |
||
92 | |||
93 | public function setInducementTable(ArrayCollection $inducements):self |
||
94 | { |
||
95 | $this->inducements = $inducements; |
||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | public function getRosters():ArrayCollection |
||
100 | { |
||
101 | return $this->rosters; |
||
102 | } |
||
103 | |||
104 | public function setRosters(ArrayCollection $rosters):self |
||
105 | { |
||
106 | $this->rosters = $rosters; |
||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public function getSkills():ArrayCollection |
||
111 | { |
||
112 | return $this->skills; |
||
113 | } |
||
114 | |||
115 | public function setSkills(ArrayCollection $skills):self |
||
116 | { |
||
117 | $this->skills = $skills; |
||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | private function prepareInjuriesTable(string $ruleKey, array $rule) |
||
122 | { |
||
123 | $this->injuries = new ArrayCollection(); |
||
124 | foreach ($rule['injuries'] as $key => $injury) { |
||
125 | $label = CoreTranslation::getInjuryKey($ruleKey, $key); |
||
126 | $effectLabel = CoreTranslation::getInjuryEffect($ruleKey, $key); |
||
127 | if (isset($injury['to'])) { |
||
128 | for ($i = $injury['from']; $i <= $injury['to']; $i++) { |
||
129 | $this->injuries->set( |
||
130 | $i, |
||
131 | (object) ['value' => $i, 'label' => $label, 'effect_label' => $effectLabel, 'effects' => $injury['effects']] |
||
132 | ); |
||
133 | } |
||
134 | } else { |
||
135 | $this->injuries->set( |
||
136 | $injury['from'], |
||
137 | (object) ['value' => $injury['from'], 'label' => $label, 'effect_label' => $effectLabel, 'effects' => $injury['effects']] |
||
138 | ); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | private function prepareInducementTypes(string $ruleKey, array $rule) |
||
144 | { |
||
145 | $this->inducementTypes = new ArrayCollection(); |
||
146 | $this->inducementTypes->set('star_players', new InducementType([ |
||
147 | 'key' => 'star_players', |
||
148 | 'translation_key' => CoreTranslation::getStarPlayerTitle($ruleKey), |
||
149 | 'translation_domain' => $ruleKey, |
||
150 | ])); |
||
151 | $this->inducementTypes->set('inducements', new InducementType([ |
||
152 | 'key' => 'inducements', |
||
153 | 'translation_key' => CoreTranslation::getInducementTitle($ruleKey), |
||
154 | 'translation_domain' => $ruleKey, |
||
155 | ])); |
||
156 | $this->inducementTypes->set('mercenary', new InducementType([ |
||
157 | 'key' => 'mercenary', |
||
158 | 'translation_key' => CoreTranslation::getMercenaryTitle($ruleKey), |
||
159 | 'translation_domain' => $ruleKey, |
||
160 | ])); |
||
161 | } |
||
162 | |||
163 | private function prepareSppTable(string $ruleKey, array $rule) |
||
164 | { |
||
165 | $spps = new ArrayCollection($rule['spp_levels']); |
||
166 | foreach ($spps as $from => $level) { |
||
167 | $to = $spps->next(); |
||
168 | if ($to) { |
||
169 | for ($i = $from; $i < $spps->indexOf($to); $i++) { |
||
170 | if (!isset($spps[$i])) { |
||
171 | $spps[$i] = $level; |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | $spps = $spps->toArray(); |
||
177 | ksort($spps); |
||
178 | $this->sppLevels = new ArrayCollection($spps); |
||
179 | } |
||
180 | |||
181 | private function prepareSkillsTable(string $ruleKey, array $rule) |
||
195 | ]) |
||
196 | ); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | private function prepareInducementTable(string $ruleKey, array $rule) |
||
202 | { |
||
203 | $this->inducements = new ArrayCollection(); |
||
204 | |||
205 | foreach ($rule['inducements'] as $key => $value) { |
||
206 | if ($key !== 'star_players') { |
||
207 | $inducement = new Inducement([ |
||
208 | 'type' => $this->inducementTypes['inducements'], |
||
209 | 'key' => join(CoreTranslation::TRANSLATION_GLUE, [$ruleKey, 'inducements', $key]), |
||
210 | 'translation_domain' => $ruleKey, |
||
211 | 'translation_key' => CoreTranslation::getInducementName($ruleKey, $key), |
||
212 | 'max' => $value['max'] ?? 0, |
||
213 | 'rosters' => $value['rosters'] ?? null, |
||
214 | 'value' => $value['cost'], |
||
215 | 'discount_cost' => $value['discount_cost'] ?? null, |
||
216 | ]); |
||
217 | if (!$this->inducements->contains($inducement)) { |
||
218 | $this->inducements->add($inducement); |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | foreach ($rule['star_players'] as $key => $starPlayer) { |
||
223 | $inducement = $this->createStarPlayerInducement($ruleKey, $key, $starPlayer); |
||
224 | if (!$this->inducements->contains($inducement)) { |
||
225 | $this->inducements->add($inducement); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | private function prepareRosterTable(string $ruleKey, array $rule) |
||
244 | ]) |
||
245 | ); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | private function createStarPlayerInducement(string $ruleKey, string $key, array $starPlayer):InducementInterface |
||
250 | { |
||
251 | $options = [ |
||
252 | 'type' => $this->inducementTypes['star_players'], |
||
253 | 'key' => join(CoreTranslation::TRANSLATION_GLUE, [$ruleKey, 'star_players', $key]), |
||
254 | 'value' => $starPlayer['cost'], |
||
255 | 'discount_cost' => $starPlayer['discount_cost'] ?? null, |
||
256 | 'characteristics' => $starPlayer['characteristics'] ?? null, |
||
257 | 'skills' => $starPlayer['skills'] ?? null, |
||
258 | 'rosters' => $starPlayer['rosters'] ?? null, |
||
259 | 'translation_domain' => $ruleKey, |
||
260 | 'translation_key' => CoreTranslation::getStarPlayerName($ruleKey, $key), |
||
261 | 'max' => $starPlayer['max'] ?? 1, |
||
262 | ]; |
||
263 | if (isset($starPlayer['multi_parts']) && $starPlayer['multi_parts']) { |
||
264 | $options['parts'] = []; |
||
265 | $first = true; |
||
266 | foreach ($starPlayer['multi_parts'] as $key => $part) { |
||
267 | $part['cost'] = $first ? $starPlayer['cost'] : 0; |
||
268 | $options['parts'][] = $this->createStarPlayerInducement($ruleKey, $key, $part); |
||
269 | $first = false; |
||
270 | } |
||
271 | $inducement = new MultipleStarPlayer($options); |
||
272 | } else { |
||
273 | $inducement = new StarPlayer($options); |
||
274 | } |
||
275 | return $inducement; |
||
276 | } |
||
277 | |||
278 | protected function getInducementExpression($options = ['type' => 'inducements']):CompositeExpression |
||
279 | { |
||
280 | $subExpressions = []; |
||
281 | if (isset($options['type'])) { |
||
282 | // Criteria by inducement type |
||
283 | if (is_array($options['type'])) { |
||
284 | $types = []; |
||
285 | foreach ($options['type'] as $type) { |
||
286 | if (is_string($type)) { |
||
287 | $inducementType = $this->getInducementType($type); |
||
288 | $types[] = Criteria::expr()->eq('type', $inducementType); |
||
289 | } |
||
290 | } |
||
291 | $subExpressions['type'] = new CompositeExpression(CompositeExpression::TYPE_OR, $types); |
||
292 | ; |
||
293 | } elseif (is_string($options['type'])) { |
||
294 | $inducementType = $this->getInducementType($options['type']); |
||
295 | $subExpressions['type'] = Criteria::expr()->eq('type', $inducementType); |
||
296 | } |
||
297 | } |
||
298 | if (isset($options['cost_limit'])) { |
||
299 | // Criteria by cost limit |
||
300 | if (is_int($options['cost_limit'])) { |
||
301 | $subExpressions['cost'] = Criteria::expr()->lte('value', $options['cost_limit']); |
||
302 | } |
||
303 | } |
||
304 | if (isset($options['roster'])) { |
||
305 | // Criteria by roster |
||
306 | if (is_string($options['roster'])) { |
||
307 | $subExpressions['roster'] = Criteria::expr()->orX( |
||
308 | Criteria::expr()->memberOf('rosters', $options['roster']), |
||
309 | Criteria::expr()->eq('rosters', []) |
||
310 | ); |
||
311 | } |
||
312 | } |
||
313 | return new CompositeExpression(CompositeExpression::TYPE_AND, $subExpressions); |
||
314 | } |
||
315 | |||
316 | public function getInducementsFor(Team $team, ?int $budget = null):array |
||
334 | } |
||
335 | |||
336 | public function getAllStarPlayers():ArrayCollection |
||
337 | { |
||
348 |