Team::createFinalPosition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
namespace OSS\CoreBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
8
/**
9
 * @ORM\Entity
10
 */
11
class Team
12
{
13
    /**
14
     * @var int
15
     *
16
     * @ORM\Id
17
     * @ORM\GeneratedValue
18
     * @ORM\Column(type="integer")
19
     */
20
    private $id;
21
22
    /**
23
     * @var string
24
     *
25
     * @ORM\Column(type="string")
26
     */
27
    private $name;
28
29
    /**
30
     * @var League
31
     *
32
     * @ORM\ManyToOne(targetEntity="League", inversedBy="teams")
33
     * @ORM\JoinColumn(nullable=false)
34
     */
35
    private $league;
36
37
    /**
38
     * @var int
39
     *
40
     * @ORM\Column(type="integer")
41
     */
42
    private $points = 0;
43
44
    /**
45
     * @var int
46
     *
47
     * @ORM\Column(type="integer")
48
     */
49
    private $goalsFor = 0;
50
51
    /**
52
     * @var int
53
     *
54
     * @ORM\Column(type="integer")
55
     */
56
    private $goalsAgainst = 0;
57
58
    /**
59
     * @var Player[]|ArrayCollection
60
     *
61
     * @ORM\OneToMany(targetEntity="Player", mappedBy="team")
62
     */
63
    private $players;
64
65
    /**
66
     * @var int
67
     *
68
     * @ORM\Column(type="integer")
69
     */
70
    private $money;
71
72
    /**
73
     * @var Trainer
74
     *
75
     * @ORM\OneToOne(targetEntity="Trainer", mappedBy="team")
76
     */
77
    private $trainer;
78
79
    /**
80
     * @var Manager
81
     *
82
     * @ORM\OneToOne(targetEntity="Manager", mappedBy="team")
83
     */
84
    private $manager;
85
86
    /**
87
     * @var FinalPosition[]|ArrayCollection
88
     *
89
     * @ORM\OneToMany(targetEntity="FinalPosition", mappedBy="team", cascade={"all"})
90
     */
91
    private $finalPositions;
92
93
    /**
94
     * @var Transfer[]
95
     *
96
     * @ORM\OneToMany(targetEntity="Transfer", mappedBy="targetTeam")
97
     */
98
    private $transfersIncoming;
99
100
    /**
101
     * @var Transfer[]
102
     *
103
     * @ORM\OneToMany(targetEntity="Transfer", mappedBy="originTeam")
104
     */
105
    private $transfersOutgoing;
106
107 53
    public function __construct()
108
    {
109 53
        $this->players = new ArrayCollection();
110 53
        $this->finalPositions = new ArrayCollection();
111 53
    }
112
113
    /**
114
     * @return int
115
     */
116 19
    public function getId()
117
    {
118 19
        return $this->id;
119
    }
120
121
    /**
122
     * @param Team $team
123
     *
124
     * @return bool
125
     */
126 18
    public function equals(Team $team)
127
    {
128 18
        return $this->getId() == $team->getId();
129
    }
130
131
    /**
132
     * @param int $id
133
     */
134 16
    public function setId($id)
135
    {
136 16
        $this->id = $id;
137 16
    }
138
139
    /**
140
     * @param int $points
141
     */
142 5
    public function setPoints($points)
143
    {
144 5
        $this->points = $points;
145 5
    }
146
147
    /**
148
     * @return int
149
     */
150 7
    public function getPoints()
151
    {
152 7
        return $this->points;
153
    }
154
155
    /**
156
     * @param int $points
157
     */
158 5
    public function addPoints($points)
159
    {
160 5
        $this->points += $points;
161 5
    }
162
163
    /**
164
     * @param int $goalsFor
165
     */
166 3
    public function setGoalsFor($goalsFor)
167
    {
168 3
        $this->goalsFor = $goalsFor;
169 3
    }
170
171
    /**
172
     * @param int $goalsFor
173
     */
174 5
    public function addGoalsFor($goalsFor)
175
    {
176 5
        $this->goalsFor += $goalsFor;
177 5
    }
178
179
    /**
180
     * @return int
181
     */
182 5
    public function getGoalsDifference()
183
    {
184 5
        return $this->goalsFor - $this->goalsAgainst;
185
    }
186
187
    /**
188
     * @param int $goalsAgainst
189
     */
190 2
    public function setGoalsAgainst($goalsAgainst)
191
    {
192 2
        $this->goalsAgainst = $goalsAgainst;
193 2
    }
194
195
    /**
196
     * @param int $goalsAgainst
197
     */
198 5
    public function addGoalsAgainst($goalsAgainst)
199
    {
200 5
        $this->goalsAgainst += $goalsAgainst;
201 5
    }
202
203
    /**
204
     * @return int
205
     */
206 7
    public function getGoalsFor()
207
    {
208 7
        return $this->goalsFor;
209
    }
210
211
    /**
212
     * @return int
213
     */
214 2
    public function getGoalsAgainst()
215
    {
216 2
        return $this->goalsAgainst;
217
    }
218
219
    /**
220
     * @return string
221
     */
222 1
    public function getName()
223
    {
224 1
        return $this->name;
225
    }
226
227
    /**
228
     * @param string $name
229
     */
230 1
    public function setName($name)
231
    {
232 1
        $this->name = $name;
233 1
    }
234
235
    /**
236
     * @param League $league
237
     */
238
    public function setLeague(League $league)
239
    {
240
        $this->league = $league;
241
    }
242
243
    /**
244
     * @return ArrayCollection|Player[]
245
     */
246 10
    public function getPlayers()
247
    {
248 10
        return $this->players;
249
    }
250
251
    /**
252
     * @param Player $player
253
     */
254 31
    public function addPlayer(Player $player)
255
    {
256 31
        if (!$this->players->contains($player)) {
257 31
            $this->players->add($player);
258 31
            $player->setTeam($this);
259 31
        }
260 31
    }
261
262
    /**
263
     * @return Player
264
     *
265
     * @throws \Exception
266
     */
267 1
    public function getRandomPlayer()
268
    {
269 1
        if (0 == count($this->players)) {
270
            throw new \Exception('this team has no players');
271
        }
272
273 1
        return $this->players[rand(0, count($this->players) - 1)];
274
    }
275
276
    /**
277
     * @return Player
278
     *
279
     * @throws \Exception
280
     */
281 5
    public function getRandomPlayerFromLineup()
282
    {
283 5
        $lineup = $this->getLineup();
284
285 5
        if (0 == count($lineup)) {
286
            throw new \Exception('no players in the lineup');
287
        }
288
289 5
        return $lineup[rand(0, count($lineup) - 1)];
290
    }
291
292
    /**
293
     * @return Player[]
294
     */
295 6
    public function getLineup()
296
    {
297 6
        $players = $this->players->toArray();
298 6
        usort($players, array('OSS\CoreBundle\Entity\PlayerSkills', 'compareAverage'));
299
300 6
        return array_slice($players, 0, 11);
301
    }
302
303 1
    public function resetPointsAndGoals()
304
    {
305 1
        $this->points = 0;
306 1
        $this->goalsFor = 0;
307 1
        $this->goalsAgainst = 0;
308 1
    }
309
310
    /**
311
     * @return League
312
     */
313
    public function getLeague()
314
    {
315
        return $this->league;
316
    }
317
318
    /**
319
     * @return FinalPosition[]
320
     */
321
    public function getFinalPositions()
322
    {
323
        return $this->finalPositions;
324
    }
325
326
    /**
327
     * @return int
328
     */
329 16
    public function getMoney()
330
    {
331 16
        return $this->money;
332
    }
333
334
    /**
335
     * @param int $money
336
     */
337 17
    public function setMoney($money)
338
    {
339 17
        $this->money = $money;
340 17
    }
341
342
    /**
343
     * @param int $amount
344
     */
345 1
    public function addMoney($amount)
346
    {
347 1
        $this->money += $amount;
348 1
    }
349
350
    /**
351
     * @param int $amount
352
     */
353 1
    public function subtractAmount($amount)
354
    {
355 1
        $this->money -= $amount;
356 1
    }
357
358
    /**
359
     * @return Trainer
360
     */
361 5
    public function getTrainer()
362
    {
363 5
        return $this->trainer;
364
    }
365
366
    /**
367
     * @param Trainer $trainer
368
     */
369 4
    public function setTrainer(Trainer $trainer)
370
    {
371 4
        $this->trainer = $trainer;
372 4
        if (null === $trainer->getTeam()) {
373 3
            $trainer->setTeam($this);
374 3
        }
375 4
    }
376
377
    /**
378
     * @return bool
379
     */
380 3
    public function hasTrainer()
381
    {
382 3
        return null !== $this->trainer;
383
    }
384
385
    /**
386
     * @return Manager
387
     */
388 22
    public function getManager()
389
    {
390 22
        return $this->manager;
391
    }
392
393
    /**
394
     * @param Manager $manager
395
     */
396 21
    public function setManager(Manager $manager)
397
    {
398 21
        $this->manager = $manager;
399 21
        if (null === $manager->getTeam()) {
400 2
            $manager->setTeam($this);
401 2
        }
402 21
    }
403
404
    /**
405
     * @return bool
406
     */
407 1
    public function hasManager()
408
    {
409 1
        return null !== $this->manager;
410
    }
411
412
    /**
413
     * @param int $season
414
     *
415
     * @throws \Exception
416
     */
417
    public function createFinalPosition($season)
418
    {
419
        $finalPosition = new FinalPosition();
420
        $finalPosition->setTeam($this);
421
        $finalPosition->setSeason($season);
422
        $finalPosition->setLeague($this->league);
423
        $finalPosition->setPosition($this->league->getPositionByTeam($this));
424
        $this->finalPositions->add($finalPosition);
425
    }
426
427
    /**
428
     * @return Transfer[]
429
     */
430
    public function getTransfersIncoming()
431
    {
432
        return $this->transfersIncoming;
433
    }
434
435
    /**
436
     * @return Transfer[]
437
     */
438
    public function getTransfersOutgoing()
439
    {
440
        return $this->transfersOutgoing;
441
    }
442
443 2
    public function train()
444
    {
445 2
        if (!$this->hasTrainer()) {
446 1
            return;
447
        }
448
449 1
        foreach ($this->players as $player)
450
        {
451 1
            $this->trainer->train($player->getSkills());
452 1
        }
453 1
    }
454
455
    /**
456
     * @param Team $team
457
     * @param int $amount
458
     */
459 1
    public function sendMoney(Team $team, $amount)
460
    {
461 1
        $this->subtractAmount($amount);
462 1
        $team->addMoney($amount);
463 1
    }
464
}
465