UserController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 5
c 5
b 0
f 0
lcom 1
cbo 8
dl 0
loc 75
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A profileAction() 0 9 1
A getUsers() 0 11 1
A getUserVotes() 0 11 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Pagerfanta\Adapter\ArrayAdapter;
6
use Pagerfanta\Adapter\DoctrineORMAdapter;
7
use Pagerfanta\Pagerfanta;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\AppBundle\Entity\User;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class UserController extends Controller
16
{
17
    /**
18
     * @Route("/users", name="user")
19
     */
20 1
    public function listAction(Request $request): Response
21
    {
22 1
        $users = $this->getUsers($request);
23
24 1
        return $this->render('default/index.html.twig', ['users' => $users]);
25
    }
26
27
    /**
28
     * @Route("/users/create", name="user_create")
29
     */
30 1
    public function createAction(): Response
31
    {
32 1
        $response = $this->forward('FOSUserBundle:Registration:register');
33
34 1
        return $response;
35
    }
36
37
    /**
38
     * @Route("/users/{name}", name="user_profile")
39
     * @ParamConverter("user", options={"mapping": {"name": "username"}})
40
     */
41
    public function profileAction(Request $request, User $user): Response
42
    {
43
        $votes = $this->getUserVotes($request, $user);
44
45
        return $this->render(
46
            'default/index.html.twig',
47
            ['votes' => $votes, 'user' => $user]
48
        );
49
    }
50
51
    /**
52
     * Get pagination object of users.
53
     *
54
     * @param Request $request
55
     *
56
     * @return Pagerfanta
57
     */
58 1
    protected function getUsers(Request $request): Pagerfanta
59
    {
60 1
        $users = $this->getDoctrine()
61 1
            ->getRepository('AppBundle:User')
62 1
            ->findAll();
63 1
        $paginator = new Pagerfanta(new ArrayAdapter($users));
64 1
        $paginator->setMaxPerPage(20);
65 1
        $paginator->setCurrentPage($request->query->get('page', 1), false, true);
66
67 1
        return $paginator;
68
    }
69
70
    /**
71
     * Get pagination object of user votes.
72
     *
73
     * @param Request $request
74
     * @param User    $user
75
     *
76
     * @return Pagerfanta
77
     */
78
    protected function getUserVotes(Request $request, User $user): Pagerfanta
79
    {
80
        $votes = $this->getDoctrine()
81
            ->getRepository('AppBundle:Votes') // TODO: We won't do this?
82
            ->getFilteredQueryBuilder(array('voter' => $user->getId()), true);
83
        $paginator = new Pagerfanta(new DoctrineORMAdapter($votes, true));
84
        $paginator->setMaxPerPage(20);
85
        $paginator->setCurrentPage($request->query->get('page', 1), false, true);
86
87
        return $paginator;
88
    }
89
}
90