Passed
Branch develop (caeacb)
by BENARD
05:23
created

Game   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 670
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 52
eloc 113
c 0
b 0
f 0
dl 0
loc 670
rs 7.44

51 Methods

Rating   Name   Duplication   Size   Complexity  
A removeRule() 0 3 1
A __construct() 0 5 1
A setNbPlayer() 0 5 1
A setNbChart() 0 5 1
A getId() 0 3 1
A setBadge() 0 5 1
A __toString() 0 3 1
A getNbTeam() 0 3 1
A getPublishedAt() 0 3 1
A getPlatforms() 0 3 1
A addGroup() 0 5 1
A getBoolRanking() 0 3 1
A getPicture() 0 3 1
A setPicture() 0 5 1
A setNbPost() 0 5 1
A getGroups() 0 3 1
A getNbChart() 0 3 1
A addRule() 0 4 1
A getStatus() 0 3 1
A getDefaultName() 0 3 1
A setBoolRanking() 0 5 1
A setSerie() 0 5 1
A addPlatform() 0 4 1
A getVideos() 0 3 1
A getNbPlayer() 0 3 1
A setLibGameFr() 0 4 1
A getPlayerGame() 0 3 1
A getBadge() 0 3 1
A getForum() 0 3 1
A getUrl() 0 6 1
A getLibGameEn() 0 3 1
A setNbTeam() 0 5 1
A getSerie() 0 3 1
A getEtatsChoices() 0 7 1
A setForum() 0 4 1
A getNbPost() 0 3 1
A getEtat() 0 3 1
A getDays() 0 3 1
A setId() 0 4 1
A getLibGameFr() 0 3 1
A getName() 0 7 2
A setStatus() 0 5 1
A setLibGameEn() 0 4 1
A setBoolDlc() 0 5 1
A getBoolDlc() 0 3 1
A removeGroup() 0 3 1
A getRules() 0 3 1
A setEtat() 0 5 1
A getSluggableFields() 0 3 1
A removePlatform() 0 3 1
A setPublishedAt() 0 5 1

How to fix   Complexity   

Complex Class

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
2
3
namespace VideoGamesRecords\CoreBundle\Entity;
4
5
use ApiPlatform\Core\Annotation\ApiFilter;
6
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
7
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
8
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
9
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
10
use DateTime;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Collection;
13
use Doctrine\ORM\Mapping as ORM;
14
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
15
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
16
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
17
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
18
use Symfony\Component\Intl\Locale;
19
use Symfony\Component\Validator\Constraints as Assert;
20
use VideoGamesRecords\CoreBundle\ValueObject\GameStatus;
21
22
/**
23
 * Game
24
 *
25
 * @ORM\Table(name="vgr_game")
26
 * @ORM\Entity(repositoryClass="VideoGamesRecords\CoreBundle\Repository\GameRepository")
27
 * @ORM\EntityListeners({"VideoGamesRecords\CoreBundle\EventListener\Entity\GameListener"})
28
 * @ApiFilter(
29
 *     SearchFilter::class,
30
 *     properties={
31
 *          "status": "exact",
32
 *          "platforms": "exact",
33
 *          "playerGame.player": "exact",
34
 *          "groups.charts.lostPositions.player": "exact",
35
 *          "libGameEn" : "partial",
36
 *          "libGameFr" : "partial",
37
 *          "badge": "exact",
38
 *      }
39
 * )
40
 * @ApiFilter(DateFilter::class, properties={"publishedAt": DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER})
41
 * @ApiFilter(
42
 *     GroupFilter::class,
43
 *     arguments={
44
 *          "parameterName": "groups",
45
 *          "overrideDefaultGroups": true,
46
 *          "whitelist": {"game.read.mini","game.list","game.platforms","platform.read"}
47
 *     }
48
 * )
49
 * @ApiFilter(
50
 *     OrderFilter::class,
51
 *     properties={
52
 *          "id":"ASC",
53
 *          "libGameEn" : "ASC",
54
 *          "libGameFr" : "ASC",
55
 *          "publishedAt": "DESC",
56
 *          "nbChart": "DESC",
57
 *          "nbPost": "DESC",
58
 *          "nbPlayer": "DESC"
59
 *     },
60
 *     arguments={"orderParameterName"="order"}
61
 * )
62
 */
63
class Game implements SluggableInterface, TimestampableInterface
64
{
65
    use TimestampableTrait;
66
    use SluggableTrait;
67
68
    const ETAT_INIT = 'CREATION';
69
    const ETAT_CHART = 'RECORD';
70
    const ETAT_PICTURE = 'IMAGE';
71
    const ETAT_END = 'FINI';
72
73
74
    /**
75
     * @ORM\Column(name="id", type="integer")
76
     * @ORM\Id
77
     * @ORM\GeneratedValue(strategy="IDENTITY")
78
     */
79
    protected ?int $id = null;
80
81
    /**
82
     * @Assert\Length(max="255")
83
     * @ORM\Column(name="libGameEn", type="string", length=255, nullable=false)
84
     */
85
    private ?string $libGameEn;
86
87
    /**
88
     * @Assert\Length(max="255")
89
     * @ORM\Column(name="libGameFr", type="string", length=255, nullable=false)
90
     */
91
    private ?string $libGameFr = null;
92
93
    /**
94
     * @Assert\Length(max="200")
95
     * @ORM\Column(name="picture", type="string", length=200, nullable=true)
96
     */
97
    private ?string $picture;
98
99
    /**
100
     * @ORM\Column(name="status", type="string", nullable=false)
101
     */
102
    private string $status = GameStatus::STATUS_INACTIVE;
103
104
    /**
105
     * @ORM\Column(name="etat", type="string", nullable=false)
106
     */
107
    private string $etat = self::ETAT_INIT;
108
109
    /**
110
     * @ORM\Column(name="published_at", type="datetime", nullable=true)
111
     */
112
    private ?DateTime $publishedAt = null;
113
114
    /**
115
     * @ORM\Column(name="boolDlc", type="boolean", nullable=false, options={"default":0})
116
     */
117
    private bool $boolDlc = false;
118
119
    /**
120
     * @ORM\Column(name="boolRanking", type="boolean", nullable=true, options={"default":1})
121
     */
122
    private bool $boolRanking = true;
123
124
    /**
125
     * @ORM\Column(name="nbChart", type="integer", nullable=false, options={"default":0})
126
     */
127
    private int $nbChart = 0;
128
129
    /**
130
     * @ORM\Column(name="nbPost", type="integer", nullable=false, options={"default":0})
131
     */
132
    private int $nbPost = 0;
133
134
    /**
135
     * @ORM\Column(name="nbPlayer", type="integer", nullable=false, options={"default":0})
136
     */
137
    private int $nbPlayer = 0;
138
139
    /**
140
     * @ORM\Column(name="nbTeam", type="integer", nullable=false, options={"default":0})
141
     */
142
    private int $nbTeam = 0;
143
144
145
    /**
146
     * @ORM\ManyToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\Serie", inversedBy="games")
147
     * @ORM\JoinColumns({
148
     *   @ORM\JoinColumn(name="idSerie", referencedColumnName="id")
149
     * })
150
     */
151
    private ?Serie $serie;
152
153
    /**
154
     * @ORM\OneToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\Badge", inversedBy="game",cascade={"persist"}))
155
     * @ORM\JoinColumns({
156
     *   @ORM\JoinColumn(name="idBadge", referencedColumnName="id")
157
     * })
158
     */
159
    private ?Badge $badge;
160
161
    /**
162
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\Group", mappedBy="game", cascade={"persist", "remove"}, orphanRemoval=true)
163
     */
164
    private Collection $groups;
165
166
    /**
167
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\GameDay", mappedBy="game", cascade={"persist", "remove"}, orphanRemoval=true)
168
     */
169
    private $days;
170
171
172
    /**
173
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\Video", mappedBy="game", cascade={"persist", "remove"}, orphanRemoval=true)
174
     */
175
    private $videos;
176
177
    /**
178
     * @ORM\ManyToMany(targetEntity="Platform", inversedBy="games")
179
     * @ORM\JoinTable(name="vgr_game_platform",
180
     *      joinColumns={@ORM\JoinColumn(name="idGame", referencedColumnName="id")},
181
     *      inverseJoinColumns={@ORM\JoinColumn(name="idPlatform", referencedColumnName="id")}
182
     *      )
183
     * @ORM\OrderBy({"libPlatform" = "ASC"})
184
     */
185
    private $platforms;
186
187
    /**
188
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\PlayerGame", mappedBy="game")
189
     */
190
    private $playerGame;
191
192
    /**
193
     * @ORM\OneToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\ForumInterface",cascade={"persist"})
194
     * @ORM\JoinColumn(name="idForum", referencedColumnName="id")
195
     */
196
    private $forum;
197
198
    /**
199
     * @ORM\ManyToMany(targetEntity="Rule", inversedBy="games")
200
     * @ORM\JoinTable(name="vgr_rule_game",
201
     *      joinColumns={@ORM\JoinColumn(name="idGame", referencedColumnName="id")},
202
     *      inverseJoinColumns={@ORM\JoinColumn(name="idRule", referencedColumnName="id")}
203
     *      )
204
     */
205
    private $rules;
206
207
208
    /**
209
     * Constructor
210
     */
211
    public function __construct()
212
    {
213
        $this->groups = new ArrayCollection();
214
        $this->platforms = new ArrayCollection();
215
        $this->rules = new ArrayCollection();
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function __toString()
222
    {
223
        return sprintf('%s [%s]', $this->getDefaultName(), $this->id);
224
    }
225
226
    /**
227
     * @return string
228
     */
229
    public function getDefaultName(): string
230
    {
231
        return $this->libGameEn;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->libGameEn could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function getName(): string
238
    {
239
        $locale = Locale::getDefault();
240
        if ($locale == 'fr') {
241
            return $this->libGameFr;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->libGameFr could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
242
        } else {
243
            return $this->libGameEn;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->libGameEn could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
244
        }
245
    }
246
247
    /**
248
     * Set id
249
     *
250
     * @param integer $id
251
     * @return Game
252
     */
253
    public function setId(int $id)
254
    {
255
        $this->id = $id;
256
        return $this;
257
    }
258
259
    /**
260
     * Get id
261
     *
262
     * @return integer
263
     */
264
    public function getId()
265
    {
266
        return $this->id;
267
    }
268
269
    /**
270
     * @param string $libGameEn
271
     * @return Game
272
     */
273
    public function setLibGameEn(string $libGameEn): Game
274
    {
275
        $this->libGameEn = $libGameEn;
276
        return $this;
277
    }
278
279
    /**
280
     * @return string|null
281
     */
282
    public function getLibGameEn(): ?string
283
    {
284
        return $this->libGameEn;
285
    }
286
287
    /**
288
     * @param string $libGameFr
289
     * @return Game
290
     */
291
    public function setLibGameFr(string $libGameFr): Game
292
    {
293
        $this->libGameFr = $libGameFr;
294
        return $this;
295
    }
296
297
    /**
298
     * @return string|null
299
     */
300
    public function getLibGameFr(): ?string
301
    {
302
        return $this->libGameFr;
303
    }
304
305
306
    /**
307
     * Set picture
308
     *
309
     * @param string|null $picture
310
     * @return Game
311
     */
312
    public function setPicture(string $picture = null): Game
313
    {
314
        $this->picture = $picture;
315
316
        return $this;
317
    }
318
319
    /**
320
     * Get picture
321
     * @return string|null
322
     */
323
    public function getPicture(): ?string
324
    {
325
        return $this->picture;
326
    }
327
328
    /**
329
     * Set status
330
     *
331
     * @param string $status
332
     * @return Game
333
     */
334
    public function setStatus(string $status): Game
335
    {
336
        $this->status = $status;
337
338
        return $this;
339
    }
340
341
    /**
342
     * Get status
343
     *
344
     * @return GameStatus
345
     */
346
    public function getStatus(): GameStatus
347
    {
348
        return new GameStatus($this->status);
349
    }
350
351
    /**
352
     * Set etat
353
     *
354
     * @param string $etat
355
     * @return Game
356
     */
357
    public function setEtat(string $etat): Game
358
    {
359
        $this->etat = $etat;
360
361
        return $this;
362
    }
363
364
    /**
365
     * Get etat
366
     *
367
     * @return string
368
     */
369
    public function getEtat(): string
370
    {
371
        return $this->etat;
372
    }
373
374
    /**
375
     * @param DateTime|null $pubishedAt
376
     * @return Game
377
     */
378
    public function setPublishedAt(DateTime $pubishedAt = null): Game
379
    {
380
        $this->publishedAt = $pubishedAt;
381
382
        return $this;
383
    }
384
385
    /**
386
     * Get publishedAt
387
     *
388
     * @return DateTime
389
     */
390
    public function getPublishedAt(): ?DateTime
391
    {
392
        return $this->publishedAt;
393
    }
394
395
    /**
396
     * Set boolDlc
397
     *
398
     * @param bool $boolDlc
399
     * @return Game
400
     */
401
    public function setBoolDlc(bool $boolDlc): Game
402
    {
403
        $this->boolDlc = $boolDlc;
404
405
        return $this;
406
    }
407
408
    /**
409
     * Get boolDlc
410
     *
411
     * @return bool
412
     */
413
    public function getBoolDlc(): bool
414
    {
415
        return $this->boolDlc;
416
    }
417
418
    /**
419
     * Set boolRanking
420
     *
421
     * @param bool $boolRanking
422
     * @return Game
423
     */
424
    public function setBoolRanking(bool $boolRanking): Game
425
    {
426
        $this->boolRanking = $boolRanking;
427
428
        return $this;
429
    }
430
431
    /**
432
     * Get boolRanking
433
     *
434
     * @return bool
435
     */
436
    public function getBoolRanking(): bool
437
    {
438
        return $this->boolRanking;
439
    }
440
441
    /**
442
     * Set nbChart
443
     *
444
     * @param integer $nbChart
445
     * @return Game
446
     */
447
    public function setNbChart(int $nbChart): Game
448
    {
449
        $this->nbChart = $nbChart;
450
451
        return $this;
452
    }
453
454
    /**
455
     * Get nbChart
456
     *
457
     * @return integer
458
     */
459
    public function getNbChart(): int
460
    {
461
        return $this->nbChart;
462
    }
463
464
    /**
465
     * Set nbPost
466
     *
467
     * @param integer $nbPost
468
     * @return Game
469
     */
470
    public function setNbPost(int $nbPost): Game
471
    {
472
        $this->nbPost = $nbPost;
473
474
        return $this;
475
    }
476
477
    /**
478
     * Get nbPost
479
     *
480
     * @return integer
481
     */
482
    public function getNbPost(): int
483
    {
484
        return $this->nbPost;
485
    }
486
487
    /**
488
     * Set nbPlayer
489
     *
490
     * @param integer $nbPlayer
491
     * @return Game
492
     */
493
    public function setNbPlayer(int $nbPlayer): Game
494
    {
495
        $this->nbPlayer = $nbPlayer;
496
497
        return $this;
498
    }
499
500
    /**
501
     * Get nbPlayer
502
     *
503
     * @return integer
504
     */
505
    public function getNbPlayer(): int
506
    {
507
        return $this->nbPlayer;
508
    }
509
510
    /**
511
     * Set nbTeam
512
     *
513
     * @param integer $nbTeam
514
     * @return Game
515
     */
516
    public function setNbTeam(int $nbTeam): Game
517
    {
518
        $this->nbTeam = $nbTeam;
519
520
        return $this;
521
    }
522
523
    /**
524
     * Get nbTeam
525
     *
526
     * @return integer
527
     */
528
    public function getNbTeam(): int
529
    {
530
        return $this->nbTeam;
531
    }
532
533
    /**
534
     * Set Serie
535
     * @param Serie|null $serie
536
     * @return Game
537
     */
538
    public function setSerie(Serie $serie = null): Game
539
    {
540
        $this->serie = $serie;
541
542
        return $this;
543
    }
544
545
    /**
546
     * Get idSerie
547
     *
548
     * @return Serie
549
     */
550
    public function getSerie(): ?Serie
551
    {
552
        return $this->serie;
553
    }
554
555
    /**
556
     * Set badge
557
     *
558
     * @param $badge
559
     * @return Game
560
     */
561
    public function setBadge($badge = null): Game
562
    {
563
        $this->badge = $badge;
564
565
        return $this;
566
    }
567
568
    /**
569
     * Get idBadge
570
     *
571
     * @return Badge
572
     */
573
    public function getBadge(): ?Badge
574
    {
575
        return $this->badge;
576
    }
577
578
    /**
579
     * @param Group $group
580
     * @return Game
581
     */
582
    public function addGroup(Group $group): Game
583
    {
584
        $group->setGame($this);
585
        $this->groups[] = $group;
586
        return $this;
587
    }
588
589
    /**
590
     * @param Group $group
591
     */
592
    public function removeGroup(Group $group)
593
    {
594
        $this->groups->removeElement($group);
595
    }
596
597
    /**
598
     * @return mixed
599
     */
600
    public function getGroups()
601
    {
602
        return $this->groups;
603
    }
604
605
    /**
606
     * @return mixed
607
     */
608
    public function getDays()
609
    {
610
        return $this->days;
611
    }
612
613
    /**
614
     * @return mixed
615
     */
616
    public function getVideos()
617
    {
618
        return $this->videos;
619
    }
620
621
    /**
622
     * @param Platform $platform
623
     * @return Game
624
     */
625
    public function addPlatform(Platform $platform): Game
626
    {
627
        $this->platforms[] = $platform;
628
        return $this;
629
    }
630
631
    /**
632
     * @param Platform $platform
633
     */
634
    public function removePlatform(Platform $platform)
635
    {
636
        $this->groups->removeElement($platform);
637
    }
638
639
    /**
640
     * @return mixed
641
     */
642
    public function getPlatforms()
643
    {
644
        return $this->platforms;
645
    }
646
647
    /**
648
     * @return mixed
649
     */
650
    public function getPlayerGame()
651
    {
652
        return $this->playerGame;
653
    }
654
655
    /**
656
     * @return ForumInterface
657
     */
658
    public function getForum()
659
    {
660
        return $this->forum;
661
    }
662
663
    /**
664
     * @param $forum
665
     * @return Game
666
     */
667
    public function setForum($forum): Game
668
    {
669
        $this->forum = $forum;
670
        return $this;
671
    }
672
673
674
    /**
675
     * @return array
676
     */
677
    public static function getEtatsChoices(): array
678
    {
679
        return [
680
            self::ETAT_INIT => self::ETAT_INIT,
681
            self::ETAT_CHART => self::ETAT_CHART,
682
            self::ETAT_PICTURE => self::ETAT_PICTURE,
683
            self::ETAT_END => self::ETAT_END,
684
        ];
685
    }
686
687
    /**
688
     * Returns an array of the fields used to generate the slug.
689
     *
690
     * @return string[]
691
     */
692
    public function getSluggableFields(): array
693
    {
694
        return ['defaultName'];
695
    }
696
697
    /**
698
     * @return string
699
     */
700
    public function getUrl(): string
701
    {
702
        return sprintf(
703
            '%s-game-g%d/index',
704
            $this->getSlug(),
705
            $this->getId()
706
        );
707
    }
708
709
    /**
710
     * @param Rule $rule
711
     * @return Game
712
     */
713
    public function addRule(Rule $rule): Game
714
    {
715
        $this->rules[] = $rule;
716
        return $this;
717
    }
718
719
    /**
720
     * @param Rule $rule
721
     */
722
    public function removeRule(Rule $rule)
723
    {
724
        $this->rules->removeElement($rule);
725
    }
726
727
    /**
728
     * @return mixed
729
     */
730
    public function getRules()
731
    {
732
        return $this->rules;
733
    }
734
}
735