|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\Poll; |
|
6
|
|
|
use AppBundle\Exception\InvalidSort; |
|
7
|
|
|
use AppBundle\Utils\ElectionManager; |
|
8
|
|
|
use AppBundle\Utils\PollManager; |
|
9
|
|
|
use AppBundle\Utils\VotableManager; |
|
10
|
|
|
use Pagerfanta\Adapter\ArrayAdapter; |
|
11
|
|
|
use Pagerfanta\Pagerfanta; |
|
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
17
|
|
|
|
|
18
|
1 |
|
class VoteController extends Controller |
|
19
|
|
|
{ |
|
20
|
1 |
|
private $electionManager; |
|
21
|
|
|
private $pollManager; |
|
22
|
1 |
|
|
|
23
|
1 |
|
/** |
|
24
|
|
|
* VoteController constructor. |
|
25
|
1 |
|
* |
|
26
|
|
|
* @param \AppBundle\Utils\PollManager $pollManager |
|
27
|
|
|
* @param \AppBundle\Utils\ElectionManager $electionManager |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(PollManager $pollManager, ElectionManager $electionManager) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->pollManager = $pollManager; |
|
32
|
|
|
$this->electionManager = $electionManager; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @Route("/polls", name="polls_list") |
|
37
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
38
|
|
|
* |
|
39
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
40
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function listAction(Request $request): Response |
|
43
|
1 |
|
{ |
|
44
|
|
|
try { |
|
45
|
1 |
|
$sort = $request->request->has('sort') ? $sort = $request->get('sort') : null; |
|
46
|
1 |
|
$polls = $this->pollManager->getPolls($sort); |
|
47
|
1 |
|
$current = $this->pollManager->getPolls($sort, true); |
|
|
|
|
|
|
48
|
1 |
|
} catch ( InvalidSort $e ) { |
|
|
|
|
|
|
49
|
|
|
throw new NotFoundHttpException('This is an invalid sorting method'); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
$pollsPaginated = $this->paginate($request, $polls); |
|
53
|
|
|
|
|
54
|
|
|
return $this->render('default/index.html.twig', ['polls' => $pollsPaginated, 'current' => $current]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get pagination object of Polls. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Request $request |
|
61
|
|
|
* |
|
62
|
|
|
* @return Pagerfanta |
|
63
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function paginate(Request $request, array $items): Pagerfanta |
|
66
|
|
|
{ |
|
67
|
|
|
$paginator = new Pagerfanta(new ArrayAdapter($items)); |
|
68
|
|
|
|
|
69
|
|
|
$paginator->setMaxPerPage(20); |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
$paginator->setCurrentPage($request->query->get('page', 1), false, true); |
|
73
|
|
|
} catch ( \PagerFanta\Exception\Exception $e ) { |
|
|
|
|
|
|
74
|
|
|
throw new NotFoundHttpException('Page not found'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $paginator; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @Route("/poll/{id}", name="poll_view") |
|
82
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
83
|
|
|
* @param \AppBundle\Entity\Poll $poll |
|
84
|
|
|
* |
|
85
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
86
|
|
|
*/ |
|
87
|
|
|
public function viewAction(Request $request, Poll $poll): Response |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: