Completed
Branch v1.x-dev (5c2708)
by Benjamin
04:14
created

Team::getAvailablePlayers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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