1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Badger\Bundle\GameBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Badger\Bundle\GameBundle\Entity\Vote; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Olivier Soulet <[email protected]> |
13
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
*/ |
15
|
|
|
class VoteController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @Route("/votable-badges", name="votable_badges") |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function indexAction() |
23
|
|
|
{ |
24
|
|
|
$votableItems = $this->get('badger.game.repository.votable_item') |
25
|
|
|
->findBy(['pending' => 1]); |
26
|
|
|
|
27
|
|
|
$votesResults = []; |
28
|
|
|
foreach ($votableItems as $votableItem) { |
29
|
|
|
$voteResult = 0; |
30
|
|
|
$votes = $votableItem->getVotes(); |
31
|
|
|
foreach ($votes as $vote) { |
32
|
|
|
$voteResult += (int) $vote->getVote(); |
33
|
|
|
} |
34
|
|
|
$votesResults[$votableItem->getId()] = $voteResult; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->render('@Game/pending-votes.html.twig', [ |
38
|
|
|
'votableItems' => $votableItems, |
39
|
|
|
'votesResults' => $votesResults |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @Route("/pending-badge/upvote/{id}", name="badge_upvote") |
45
|
|
|
* |
46
|
|
|
* @return Response |
47
|
|
|
*/ |
48
|
|
|
public function upvoteAction($id) |
49
|
|
|
{ |
50
|
|
|
$votableItem = $this->get('badger.game.repository.votable_item') |
51
|
|
|
->findOneBy(['id' => $id, 'pending' => 1]); |
52
|
|
|
|
53
|
|
|
//TODO: to extract |
54
|
|
|
$vote = $this->get('badger.game.repository.vote')->findOneBy( |
55
|
|
|
[ |
56
|
|
|
'votableItem' => $votableItem, |
57
|
|
|
'user' => $this->getUser() |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
|
61
|
|
View Code Duplication |
if (null === $vote) { |
|
|
|
|
62
|
|
|
$vote = new Vote(); |
63
|
|
|
$vote->setVotableItem($votableItem); |
64
|
|
|
$vote->setUser($this->getUser()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (1 === $vote->getVote()) { |
68
|
|
|
$vote->setVote(0); |
69
|
|
|
} else { |
70
|
|
|
$vote->setVote(1); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->get('badger.game.saver.vote')->save($vote); |
74
|
|
|
|
75
|
|
|
if (null === $votableItem) { |
76
|
|
|
throw new NotFoundHttpException(sprintf('No pending votable entity with id %s', $id)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->addFlash('notice', sprintf( |
80
|
|
|
'You successfully voted for "%s" to have the badge %s!', |
81
|
|
|
$votableItem->getBadgeCompletion()->getUser()->getUsername(), |
82
|
|
|
$votableItem->getBadgeCompletion()->getBadge()->getTitle() |
83
|
|
|
)); |
84
|
|
|
|
85
|
|
|
return $this->redirectToRoute('votable_badges'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @Route("/pending-badge/downvote/{id}", name="badge_downvote") |
90
|
|
|
* |
91
|
|
|
* @return Response |
92
|
|
|
*/ |
93
|
|
|
public function downvoteAction($id) |
94
|
|
|
{ |
95
|
|
|
$votableItem = $this->get('badger.game.repository.votable_item') |
96
|
|
|
->findOneBy(['id' => $id, 'pending' => 1]); |
97
|
|
|
|
98
|
|
|
//TODO: to extract |
99
|
|
|
$vote = $this->get('badger.game.repository.vote')->findOneBy( |
100
|
|
|
[ |
101
|
|
|
'votableItem' => $votableItem, |
102
|
|
|
'user' => $this->getUser() |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
|
106
|
|
View Code Duplication |
if (null === $vote) { |
|
|
|
|
107
|
|
|
$vote = new Vote(); |
108
|
|
|
$vote->setVotableItem($votableItem); |
109
|
|
|
$vote->setUser($this->getUser()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (-1 === $vote->getVote()) { |
113
|
|
|
$vote->setVote(0); |
114
|
|
|
} else { |
115
|
|
|
$vote->setVote(-1); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->get('badger.game.saver.vote')->save($vote); |
119
|
|
|
|
120
|
|
|
$this->addFlash('notice', 'You successfully voted ! "%s" to NOT have the badge %s!'); |
121
|
|
|
|
122
|
|
|
return $this->redirectToRoute('votable_badges'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.