Passed
Push — develop ( 5d6d98...07983f )
by BENARD
05:05
created

Chart::setPlayerCharts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 5
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\OrderFilter;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
11
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
12
use Gedmo\Timestampable\Traits\TimestampableEntity;
13
use Symfony\Component\Intl\Locale;
14
use Symfony\Component\Validator\Constraints as Assert;
15
use VideoGamesRecords\CoreBundle\Traits\Entity\NbPostTrait;
16
use VideoGamesRecords\CoreBundle\ValueObject\ChartStatus;
17
18
/**
19
 * Chart
20
 *
21
 * @ORM\Table(
22
 *     name="vgr_chart",
23
 *     indexes={
24
 *         @ORM\Index(name="idx_libChartFr", columns={"libChartFr"}),
25
 *         @ORM\Index(name="idx_libChartEn", columns={"libChartEn"}),
26
 *         @ORM\Index(name="idx_status_player", columns={"statusPlayer"}),
27
 *         @ORM\Index(name="idx_status_team", columns={"statusTeam"})
28
 *     }
29
 * )
30
 * @ORM\Entity(repositoryClass="VideoGamesRecords\CoreBundle\Repository\ChartRepository")
31
 * @ORM\EntityListeners({"VideoGamesRecords\CoreBundle\EventListener\Entity\ChartListener"})
32
 * @ApiFilter(
33
 *     OrderFilter::class,
34
 *     properties={
35
 *          "id":"ASC",
36
 *          "libChartEn" : "ASC",
37
 *          "libChartFr" : "ASC",
38
 *     },
39
 *     arguments={"orderParameterName"="order"}
40
 * )
41
 */
42
class Chart implements SluggableInterface
43
{
44
    use TimestampableEntity;
45
    use SluggableTrait;
46
    use NbPostTrait;
47
48
    /**
49
     * @ORM\Column(name="id", type="integer")
50
     * @ORM\Id
51
     * @ORM\GeneratedValue(strategy="IDENTITY")
52
     */
53
    private ?int $id = null;
54
55
    /**
56
     * @Assert\Length(max="255")
57
     * @ORM\Column(name="libChartEn", type="string", length=255, nullable=false)
58
     */
59
    private string $libChartEn = '';
60
61
    /**
62
     * @Assert\Length(max="255")
63
     * @ORM\Column(name="libChartFr", type="string", length=255, nullable=false)
64
     */
65
    private string $libChartFr = '';
66
67
    /**
68
     * @ORM\Column(name="statusPlayer", type="string", length=30, nullable=false, options={"default" : "NORMAL"}))
69
     */
70
    private string $statusPlayer = 'NORMAL';
71
72
    /**
73
     * @ORM\Column(name="statusTeam", type="string", length=30, nullable=false, options={"default" : "NORMAL"}))
74
     */
75
    private string $statusTeam = 'NORMAL';
76
77
    /**
78
     * @Assert\NotNull
79
     * @ORM\ManyToOne(targetEntity="VideoGamesRecords\CoreBundle\Entity\Group", inversedBy="charts")
80
     * @ORM\JoinColumns({
81
     *   @ORM\JoinColumn(name="idGroup", referencedColumnName="id", nullable=false)
82
     * })
83
     */
84
    private Group $group;
85
86
    /**
87
     * @var Collection<ChartLib>
88
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\ChartLib", mappedBy="chart", cascade={"persist", "remove"}, orphanRemoval=true)
89
     */
90
    private Collection $libs;
91
92
    /**
93
     * @var Collection<PlayerChart>
94
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\PlayerChart", mappedBy="chart", fetch="EXTRA_LAZY")
95
     */
96
    private Collection $playerCharts;
97
98
    /**
99
     * Shortcut to playerChart.rank = 1
100
     */
101
    private ?PlayerChart $playerChart1 = null;
102
103
    /**
104
     * Shortcut to playerChart.player = player
105
     */
106
    private ?PlayerChart $playerChartP = null;
107
108
    /**
109
     * @var Collection<Proof>
110
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\Proof", mappedBy="chart", cascade={"persist", "remove"}, orphanRemoval=true)
111
     */
112
    private Collection $proofs;
113
114
    /**
115
     * @var Collection<LostPosition>
116
     * @ORM\OneToMany(targetEntity="VideoGamesRecords\CoreBundle\Entity\LostPosition", mappedBy="chart")
117
     */
118
    private Collection $lostPositions;
119
120
121
    /**
122
     * Constructor
123
     */
124
    public function __construct()
125
    {
126
        $this->libs = new ArrayCollection();
127
        $this->playerCharts = new ArrayCollection();
128
        $this->lostPositions = new ArrayCollection();
129
        $this->proofs = new ArrayCollection();
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function __toString()
136
    {
137
        return sprintf('%s [%s]', $this->getDefaultName(), $this->id);
138
    }
139
140
    /**
141
     * @return string|null
142
     */
143
    public function getDefaultName(): ?string
144
    {
145
        return $this->libChartEn;
146
    }
147
148
    /**
149
     * @return string|null
150
     */
151
    public function getName(): ?string
152
    {
153
        $locale = Locale::getDefault();
154
        if ($locale == 'fr') {
155
            return $this->libChartFr;
156
        } else {
157
            return $this->libChartEn;
158
        }
159
    }
160
161
    /**
162
     * @param string $locale
163
     * @return string
164
     */
165
    public function getCompleteName(string $locale = 'en'): string
166
    {
167
        if ($locale == 'fr') {
168
            return $this->getGroup()
169
                    ->getGame()
170
                    ->getLibGameFr() . ' - ' . $this->getGroup()
171
                    ->getLibGroupFr() . ' - ' . $this->getLibChartFr();
172
        } else {
173
            return $this->getGroup()
174
                    ->getGame()
175
                    ->getLibGameEn() . ' - ' . $this->getGroup()
176
                    ->getLibGroupEn() . ' - ' . $this->getLibChartEn();
177
        }
178
    }
179
180
    /**
181
     * Set idChart
182
     *
183
     * @param integer $id
184
     * @return Chart
185
     */
186
    public function setId(int $id): self
187
    {
188
        $this->id = $id;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Get idChart
195
     * @return int|null
196
     */
197
    public function getId(): ?int
198
    {
199
        return $this->id;
200
    }
201
202
    /**
203
     * @param string $libChartEn
204
     * @return $this
205
     */
206
    public function setLibChartEn(string $libChartEn): self
207
    {
208
        $this->libChartEn = $libChartEn;
209
        return $this;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getLibChartEn(): string
216
    {
217
        return $this->libChartEn;
218
    }
219
220
    /**
221
     * @param string|null $libChartFr
222
     * @return $this
223
     */
224
    public function setLibChartFr(?string $libChartFr): self
225
    {
226
        if ($libChartFr) {
227
            $this->libChartFr = $libChartFr;
228
        }
229
        return $this;
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getLibChartFr(): string
236
    {
237
        return $this->libChartFr;
238
    }
239
240
    /**
241
     * Set statusPlayer
242
     *
243
     * @param string $statusPlayer
244
     * @return Chart
245
     */
246
    public function setStatusPlayer(string $statusPlayer): self
247
    {
248
        $this->statusPlayer = $statusPlayer;
249
        return $this;
250
    }
251
252
    /**
253
     * Get statusPlayer
254
     *
255
     * @return ChartStatus
256
     */
257
    public function getStatusPlayer(): ChartStatus
258
    {
259
        return new ChartStatus($this->statusPlayer);
260
    }
261
262
263
    /**
264
     * Set statusTeam
265
     *
266
     * @param string $statusTeam
267
     * @return Chart
268
     */
269
    public function setStatusTeam(string $statusTeam): self
270
    {
271
        $this->statusTeam = $statusTeam;
272
        return $this;
273
    }
274
275
    /**
276
     * Get statusTeam
277
     *
278
     * @return ChartStatus
279
     */
280
    public function getStatusTeam(): ChartStatus
281
    {
282
        return new ChartStatus($this->statusTeam);
283
    }
284
285
    /**
286
     * @return Collection
287
     */
288
    public function getPlayerCharts(): Collection
289
    {
290
        return $this->playerCharts;
291
    }
292
293
    /**
294
     * @param PlayerChart $playerChart
295
     * @return $this
296
     */
297
    public function addPlayerChart(PlayerChart $playerChart): self
298
    {
299
        $this->playerCharts->add($playerChart);
300
301
        return $this;
302
    }
303
304
    /**
305
     * Set group
306
     * @param Group|null $group
307
     * @return Chart
308
     */
309
    public function setGroup(Group $group = null): Chart
310
    {
311
        $this->group = $group;
312
313
        return $this;
314
    }
315
316
    /**
317
     * Get group
318
     *
319
     * @return Group
320
     */
321
    public function getGroup(): Group
322
    {
323
        return $this->group;
324
    }
325
326
    /**
327
     * @param ChartLib $lib
328
     * @return $this
329
     */
330
    public function addLib(ChartLib $lib): Chart
331
    {
332
        $lib->setChart($this);
333
        $this->libs[] = $lib;
334
        return $this;
335
    }
336
337
    /**
338
     * @param ChartLib $lib
339
     */
340
    public function removeLib(ChartLib $lib): void
341
    {
342
        $this->libs->removeElement($lib);
343
    }
344
345
    /**
346
     * @return Collection
347
     */
348
    public function getLibs(): Collection
349
    {
350
        return $this->libs;
351
    }
352
353
354
    /**
355
     * @param PlayerChart|null $playerChart1
356
     */
357
    public function setPlayerChart1(?PlayerChart $playerChart1): void
358
    {
359
        $this->playerChart1 = $playerChart1;
360
    }
361
362
    /**
363
     * @return PlayerChart|null
364
     */
365
    public function getPlayerChart1(): ?PlayerChart
366
    {
367
        return $this->playerChart1;
368
    }
369
370
    /**
371
     * @param PlayerChart|null $playerChartP
372
     */
373
    public function setPlayerChartP(?PlayerChart $playerChartP): void
374
    {
375
        $this->playerChartP = $playerChartP;
376
    }
377
378
    /**
379
     * @return PlayerChart|null
380
     */
381
    public function getPlayerChartP(): ?PlayerChart
382
    {
383
        return $this->playerChartP;
384
    }
385
386
    /**
387
     * @return string
388
     */
389
    public function getUrl(): string
390
    {
391
        return sprintf(
392
            '%s-game-g%d/%s-group-g%d/%s-chart-c%d/index',
393
            $this->getGroup()->getGame()->getSlug(),
394
            $this->getGroup()->getGame()->getId(),
395
            $this->getGroup()->getSlug(),
396
            $this->getGroup()->getId(),
397
            $this->getSlug(),
398
            $this->getId()
399
        );
400
    }
401
402
403
    /**
404
     * Returns an array of the fields used to generate the slug.
405
     *
406
     * @return string[]
407
     */
408
    public function getSluggableFields(): array
409
    {
410
        return ['defaultName'];
411
    }
412
}
413