Completed
Branch develop (598d0f)
by Benjamin
03:23
created

Team::getNotDeadPlayers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Obblm\Core\Entity;
4
5
use Obblm\Core\Repository\TeamRepository;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\Common\Collections\Criteria;
9
use Doctrine\ORM\Mapping as ORM;
10
use Symfony\Component\Validator\Constraints as Assert;
11
use Symfony\Component\Validator\Context\ExecutionContextInterface;
12
use Symfony\Component\Validator\Mapping\ClassMetadata;
13
14
/**
15
 * @ORM\Entity(repositoryClass=TeamRepository::class)
16
 * @ORM\Table(name="obblm_team")
17
 */
18
class Team
19
{
20
    /**
21
     * @ORM\Id()
22
     * @ORM\GeneratedValue()
23
     * @ORM\Column(type="integer")
24
     */
25
    private $id;
26
27
    /**
28
     * @ORM\Column(type="string", length=255)
29
     */
30
    private $name;
31
32
    /**
33
     * @ORM\ManyToOne(targetEntity=Coach::class, inversedBy="teams")
34
     * @ORM\JoinColumn(nullable=false)
35
     */
36
    private $coach;
37
38
    /**
39
     * @ORM\ManyToOne(targetEntity=Rule::class, inversedBy="teams")
40
     */
41
    private $rule;
42
43
    /**
44
     * @ORM\Column(type="text", nullable=true)
45
     */
46
    private $anthem;
47
48
    /**
49
     * @ORM\Column(type="text", nullable=true)
50
     */
51
    private $fluff;
52
53
    /**
54
     * @ORM\Column(type="string", length=50)
55
     */
56
    private $roster;
57
58
    /**
59
     * @ORM\OneToMany(targetEntity=Player::class, fetch="EAGER", mappedBy="team", orphanRemoval=true, cascade={"persist", "remove"})
60
     * @ORM\OrderBy({"number"="ASC"})
61
     */
62
    private $players;
63
64
    /**
65
     * @ORM\Column(type="boolean")
66
     */
67
    private $ready = false;
68
69
    /**
70
     * @ORM\Column(type="boolean")
71
     */
72
    private $locked_by_managment = false;
73
74
    /**
75
     * @ORM\OneToMany(targetEntity=TeamVersion::class, fetch="EAGER", mappedBy="team", orphanRemoval=true, cascade={"persist", "remove"})
76
     * @ORM\OrderBy({"id"="DESC"})
77
     */
78
    private $versions;
79
80
    public function __construct()
81
    {
82
        $this->players = new ArrayCollection();
83
        $this->versions = new ArrayCollection();
84
    }
85
86
    public function getId(): ?int
87
    {
88
        return $this->id;
89
    }
90
91
    public function getName(): ?string
92
    {
93
        return $this->name;
94
    }
95
96
    public function setName(string $name): self
97
    {
98
        $this->name = $name;
99
100
        return $this;
101
    }
102
103
    public function getCoach(): ?Coach
104
    {
105
        return $this->coach;
106
    }
107
108
    public function setCoach(?Coach $coach): self
109
    {
110
        $this->coach = $coach;
111
112
        return $this;
113
    }
114
115
    public function getRule(): ?Rule
116
    {
117
        return $this->rule;
118
    }
119
120
    public function setRule(?Rule $rule): self
121
    {
122
        $this->rule = $rule;
123
124
        return $this;
125
    }
126
127
    public function getAnthem(): ?string
128
    {
129
        return $this->anthem;
130
    }
131
132
    public function setAnthem(?string $anthem): self
133
    {
134
        $this->anthem = $anthem;
135
136
        return $this;
137
    }
138
139
    public function getFluff(): ?string
140
    {
141
        return $this->fluff;
142
    }
143
144
    public function setFluff(?string $fluff): self
145
    {
146
        $this->fluff = $fluff;
147
148
        return $this;
149
    }
150
151
    public function getRoster(): ?string
152
    {
153
        return $this->roster;
154
    }
155
156
    public function setRoster(string $roster): self
157
    {
158
        $this->roster = $roster;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @return Collection|Player[]
165
     */
166
    public function getPlayers(): Collection
167
    {
168
        return $this->players;
169
    }
170
171
    /**
172
     * @return Collection|Player[]
173
     */
174
    public function getAvailablePlayers(): Collection
175
    {
176
        $criteria = Criteria::create()
177
            ->andWhere(Criteria::expr()->eq('dead', false))
178
            ->andWhere(Criteria::expr()->eq('fire', false))
179
            ->orderBy(['number' => 'ASC'])
180
        ;
181
        return $this->players->matching($criteria);
182
    }
183
184
    public function getAvailablePlayersSheet(): Collection
185
    {
186
        // In want to have 16 players in the list, no less, no more
187
        $used_numbers = [];
188
        $new_player_list = $this->getAvailablePlayers();
189
        foreach ($new_player_list as $player) {
190
            $used_numbers[$player->getNumber()] = $player;
191
        }
192
        for ($i=1; $i<=16; $i++) {
193
            if (!isset($used_numbers[$i])) {
194
                $new_player_list->add((new Player())->setNumber($i));
195
            }
196
        }
197
        $criteria = Criteria::create();
198
        $criteria->orderBy(['number' => 'ASC']);
199
        return $new_player_list->matching($criteria);
200
    }
201
202
    public function addPlayer(Player $player): self
203
    {
204
        if (!$this->players->contains($player)) {
205
            $this->players[] = $player;
206
            $player->setTeam($this);
207
        }
208
209
        return $this;
210
    }
211
212
    public function removePlayer(Player $player): self
213
    {
214
        if ($this->players->contains($player)) {
215
            $this->players->removeElement($player);
216
            // set the owning side to null (unless already changed)
217
            if ($player->getTeam() === $this) {
218
                $player->setTeam(null);
219
            }
220
        }
221
222
        return $this;
223
    }
224
225
    /*public static function loadValidatorMetadata(ClassMetadata $metadata)
226
    {
227
        $metadata->addConstraint(new Assert\Callback('validateRule'));
228
    }
229
230
    public function validateRule(ExecutionContextInterface $context, $payload)
231
    {
232
        if (!($context->getValue()->getRule() || $context->getValue()->getChampionship()) ||
233
            $context->getValue()->getRule() && $context->getValue()->getChampionship()) {
234
            $context->buildViolation('obblm.constraints.team.rule_or_championship.violation')
235
                ->addViolation();
236
        }
237
    }*/
238
239
    public function getReady(): ?bool
240
    {
241
        return $this->ready;
242
    }
243
244
    public function isReady(): ?bool
245
    {
246
        return $this->getReady();
247
    }
248
249
    public function setReady(bool $ready): self
250
    {
251
        $this->ready = $ready;
252
253
        return $this;
254
    }
255
256
    public function getLockedByManagment(): ?bool
257
    {
258
        return $this->locked_by_managment;
259
    }
260
261
    public function isLockedByManagment(): ?bool
262
    {
263
        return $this->getLockedByManagment();
264
    }
265
266
    public function setLockedByManagment(bool $locked_by_managment): self
267
    {
268
        $this->locked_by_managment = $locked_by_managment;
269
270
        return $this;
271
    }
272
273
    /**
274
     * @return Collection|TeamVersion[]
275
     */
276
    public function getVersions(): Collection
277
    {
278
        return $this->versions;
279
    }
280
281
    public function addVersion(TeamVersion $version): self
282
    {
283
        if (!$this->versions->contains($version)) {
284
            $this->versions[] = $version;
285
            $version->setTeam($this);
286
        }
287
288
        return $this;
289
    }
290
291
    public function removeVersion(TeamVersion $version): self
292
    {
293
        if ($this->versions->contains($version)) {
294
            $this->versions->removeElement($version);
295
            // set the owning side to null (unless already changed)
296
            if ($version->getTeam() === $this) {
297
                $version->setTeam(null);
298
            }
299
        }
300
301
        return $this;
302
    }
303
}
304