1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2014-2016 Arnaud Bienvenu |
4
|
|
|
* |
5
|
|
|
* This file is part of Kyela. |
6
|
|
|
|
7
|
|
|
* Kyela is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
|
12
|
|
|
* Kyela is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with Kyela. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Abienvenu\KyelaBundle\Controller; |
23
|
|
|
|
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
26
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
27
|
|
|
use Abienvenu\KyelaBundle\Entity\Comment; |
28
|
|
|
use Abienvenu\KyelaBundle\Form\Type\CommentType; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Comment controller. |
32
|
|
|
* |
33
|
|
|
* @Route("/{pollUrl}/comment") |
34
|
|
|
*/ |
35
|
|
|
class CommentController extends CRUDController |
36
|
|
|
{ |
37
|
|
|
protected $entityName = 'KyelaBundle:Comment'; |
38
|
|
|
protected $cancelRoute = 'poll_show'; |
39
|
|
|
protected $successRoute = 'poll_show'; |
40
|
|
|
protected $deleteRoute = 'comment_delete'; |
41
|
|
|
protected $deleteSuccessRoute = 'poll_show'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Displays latest comments |
45
|
|
|
* |
46
|
|
|
* @Route("/", methods="GET") |
47
|
|
|
* @Template() |
48
|
|
|
*/ |
49
|
|
|
public function showAction() |
50
|
|
|
{ |
51
|
|
|
$em = $this->getDoctrine()->getManager(); |
52
|
|
|
$comments = $em->getRepository('KyelaBundle:Comment')->getLatestComments($this->poll); |
|
|
|
|
53
|
|
|
return ['poll' => $this->poll, 'comments' => $comments]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Displays a form to create a new Comment entity. |
58
|
|
|
* |
59
|
|
|
* @Route("/new", name="comment_new", methods={"GET", "POST"}) |
60
|
|
|
* @Template() |
61
|
|
|
*/ |
62
|
|
|
public function newAction(Request $request) |
63
|
|
|
{ |
64
|
|
|
$comment = new Comment(); |
65
|
|
|
if ($request->isMethod('POST')) |
66
|
|
|
{ |
67
|
|
|
$comment->setDatetime(new \DateTime()); |
68
|
|
|
} |
69
|
|
|
return $this->doNewAction(CommentType::class, $comment, $request, null, ['authors' => $this->poll->getParticipantsAsArrayOfNames()]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Displays a form to edit an existing Comment entity. |
74
|
|
|
* |
75
|
|
|
* @Route("/{id}/edit", name="comment_edit", methods={"GET", "PUT"}) |
76
|
|
|
* @Template() |
77
|
|
|
*/ |
78
|
|
|
public function editAction(Request $request, $id) |
79
|
|
|
{ |
80
|
|
|
return $this->doEditAction(CommentType::class, $id, $request, ['authors' => $this->poll->getParticipantsAsArrayOfNames()]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Deletes a Comment entity. |
85
|
|
|
* |
86
|
|
|
* @Route("/{id}", name="comment_delete", methods="DELETE") |
87
|
|
|
*/ |
88
|
|
|
public function deleteAction(Request $request, $id) |
89
|
|
|
{ |
90
|
|
|
return $this->doDeleteAction($request, $id); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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 implementation 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 interface: