|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\Form\FormError; |
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
5
|
|
|
|
|
6
|
|
|
class MatchController extends CRUDController |
|
7
|
|
|
{ |
|
8
|
1 |
|
public function listAction(Request $request, Team $team = null, $type = null) |
|
9
|
|
|
{ |
|
10
|
1 |
|
$qb = $this->getQueryBuilder(); |
|
11
|
|
|
|
|
12
|
1 |
|
$currentPage = $request->query->get('page', 1); |
|
13
|
|
|
|
|
14
|
1 |
|
$query = $qb->sortBy('time')->reverse() |
|
|
|
|
|
|
15
|
1 |
|
->with($team, $type) |
|
16
|
1 |
|
->limit(50)->fromPage($currentPage); |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
return array( |
|
20
|
1 |
|
"matches" => $query->getModels(), |
|
21
|
1 |
|
"team" => $team, |
|
22
|
1 |
|
"currentPage" => $currentPage, |
|
23
|
1 |
|
"totalPages" => $qb->countPages() |
|
24
|
|
|
); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function createAction(Player $me) |
|
28
|
|
|
{ |
|
29
|
1 |
|
return $this->create($me); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function deleteAction(Player $me, Match $match) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$match->getTeamA()->isLastMatch($match) |
|
|
|
|
|
|
35
|
|
|
|| !$match->getTeamB()->isLastMatch($match)) { |
|
|
|
|
|
|
36
|
|
|
throw new BadRequestException("You can only delete the last match of a team"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $this->delete($match, $me); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
1 |
|
protected function getMessages($type, $name = '') |
|
46
|
|
|
{ |
|
47
|
1 |
|
$messages = parent::getMessages($type, $name); |
|
48
|
|
|
|
|
49
|
|
|
// Don't show the match info on the successful create/edit message |
|
50
|
1 |
|
foreach ($messages as &$action) { |
|
51
|
1 |
|
foreach ($action as &$status) { |
|
52
|
1 |
|
if (isset($status['named'])) { |
|
53
|
1 |
|
$status['named'] = $status['unnamed']; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
return $messages; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
protected function validate($form) |
|
62
|
|
|
{ |
|
63
|
|
|
// Make sure that two different teams participated in a match, i.e. a team |
|
64
|
|
|
// didn't match against itself |
|
65
|
1 |
|
$firstTeam = $form->get('first_team')->get('team')->getData(); |
|
66
|
1 |
|
$secondTeam = $form->get('second_team')->get('team')->getData(); |
|
67
|
|
|
|
|
68
|
1 |
|
if (!$firstTeam || !$secondTeam) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
if ($firstTeam->isSameAs($secondTeam)) { |
|
73
|
1 |
|
$message = "You can't report a match where a team played against itself!"; |
|
74
|
1 |
|
$form->addError(new FormError($message)); |
|
75
|
|
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: