Total Complexity | 41 |
Total Lines | 514 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like Game 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 Game, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | #[ORM\Table(name:'vgr_game')] |
||
46 | #[ORM\Entity(repositoryClass: GameRepository::class)] |
||
47 | #[ORM\EntityListeners(["VideoGamesRecords\CoreBundle\EventListener\Entity\GameListener"])] |
||
48 | #[ORM\Index(name: "idx_lib_game_fr", columns: ["lib_game_fr"])] |
||
49 | #[ORM\Index(name: "idx_lib_game_en", columns: ["lib_game_en"])] |
||
50 | #[ORM\Index(name: "status", columns: ["status"])] |
||
51 | #[ApiResource( |
||
52 | operations: [ |
||
53 | new GetCollection(), |
||
54 | new GetCollection( |
||
55 | uriTemplate: '/games-list-by-letter', |
||
56 | controller: GetListByLetter::class, |
||
57 | normalizationContext: ['groups' => [ |
||
58 | 'game:read', 'game:platforms', 'platform:read'] |
||
59 | ], |
||
60 | openapi: new Model\Operation( |
||
61 | summary: 'Retrieves games by letter', |
||
62 | description: 'Retrieves games by letter' |
||
63 | ), |
||
64 | /*openapiContext: [ |
||
65 | 'parameters' => [ |
||
66 | [ |
||
67 | 'name' => 'letter', |
||
68 | 'in' => 'query', |
||
69 | 'type' => 'string', |
||
70 | 'required' => false |
||
71 | ] |
||
72 | ] |
||
73 | ]*/ |
||
74 | ), |
||
75 | new GetCollection( |
||
76 | uriTemplate: '/games/autocomplete', |
||
77 | controller: Autocomplete::class, |
||
78 | normalizationContext: ['groups' => [ |
||
79 | 'game:read'] |
||
80 | ], |
||
81 | openapi: new Model\Operation( |
||
82 | summary: 'Retrieves games by autocompletion', |
||
83 | description: 'Retrieves games by autocompletion' |
||
84 | ), |
||
85 | /*openapiContext: [ |
||
86 | 'parameters' => [ |
||
87 | [ |
||
88 | 'name' => 'query', |
||
89 | 'in' => 'query', |
||
90 | 'type' => 'string', |
||
91 | 'required' => true |
||
92 | ] |
||
93 | ] |
||
94 | ]*/ |
||
95 | ), |
||
96 | new Get( |
||
97 | normalizationContext: ['groups' => [ |
||
98 | 'game:read', |
||
99 | 'game:platforms', 'platform:read', |
||
100 | 'game:serie', 'serie:read', |
||
101 | 'game:rules', 'rule:read', |
||
102 | 'game:forum', 'forum:read' |
||
103 | ] |
||
104 | ] |
||
105 | ), |
||
106 | new Get( |
||
107 | uriTemplate: '/games/{id}/form-data', |
||
108 | controller: GetFormData::class, |
||
109 | security: "is_granted('ROLE_PLAYER')", |
||
110 | normalizationContext: ['groups' => [ |
||
111 | 'chart:read', 'chart:libs', 'chart-lib:read', |
||
112 | 'chart-lib:type', 'chart-type:read', |
||
113 | 'chart:group', 'group:read', |
||
114 | 'chart:player-charts', 'player-chart:read', 'player-chart:chart', |
||
115 | 'player-chart:libs', 'player-chart-lib:read', |
||
116 | 'player-chart:player', 'player-chart:platform', |
||
117 | 'player-chart:status', 'player-chart-status:read'] |
||
118 | ], |
||
119 | openapi: new Model\Operation( |
||
120 | summary: 'Fetch game form data', |
||
121 | description: 'Fetch game form data' |
||
122 | ), |
||
123 | /*openapiContext: [ |
||
124 | 'parameters' => [ |
||
125 | [ |
||
126 | 'name' => 'term', |
||
127 | 'in' => 'query', |
||
128 | 'type' => 'string', |
||
129 | 'required' => false |
||
130 | ] |
||
131 | ] |
||
132 | ]*/ |
||
133 | ), |
||
134 | new Get( |
||
135 | uriTemplate: '/games/{id}/player-ranking-points', |
||
136 | controller: PlayerGetRankingPoints::class, |
||
137 | normalizationContext: ['groups' => [ |
||
138 | 'player-game:read', |
||
139 | 'player-game:player', 'player:read', |
||
140 | 'player:team', 'team:read', |
||
141 | 'player:country', 'country:read'] |
||
142 | ], |
||
143 | openapi: new Model\Operation( |
||
144 | summary: 'Retrieves the player points leaderboard', |
||
145 | description: 'Retrieves the player points leaderboard' |
||
146 | ), |
||
147 | /*openapiContext: [ |
||
148 | 'parameters' => [ |
||
149 | [ |
||
150 | 'name' => 'maxRank', |
||
151 | 'in' => 'query', |
||
152 | 'type' => 'integer', |
||
153 | 'required' => false |
||
154 | ] |
||
155 | ] |
||
156 | ]*/ |
||
157 | ), |
||
158 | new Get( |
||
159 | uriTemplate: '/games/{id}/player-ranking-medals', |
||
160 | controller: PlayerGetRankingMedals::class, |
||
161 | normalizationContext: ['groups' => [ |
||
162 | 'player-game:read', |
||
163 | 'player-game:player', 'player:read', |
||
164 | 'player:team', 'team:read', |
||
165 | 'player:country', 'country:read'] |
||
166 | ], |
||
167 | openapi: new Model\Operation( |
||
168 | summary: 'Retrieves the player medals leaderboard', |
||
169 | description: 'Retrieves the player medals leaderboard' |
||
170 | ), |
||
171 | /*openapiContext: [ |
||
172 | 'parameters' => [ |
||
173 | [ |
||
174 | 'name' => 'maxRank', |
||
175 | 'in' => 'query', |
||
176 | 'type' => 'integer', |
||
177 | 'required' => false |
||
178 | ] |
||
179 | ] |
||
180 | ]*/ |
||
181 | ), |
||
182 | new Get( |
||
183 | uriTemplate: '/games/{id}/team-ranking-points', |
||
184 | controller: TeamGetRankingPoints::class, |
||
185 | normalizationContext: ['groups' => [ |
||
186 | 'team-game:read', |
||
187 | 'team-game:team', 'team:read'] |
||
188 | ], |
||
189 | openapi: new Model\Operation( |
||
190 | summary: 'Retrieves the team points leaderboard', |
||
191 | description: 'Retrieves the team points leaderboard' |
||
192 | ), |
||
193 | /*openapiContext: [ |
||
194 | 'parameters' => [ |
||
195 | [ |
||
196 | 'name' => 'maxRank', |
||
197 | 'in' => 'query', |
||
198 | 'type' => 'integer', |
||
199 | 'required' => false |
||
200 | ] |
||
201 | ] |
||
202 | ]*/ |
||
203 | ), |
||
204 | new Get( |
||
205 | uriTemplate: '/games/{id}/team-ranking-medals', |
||
206 | controller: TeamGetRankingMedals::class, |
||
207 | normalizationContext: ['groups' => [ |
||
208 | 'team-game:read', |
||
209 | 'team-game:team', 'team:read'] |
||
210 | ], |
||
211 | openapi: new Model\Operation( |
||
212 | summary: 'Retrieves the team medals leaderboard', |
||
213 | description: 'Retrieves the team medals leaderboard' |
||
214 | ), |
||
215 | /*openapiContext: [ |
||
216 | 'parameters' => [ |
||
217 | [ |
||
218 | 'name' => 'maxRank', |
||
219 | 'in' => 'query', |
||
220 | 'type' => 'integer', |
||
221 | 'required' => false |
||
222 | ] |
||
223 | ] |
||
224 | ]*/ |
||
225 | ), |
||
226 | ], |
||
227 | normalizationContext: ['groups' => ['game:read']] |
||
228 | )] |
||
229 | #[ApiFilter( |
||
230 | SearchFilter::class, |
||
231 | properties: [ |
||
232 | 'id' => 'exact', |
||
233 | 'status' => 'exact', |
||
234 | 'platforms' => 'exact', |
||
235 | 'playerGame.player' => 'exact', |
||
236 | 'groups.charts.lostPositions.player' => 'exact', |
||
237 | 'libGameEn' => 'partial', |
||
238 | 'libGameFr' => 'partial', |
||
239 | 'badge' => 'exact', |
||
240 | 'serie' => 'exact', |
||
241 | ] |
||
242 | )] |
||
243 | #[ApiFilter( |
||
244 | OrderFilter::class, |
||
245 | properties: [ |
||
246 | 'id' => 'ASC', |
||
247 | 'libGameEn' => 'ASC', |
||
248 | 'libGameFr' => 'ASC', |
||
249 | 'publishedAt' => 'DESC', |
||
250 | 'nbChart' => 'DESC', |
||
251 | 'nbPost' => 'DESC', |
||
252 | 'nbPlayer' => 'DESC', |
||
253 | 'nbVideo' => 'DESC', |
||
254 | 'lastUpdate' => 'DESC', |
||
255 | ] |
||
256 | )] |
||
257 | #[ApiFilter( |
||
258 | GroupFilter::class, |
||
259 | arguments: [ |
||
260 | 'parameterName' => 'groups', |
||
261 | 'overrideDefaultGroups' => true, |
||
262 | 'whitelist' => [ |
||
263 | 'game:read', |
||
264 | 'game:platforms', 'platform:read', |
||
265 | 'game:last-score', 'player-chart:read', |
||
266 | 'player-chart:player', 'player:read', |
||
267 | 'player-chart:chart', 'chart:read', |
||
268 | ] |
||
269 | ] |
||
270 | )] |
||
271 | #[ApiFilter(DateFilter::class, properties: ['publishedAt' => DateFilterInterface::INCLUDE_NULL_BEFORE_AND_AFTER])] |
||
272 | class Game implements SluggableInterface |
||
273 | { |
||
274 | use TimestampableEntity; |
||
275 | use SluggableTrait; |
||
276 | use NbChartTrait; |
||
277 | use NbPostTrait; |
||
278 | use NbPlayerTrait; |
||
279 | use NbTeamTrait; |
||
280 | use PictureTrait; |
||
281 | use NbVideoTrait; |
||
282 | use IsRankTrait; |
||
283 | use LastUpdateTrait; |
||
284 | |||
285 | #[ApiProperty(identifier: true)] |
||
286 | #[ORM\Id, ORM\Column, ORM\GeneratedValue] |
||
287 | protected ?int $id = null; |
||
288 | |||
289 | #[Assert\Length(max: 255)] |
||
290 | #[ORM\Column(length: 255, nullable: false)] |
||
291 | private string $libGameEn = ''; |
||
292 | |||
293 | #[Assert\Length(max: 255)] |
||
294 | #[ORM\Column(length: 255, nullable: false)] |
||
295 | private string $libGameFr = ''; |
||
296 | |||
297 | #[Assert\Length(max: 255)] |
||
298 | #[ORM\Column(length: 255, nullable: true)] |
||
299 | private ?string $downloadUrl; |
||
300 | |||
301 | #[ORM\Column(length: 30, nullable: false, options: ['default' => GameStatus::CREATED])] |
||
302 | private string $status = GameStatus::CREATED; |
||
303 | |||
304 | #[ORM\Column(nullable: true)] |
||
305 | private ?DateTime $publishedAt = null; |
||
306 | |||
307 | |||
308 | #[ORM\ManyToOne(targetEntity: Serie::class, inversedBy: 'games')] |
||
309 | #[ORM\JoinColumn(name:'serie_id', referencedColumnName:'id', nullable:true)] |
||
310 | private ?Serie $serie = null; |
||
311 | |||
312 | |||
313 | #[ORM\OneToOne(targetEntity: Badge::class, cascade: ['persist'], inversedBy: 'game')] |
||
314 | #[ORM\JoinColumn(name:'badge_id', referencedColumnName:'id', nullable:true)] |
||
315 | private ?Badge $badge = null; |
||
316 | |||
317 | /** |
||
318 | * @var Collection<int, Group> |
||
319 | */ |
||
320 | #[ORM\OneToMany(targetEntity: Group::class, cascade:['persist', 'remove'], mappedBy: 'game', orphanRemoval: true)] |
||
321 | private Collection $groups; |
||
322 | |||
323 | /** |
||
324 | * @var Collection<int, Platform> |
||
325 | */ |
||
326 | #[ORM\JoinTable(name: 'vgr_game_platform')] |
||
327 | #[ORM\JoinColumn(name: 'game_id', referencedColumnName: 'id')] |
||
328 | #[ORM\InverseJoinColumn(name: 'platform_id', referencedColumnName: 'id')] |
||
329 | #[ORM\ManyToMany(targetEntity: Platform::class, inversedBy: 'games')] |
||
330 | private Collection $platforms; |
||
331 | |||
332 | #[ORM\OneToOne(targetEntity: ForumInterface::class, cascade: ['persist'])] |
||
333 | #[ORM\JoinColumn(name:'forum_id', referencedColumnName:'id', nullable:true)] |
||
334 | private $forum; |
||
335 | |||
336 | #[ORM\OneToOne(targetEntity: PlayerChart::class)] |
||
337 | #[ORM\JoinColumn(name:'last_score_id', referencedColumnName:'id', nullable:true)] |
||
338 | private ?PlayerChart $lastScore; |
||
339 | |||
340 | /** |
||
341 | * @var Collection<int, Rule> |
||
342 | */ |
||
343 | #[ORM\ManyToMany(targetEntity: Rule::class, inversedBy: 'games')] |
||
344 | #[ORM\JoinTable(name: 'vgr_rule_game')] |
||
345 | private Collection $rules; |
||
346 | |||
347 | /** |
||
348 | * @var Collection<int, PlayerGame> |
||
349 | */ |
||
350 | #[ORM\OneToMany(targetEntity: PlayerGame::class, mappedBy: 'game')] |
||
351 | private Collection $playerGame; |
||
352 | |||
353 | |||
354 | public function __construct() |
||
355 | { |
||
356 | $this->groups = new ArrayCollection(); |
||
357 | $this->platforms = new ArrayCollection(); |
||
358 | $this->rules = new ArrayCollection(); |
||
359 | $this->playerGame = new ArrayCollection(); |
||
360 | } |
||
361 | |||
362 | public function __toString() |
||
363 | { |
||
364 | return sprintf('%s (%d)', $this->getName(), $this->getId()); |
||
365 | } |
||
366 | |||
367 | public function getDefaultName(): string |
||
368 | { |
||
369 | return $this->libGameEn; |
||
370 | } |
||
371 | |||
372 | public function getName(string $locale = null): ?string |
||
373 | { |
||
374 | if ($locale === null) { |
||
375 | $locale = Locale::getDefault(); |
||
376 | } |
||
377 | if ($locale == 'fr') { |
||
378 | return $this->libGameFr; |
||
379 | } else { |
||
380 | return $this->libGameEn; |
||
381 | } |
||
382 | } |
||
383 | |||
384 | public function setId(int $id): void |
||
385 | { |
||
386 | $this->id = $id; |
||
387 | } |
||
388 | |||
389 | public function getId(): ?int |
||
390 | { |
||
391 | return $this->id; |
||
392 | } |
||
393 | |||
394 | public function setLibGameEn(string $libGameEn): void |
||
395 | { |
||
396 | $this->libGameEn = $libGameEn; |
||
397 | } |
||
398 | |||
399 | public function getLibGameEn(): string |
||
400 | { |
||
401 | return $this->libGameEn; |
||
402 | } |
||
403 | |||
404 | public function setLibGameFr(?string $libGameFr): void |
||
405 | { |
||
406 | if ($libGameFr) { |
||
407 | $this->libGameFr = $libGameFr; |
||
408 | } |
||
409 | } |
||
410 | |||
411 | public function getLibGameFr(): string |
||
412 | { |
||
413 | return $this->libGameFr; |
||
414 | } |
||
415 | |||
416 | public function setDownloadurl(string $downloadUrl = null): void |
||
417 | { |
||
418 | $this->downloadUrl = $downloadUrl; |
||
419 | } |
||
420 | |||
421 | public function getDownloadUrl(): ?string |
||
422 | { |
||
423 | return $this->downloadUrl; |
||
424 | } |
||
425 | |||
426 | public function setStatus(string $status): void |
||
427 | { |
||
428 | $this->status = $status; |
||
429 | } |
||
430 | |||
431 | public function getStatus(): string |
||
432 | { |
||
433 | return $this->status; |
||
434 | } |
||
435 | |||
436 | public function getGameStatus(): GameStatus |
||
437 | { |
||
438 | return new GameStatus($this->status); |
||
439 | } |
||
440 | |||
441 | public function getStatusAsString(): string |
||
442 | { |
||
443 | return $this->status; |
||
444 | } |
||
445 | |||
446 | public function setPublishedAt(DateTime $pubishedAt = null): void |
||
447 | { |
||
448 | $this->publishedAt = $pubishedAt; |
||
449 | } |
||
450 | |||
451 | public function getPublishedAt(): ?DateTime |
||
452 | { |
||
453 | return $this->publishedAt; |
||
454 | } |
||
455 | |||
456 | public function setSerie(Serie $serie = null): void |
||
457 | { |
||
458 | $this->serie = $serie; |
||
459 | } |
||
460 | |||
461 | public function getSerie(): ?Serie |
||
462 | { |
||
463 | return $this->serie; |
||
464 | } |
||
465 | |||
466 | public function setBadge($badge = null): void |
||
467 | { |
||
468 | $this->badge = $badge; |
||
469 | } |
||
470 | |||
471 | public function getBadge(): ?Badge |
||
472 | { |
||
473 | return $this->badge; |
||
474 | } |
||
475 | |||
476 | public function addGroup(Group $group): void |
||
477 | { |
||
478 | $group->setGame($this); |
||
479 | $this->groups[] = $group; |
||
480 | } |
||
481 | |||
482 | public function removeGroup(Group $group): void |
||
483 | { |
||
484 | $this->groups->removeElement($group); |
||
485 | } |
||
486 | |||
487 | public function getGroups(): Collection |
||
488 | { |
||
489 | return $this->groups; |
||
490 | } |
||
491 | |||
492 | public function addPlatform(Platform $platform): void |
||
493 | { |
||
494 | $this->platforms[] = $platform; |
||
495 | } |
||
496 | |||
497 | public function removePlatform(Platform $platform): void |
||
498 | { |
||
499 | $this->platforms->removeElement($platform); |
||
500 | } |
||
501 | |||
502 | public function getPlatforms(): ArrayCollection|Collection |
||
503 | { |
||
504 | return $this->platforms; |
||
505 | } |
||
506 | |||
507 | public function getForum() |
||
508 | { |
||
509 | return $this->forum; |
||
510 | } |
||
511 | |||
512 | public function setForum($forum): void |
||
513 | { |
||
514 | $this->forum = $forum; |
||
515 | } |
||
516 | |||
517 | public function getLastScore(): ?PlayerChart |
||
518 | { |
||
519 | return $this->lastScore; |
||
520 | } |
||
521 | |||
522 | public function setLastScore(?PlayerChart $lastScore): void |
||
523 | { |
||
524 | $this->lastScore = $lastScore; |
||
525 | } |
||
526 | |||
527 | public function getSluggableFields(): array |
||
528 | { |
||
529 | return ['defaultName']; |
||
530 | } |
||
531 | |||
532 | public function getUrl(): string |
||
533 | { |
||
534 | return sprintf( |
||
535 | '%s-game-g%d/index', |
||
536 | $this->getSlug(), |
||
537 | $this->getId() |
||
538 | ); |
||
539 | } |
||
540 | |||
541 | public function addRule(Rule $rule): void |
||
542 | { |
||
543 | $this->rules[] = $rule; |
||
544 | } |
||
545 | |||
546 | public function removeRule(Rule $rule): void |
||
549 | } |
||
550 | |||
551 | public function getPlayerGame(): Collection |
||
552 | { |
||
553 | return $this->playerGame; |
||
554 | } |
||
555 | |||
556 | public function getRules(): Collection |
||
557 | { |
||
558 | return $this->rules; |
||
559 | } |
||
560 | } |
||
561 |