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 Pagerfanta\Adapter\ArrayAdapter; |
10
|
|
|
use PagerFanta\Exception\Exception; |
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
|
|
|
* @param array $items |
63
|
|
|
* |
64
|
|
|
* @return \Pagerfanta\Pagerfanta |
65
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
66
|
|
|
*/ |
67
|
|
|
protected function paginate(Request $request, array $items): Pagerfanta |
68
|
|
|
{ |
69
|
|
|
$paginator = new Pagerfanta(new ArrayAdapter($items)); |
70
|
|
|
|
71
|
|
|
$paginator->setMaxPerPage(20); |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$paginator->setCurrentPage($request->query->get('page', 1), false, true); |
75
|
|
|
} catch (Exception $e) { |
76
|
|
|
throw new NotFoundHttpException('Page not found'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $paginator; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @Route("/poll/{id}", name="poll_view") |
84
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
85
|
|
|
* @param \AppBundle\Entity\Poll $poll |
86
|
|
|
* |
87
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
88
|
|
|
*/ |
89
|
|
|
public function viewAction(Request $request, Poll $poll): Response |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: