Completed
Push — master ( 3467f7...89f754 )
by Michael
04:55
created

UserController::getUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\AppBundle\Entity\User;
10
use Pagerfanta\Adapter\DoctrineORMAdapter;
11
use Pagerfanta\Pagerfanta;
12
13
class UserController extends Controller
14
{
15
    /**
16
     * @Template()
17
     * @Route("/users", name="user")
18
     */
19
	public function listAction(): Response
20
	{
21
		$users = $this->getUsers($request);
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
22
23
		return $this->render('default/index.html.twig', ['users' => $users]);
24
	}
25
26
	/**
27
	 * @Route("/users/create", name="user_create")
28
     * @Security("has_role('ROLE_ADMIN')")
29
	 */
30
	public function createAction(): Response
31
	{
32
		$response = $this->forward('FOSUserBundle:Registration:register');
33
34
		return $response;
35
	}
36
37
    /**
38
     * @Template()
39
     * @Route("/users/{name}", name="user_profile")
40
     * @ParamConverter("user", options={"mapping": {"name": "username"}})
41
     */
42
    public function profileAction(Request $request, User $user): Response
43
    {
44
        $votes = $this->getUserVotes($request, $user);
45
46
        return array(
47
            'votes' => $votes,
48
            'user' => $user,
49
        );
50
    }
51
52
    /**
53
     * @param Request $request
54
     *
55
     * @return Pagerfanta
56
     */
57
    protected function getUsers(Request $request): Pagerfanta
58
    {
59
        $users = $this->getDoctrine()
60
            ->getRepository('AppBundle:User');
61
        $paginator = new Pagerfanta(new DoctrineORMAdapter($users, true));
62
        $paginator->setMaxPerPage(20);
63
        $paginator->setCurrentPage($request->query->get('page', 1), false, true);
64
65
        return $paginator;
66
    }
67
68
    /**
69
     * @param Request $request
70
     * @param User $user
71
     *
72
     * @return Pagerfanta
73
     */
74
    protected function getUserVotes(Request $request, User $user): Pagerfanta
75
    {
76
        $votes = $this->getDoctrine()
77
            ->getRepository('AppBundle:VoteInstances') // TODO: We won't do this?
78
            ->getFilteredQueryBuilder(array('voter' => $user->getId()), true);
79
        $paginator = new Pagerfanta(new DoctrineORMAdapter($votes, true));
80
        $paginator->setMaxPerPage(20);
81
        $paginator->setCurrentPage($request->query->get('page', 1), false, true);
82
83
        return $paginator;
84
    }
85
}
86