Completed
Push — master ( 526007...a3325f )
by Michael
03:04
created

VoteController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a false|object<AppBundle\Utils\boolean>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48 1
		} catch ( InvalidSort $e ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $poll is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
	{
89
	}
90
}
91