Passed
Push — develop ( a84380...27260e )
by BENARD
06:07
created

Game::setBoolRanking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
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\Model\Sluggable\SluggableTrait;
16
use Gedmo\Timestampable\Traits\TimestampableEntity;
17
use Symfony\Component\Intl\Locale;
18
use Symfony\Component\Validator\Constraints as Assert;
19
use VideoGamesRecords\CoreBundle\Traits\Entity\IsRankTrait;
20
use VideoGamesRecords\CoreBundle\Traits\Entity\NbChartTrait;
21
use VideoGamesRecords\CoreBundle\Traits\Entity\NbPlayerTrait;
22
use VideoGamesRecords\CoreBundle\Traits\Entity\NbPostTrait;
23
use VideoGamesRecords\CoreBundle\Traits\Entity\NbTeamTrait;
24
use VideoGamesRecords\CoreBundle\Traits\Entity\NbVideoTrait;
25
use VideoGamesRecords\CoreBundle\Traits\Entity\PictureTrait;
26
use VideoGamesRecords\CoreBundle\ValueObject\GameStatus;
27
28
/**
29
 * Game
30
 *
31
 * @ORM\Table(
32
 *     name="vgr_game",
33
 *     indexes={
34
 *         @ORM\Index(name="idx_libGameFr", columns={"libGameFr"}),
35
 *         @ORM\Index(name="idx_libGameEn", columns={"libGameEn"}),
36
 *         @ORM\Index(name="idx_status", columns={"status"})
37
 *     }
38
 * )
39
 * @ORM\Entity(repositoryClass="VideoGamesRecords\CoreBundle\Repository\GameRepository")
40
 * @ORM\EntityListeners({"VideoGamesRecords\CoreBundle\EventListener\Entity\GameListener"})
41
 * @ApiFilter(
42
 *     SearchFilter::class,
43
 *     properties={
44
 *          "status": "exact",
45
 *          "platforms": "exact",
46
 *          "playerGame.player": "exact",
47
 *          "groups.charts.lostPositions.player": "exact",
48
 *          "libGameEn" : "partial",
49
 *          "libGameFr" : "partial",
50
 *          "badge": "exact",
51
 *      }
52
 * )
53
 * @ApiFilter(DateFilter::class, properties={"publishedAt": DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER})
54
 * @ApiFilter(
55
 *     GroupFilter::class,
56
 *     arguments={
57
 *          "parameterName": "groups",
58
 *          "overrideDefaultGroups": true,
59
 *          "whitelist": {"game.read.mini","game.list","game.platforms","platform.read"}
60
 *     }
61
 * )
62
 * @ApiFilter(
63
 *     OrderFilter::class,
64
 *     properties={
65
 *          "id":"ASC",
66
 *          "libGameEn" : "ASC",
67
 *          "libGameFr" : "ASC",
68
 *          "publishedAt": "DESC",
69
 *          "nbChart": "DESC",
70
 *          "nbPost": "DESC",
71
 *          "nbPlayer": "DESC",
72
 *          "nbVideo": "DESC"
73
 *     },
74
 *     arguments={"orderParameterName"="order"}
75
 * )
76
 */
77
class Game implements SluggableInterface
78
{
79
    use TimestampableEntity;
80
    use SluggableTrait;
81
    use NbChartTrait;
82
    use NbPostTrait;
83
    use NbPlayerTrait;
84
    use NbTeamTrait;
85
    use PictureTrait;
86
    use NbVideoTrait;
87
    use IsRankTrait;
88
89
    /**
90
     * @ORM\Column(name="id", type="integer")
91
     * @ORM\Id
92
     * @ORM\GeneratedValue(strategy="IDENTITY")
93
     */
94
    protected ?int $id = null;
95
96
    /**
97
     * @Assert\Length(max="255")
98
     * @ORM\Column(name="libGameEn", type="string", length=255, nullable=false)
99
     */
100
    private string $libGameEn = '';
101
102
    /**
103
     * @Assert\Length(max="255")
104
     * @ORM\Column(name="libGameFr", type="string", length=255, nullable=false)
105
     */
106
    private string $libGameFr = '';
107
108
    /**
109
     * @Assert\Length(max="255")
110
     * @ORM\Column(name="downloadUrl", type="string", length=255, nullable=true)
111
     */
112
    private ?string $downloadUrl;
113
114
    /**
115
     * @ORM\Column(name="status", type="string", length=30, nullable=false, options={"default":"CREATED"})
116
     */
117
    private string $status = GameStatus::STATUS_CREATED;
118
119
    /**
120
     * @ORM\Column(name="published_at", type="datetime", nullable=true)
121
     */
122
    private ?DateTime $publishedAt = null;
123
124
125
    /**
126
     * @ORM\ManyToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\Serie", inversedBy="games")
127
     * @ORM\JoinColumns({
128
     *   @ORM\JoinColumn(name="idSerie", referencedColumnName="id")
129
     * })
130
     */
131
    private ?Serie $serie;
132
133
    /**
134
     * @ORM\OneToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\Badge", inversedBy="game", cascade={"persist"}))
135
     * @ORM\JoinColumns({
136
     *   @ORM\JoinColumn(name="idBadge", referencedColumnName="id", nullable=true, onDelete="SET NULL")
137
     * })
138
     */
139
    private ?Badge $badge;
140
141
    /**
142
     * @var Collection<Group>
143
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\Group", mappedBy="game", cascade={"persist", "remove"}, orphanRemoval=true)
144
     */
145
    private Collection $groups;
146
147
    /**
148
     * @var Collection<Platform>
149
     * @ORM\ManyToMany(targetEntity="Platform", inversedBy="games")
150
     * @ORM\JoinTable(name="vgr_game_platform",
151
     *      joinColumns={@ORM\JoinColumn(name="idGame", referencedColumnName="id")},
152
     *      inverseJoinColumns={@ORM\JoinColumn(name="idPlatform", referencedColumnName="id")}
153
     *      )
154
     * @ORM\OrderBy({"libPlatform" = "ASC"})
155
     */
156
    private Collection $platforms;
157
158
159
    /**
160
     * @ORM\OneToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\ForumInterface",cascade={"persist"})
161
     * @ORM\JoinColumn(name="idForum", referencedColumnName="id")
162
     */
163
    private $forum;
164
165
    /**
166
     * @var Collection<Rule>
167
     * @ORM\ManyToMany(targetEntity="Rule", inversedBy="games")
168
     * @ORM\JoinTable(name="vgr_rule_game",
169
     *      joinColumns={@ORM\JoinColumn(name="idGame", referencedColumnName="id")},
170
     *      inverseJoinColumns={@ORM\JoinColumn(name="idRule", referencedColumnName="id")}
171
     *      )
172
     */
173
    private Collection $rules;
174
175
    /**
176
     * @var Collection<PlayerGame>
177
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\PlayerGame", mappedBy="game")
178
     */
179
    private Collection $playerGame;
180
181
182
    /**
183
     * Constructor
184
     */
185
    public function __construct()
186
    {
187
        $this->groups = new ArrayCollection();
188
        $this->platforms = new ArrayCollection();
189
        $this->rules = new ArrayCollection();
190
        $this->playerGame = new ArrayCollection();
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function __toString()
197
    {
198
        return sprintf('%s [%s]', $this->getDefaultName(), $this->id);
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getDefaultName(): string
205
    {
206
        return $this->libGameEn;
207
    }
208
209
    /**
210
     * @param string|null $locale
211
     * @return string|null
212
     */
213
    public function getName(string $locale = null): ?string
214
    {
215
        if ($locale === null) {
216
            $locale = Locale::getDefault();
217
        }
218
        if ($locale == 'fr') {
219
            return $this->libGameFr;
220
        } else {
221
            return $this->libGameEn;
222
        }
223
    }
224
225
    /**
226
     * Set id
227
     *
228
     * @param integer $id
229
     * @return Game
230
     */
231
    public function setId(int $id): Game
232
    {
233
        $this->id = $id;
234
        return $this;
235
    }
236
237
    /**
238
     * Get id
239
     *
240
     * @return int|null
241
     */
242
    public function getId(): ?int
243
    {
244
        return $this->id;
245
    }
246
247
    /**
248
     * @param string $libGameEn
249
     * @return Game
250
     */
251
    public function setLibGameEn(string $libGameEn): Game
252
    {
253
        $this->libGameEn = $libGameEn;
254
        return $this;
255
    }
256
257
    /**
258
     * @return string
259
     */
260
    public function getLibGameEn(): string
261
    {
262
        return $this->libGameEn;
263
    }
264
265
    /**
266
     * @param ?string $libGameFr
267
     * @return Game
268
     */
269
    public function setLibGameFr(?string $libGameFr): Game
270
    {
271
        if ($libGameFr) {
272
            $this->libGameFr = $libGameFr;
273
        }
274
        return $this;
275
    }
276
277
    /**
278
     * @return string
279
     */
280
    public function getLibGameFr(): string
281
    {
282
        return $this->libGameFr;
283
    }
284
285
    /**
286
     * @param string|null $downloadUrl
287
     * @return Game
288
     */
289
    public function setDownloadurl(string $downloadUrl = null): Game
290
    {
291
        $this->downloadUrl = $downloadUrl;
292
        return $this;
293
    }
294
295
    /**
296
     * @return string|null
297
     */
298
    public function getDownloadUrl(): ?string
299
    {
300
        return $this->downloadUrl;
301
    }
302
303
    /**
304
     * Set status
305
     *
306
     * @param string $status
307
     * @return Game
308
     */
309
    public function setStatus(string $status): Game
310
    {
311
        $this->status = $status;
312
313
        return $this;
314
    }
315
316
    /**
317
     * Get status
318
     *
319
     * @return GameStatus
320
     */
321
    public function getStatus(): GameStatus
322
    {
323
        return new GameStatus($this->status);
324
    }
325
326
    /**
327
     * Get status
328
     *
329
     * @return string
330
     */
331
    public function getStatusAsString(): string
332
    {
333
        return $this->status;
334
    }
335
336
    /**
337
     * @param DateTime|null $pubishedAt
338
     * @return Game
339
     */
340
    public function setPublishedAt(DateTime $pubishedAt = null): Game
341
    {
342
        $this->publishedAt = $pubishedAt;
343
344
        return $this;
345
    }
346
347
    /**
348
     * Get publishedAt
349
     * @return DateTime|null
350
     */
351
    public function getPublishedAt(): ?DateTime
352
    {
353
        return $this->publishedAt;
354
    }
355
356
357
    /**
358
     * Set Serie
359
     * @param Serie|null $serie
360
     * @return Game
361
     */
362
    public function setSerie(Serie $serie = null): Game
363
    {
364
        $this->serie = $serie;
365
366
        return $this;
367
    }
368
369
    /**
370
     * Get idSerie
371
     *
372
     * @return Serie
373
     */
374
    public function getSerie(): ?Serie
375
    {
376
        return $this->serie;
377
    }
378
379
    /**
380
     * Set badge
381
     *
382
     * @param $badge
383
     * @return Game
384
     */
385
    public function setBadge($badge = null): Game
386
    {
387
        $this->badge = $badge;
388
389
        return $this;
390
    }
391
392
    /**
393
     * Get idBadge
394
     * @return Badge|null
395
     */
396
    public function getBadge(): ?Badge
397
    {
398
        return $this->badge;
399
    }
400
401
    /**
402
     * @param Group $group
403
     * @return Game
404
     */
405
    public function addGroup(Group $group): Game
406
    {
407
        $group->setGame($this);
408
        $this->groups[] = $group;
409
        return $this;
410
    }
411
412
    /**
413
     * @param Group $group
414
     */
415
    public function removeGroup(Group $group)
416
    {
417
        $this->groups->removeElement($group);
418
    }
419
420
    /**
421
     * @return mixed
422
     */
423
    public function getGroups()
424
    {
425
        return $this->groups;
426
    }
427
428
    /**
429
     * @param Platform $platform
430
     * @return Game
431
     */
432
    public function addPlatform(Platform $platform): Game
433
    {
434
        $this->platforms[] = $platform;
435
        return $this;
436
    }
437
438
    /**
439
     * @param Platform $platform
440
     */
441
    public function removePlatform(Platform $platform)
442
    {
443
        $this->groups->removeElement($platform);
444
    }
445
446
    /**
447
     * @return mixed
448
     */
449
    public function getPlatforms()
450
    {
451
        return $this->platforms;
452
    }
453
454
455
    /**
456
     * @return ForumInterface
457
     */
458
    public function getForum()
459
    {
460
        return $this->forum;
461
    }
462
463
    /**
464
     * @param $forum
465
     * @return Game
466
     */
467
    public function setForum($forum): Game
468
    {
469
        $this->forum = $forum;
470
        return $this;
471
    }
472
473
    /**
474
     * Returns an array of the fields used to generate the slug.
475
     *
476
     * @return string[]
477
     */
478
    public function getSluggableFields(): array
479
    {
480
        return ['defaultName'];
481
    }
482
483
    /**
484
     * @return string
485
     */
486
    public function getUrl(): string
487
    {
488
        return sprintf(
489
            '%s-game-g%d/index',
490
            $this->getSlug(),
491
            $this->getId()
492
        );
493
    }
494
495
    /**
496
     * @param Rule $rule
497
     * @return Game
498
     */
499
    public function addRule(Rule $rule): Game
500
    {
501
        $this->rules[] = $rule;
502
        return $this;
503
    }
504
505
    /**
506
     * @param Rule $rule
507
     * @return Game
508
     */
509
    public function removeRule(Rule $rule): Game
510
    {
511
        $this->rules->removeElement($rule);
512
        return $this;
513
    }
514
515
    public function getPlaterGame()
516
    {
517
        return $this->playerGame;
518
    }
519
520
    /**
521
     * @return mixed
522
     */
523
    public function getRules()
524
    {
525
        return $this->rules;
526
    }
527
}
528