Fixture::getLineupHome()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
use OSS\CoreBundle\Exception\MatchException;
8
9
/**
10
 * @ORM\Entity(repositoryClass="OSS\CoreBundle\Repository\FixtureRepository")
11
 */
12
class Fixture
13
{
14
    /**
15
     * @var int
16
     *
17
     * @ORM\Id
18
     * @ORM\GeneratedValue
19
     * @ORM\Column(type="integer")
20
     */
21
    private $id;
22
23
    /**
24
     * @var int
25
     *
26
     * @ORM\Column(type="integer")
27
     */
28
    private $season;
29
30
    /**
31
     * @var int
32
     *
33
     * @ORM\Column(type="integer")
34
     */
35
    private $week;
36
37
    /**
38
     * @var League
39
     *
40
     * @ORM\ManyToOne(targetEntity="League", inversedBy="fixtures")
41
     */
42
    private $league;
43
44
    /**
45
     * @var Team
46
     *
47
     * @ORM\ManyToOne(targetEntity="Team")
48
     */
49
    private $teamHome;
50
51
    /**
52
     * @var Team
53
     *
54
     * @ORM\ManyToOne(targetEntity="Team")
55
     */
56
    private $teamAway;
57
58
    /**
59
     * @var ArrayCollection|Event[]
60
     *
61
     * @ORM\OneToMany(targetEntity="Event", mappedBy="fixture", cascade={"all"})
62
     */
63
    private $events;
64
65
    /**
66
     * @var bool
67
     *
68
     * @ORM\Column(type="boolean")
69
     */
70
    private $finished = false;
71
72
    /**
73
     * @var int
74
     *
75
     * @ORM\Column(type="integer")
76
     */
77
    private $minutesPlayed = 0;
78
79
    /**
80
     * @var int
81
     *
82
     * @ORM\Column(type="integer", nullable=true)
83
     */
84
    private $scoreHome;
85
86
    /**
87
     * @var int
88
     *
89
     * @ORM\Column(type="integer", nullable=true)
90
     */
91
    private $scoreAway;
92
93
    /**
94
     * @var Lineup[]
95
     *
96
     * @ORM\OneToMany(targetEntity="Lineup", mappedBy="fixture")
97
     */
98
    private $lineups;
99
100 16
    public function __construct()
101
    {
102 16
        $this->events = new ArrayCollection();
103 16
    }
104
105
    /**
106
     * @return int
107
     */
108 9
    public function getScoreHome()
109
    {
110 9
        return $this->scoreHome;
111
    }
112
113
    /**
114
     * @return int
115
     */
116 9
    public function getScoreAway()
117
    {
118 9
        return $this->scoreAway;
119
    }
120
121 8
    public function resetScoreHome()
122
    {
123 8
        $this->scoreHome = 0;
124 8
    }
125
126 8
    public function resetScoreAway()
127
    {
128 8
        $this->scoreAway = 0;
129 8
    }
130
131
    /**
132
     * @return int
133
     */
134 1
    public function getGoalsScored()
135
    {
136 1
        return $this->getScoreHome() + $this->getScoreAway();
137
    }
138
139
    /**
140
     * @return bool
141
     */
142 1
    public function isFinished()
143
    {
144 1
        return $this->finished;
145
    }
146
147
    /**
148
     * @param bool $isFinished
149
     */
150 5
    public function setFinished($isFinished)
151
    {
152 5
        $this->finished = $isFinished;
153 5
    }
154
155
    /**
156
     * @return bool
157
     */
158 11
    public function hasTeamHome()
159
    {
160 11
        return null !== $this->teamHome;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 10
    public function hasTeamAway()
167
    {
168 10
        return null !== $this->teamAway;
169
    }
170
171
    /**
172
     * @param Team $teamHome
173
     *
174
     * @throws MatchException
175
     */
176 10
    public function setTeamHome(Team $teamHome)
177
    {
178 10
        if ($this->hasTeamAway() && $this->getTeamAway()->equals($teamHome)) {
179
            throw new MatchException('home team must not be the same as away team');
180
        }
181
182 10
        $this->teamHome = $teamHome;
183 10
    }
184
185
    /**
186
     * @param Team $teamAway
187
     *
188
     * @throws MatchException
189
     */
190 10
    public function setTeamAway(Team $teamAway)
191
    {
192 10
        if ($this->hasTeamHome() && $this->getTeamHome()->equals($teamAway)) {
193 1
            throw new MatchException('away team must not be the same as home team');
194
        }
195
196 9
        $this->teamAway = $teamAway;
197 9
    }
198
199
    /**
200
     * @return Event[]
201
     */
202 2
    public function getEvents()
203
    {
204 2
        return $this->events;
205
    }
206
207
    /**
208
     * @return Event[]
209
     */
210 2
    public function getGoalEvents()
211
    {
212 2
        $goalEvents = array();
213 2
        foreach ($this->events as $event) {
214 2
            if ($event->isGoal()) {
215 2
                $goalEvents[] = $event;
216 2
            }
217 2
        }
218
219 2
        return $goalEvents;
220
    }
221
222
    /**
223
     * @param Event $event
224
     *
225
     * @throws MatchException
226
     */
227 8
    public function addEvent(Event $event)
228
    {
229 8
        $this->events[] = $event;
230 8
        if ($event->isGoal()) {
231 3
            if (null !== $this->teamHome && $event->getTeam()->equals($this->teamHome)) {
232 3
                $this->scoreHome++;
233 3
            } elseif (null !== $this->teamAway && $event->getTeam()->equals($this->teamAway)) {
234 1
                $this->scoreAway++;
235 1
            } else {
236
                throw new MatchException('team with id ' . $event->getTeam()->getId() . ' is not part of this match');
237
            }
238 3
        }
239 8
    }
240
241
    /**
242
     * @return int
243
     */
244 5
    public function getMinutesPlayed()
245
    {
246 5
        return $this->minutesPlayed;
247
    }
248
249 6
    public function incrementMinutesPlayed()
250
    {
251 6
        $this->minutesPlayed++;
252 6
    }
253
254
    /**
255
     * @return Team
256
     */
257 10
    public function getTeamHome()
258
    {
259 10
        return $this->teamHome;
260
    }
261
262
    /**
263
     * @return Team
264
     */
265 6
    public function getTeamAway()
266
    {
267 6
        return $this->teamAway;
268
    }
269
270
    /**
271
     * @param int $season
272
     */
273
    public function setSeason($season)
274
    {
275
        $this->season = $season;
276
    }
277
278
    /**
279
     * @param int $week
280
     */
281
    public function setWeek($week)
282
    {
283
        $this->week = $week;
284
    }
285
286
    /**
287
     * @return int
288
     */
289
    public function getWeek()
290
    {
291
        return $this->week;
292
    }
293
294
    /**
295
     * @return Team
296
     *
297
     * @throws MatchException
298
     */
299 1
    public function getWinner()
300
    {
301 1
        if ($this->getScoreHome() == $this->getScoreAway()) {
302 1
            throw new MatchException('this match has no winner');
303
        }
304
305
        return $this->scoreHome > $this->scoreAway ? $this->teamHome : $this->teamAway;
306
    }
307
308
    /**
309
     * @return bool
310
     */
311 6
    public function isDraw()
312
    {
313 6
        return $this->scoreHome == $this->scoreAway;
314
    }
315
316
    /**
317
     * @return bool
318
     *
319
     * @throws MatchException
320
     */
321
    public function isHomeTeamWinner()
322
    {
323
        return $this->getWinner()->equals($this->teamHome);
324
    }
325
326
    /**
327
     * @return int
328
     */
329
    public function getId()
330
    {
331
        return $this->id;
332
    }
333
334
    /**
335
     * @param League $league
336
     */
337
    public function setLeague(League $league)
338
    {
339
        $this->league = $league;
340
    }
341
342
    /**
343
     * @return Lineup
344
     *
345
     * @throws \Exception
346
     */
347
    public function getLineupHome()
348
    {
349
        return $this->getLineup($this->teamHome);
350
    }
351
352
    /**
353
     * @return Lineup
354
     *
355
     * @throws \Exception
356
     */
357
    public function getLineupAway()
358
    {
359
        return $this->getLineup($this->teamAway);
360
    }
361
362
    /**
363
     * @param Team $team
364
     *
365
     * @return Lineup
366
     *
367
     * @throws \Exception
368
     */
369
    private function getLineup(Team $team)
370
    {
371
        foreach ($this->lineups as $lineup) {
372
            if ($lineup->getTeam()->equals($team)) {
373
                return $lineup;
374
            }
375
        }
376
377
        throw new \Exception('could not find lineup for team ' . $team->getId());
378
    }
379
}
380