Passed
Push — master ( 32373f...f18c17 )
by Benjamin
05:50 queued 01:43
created

Team::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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