Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

getCurrentUserSuggestions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\CoreBundle\Controller;
4
5
use LoginCidadao\CoreBundle\Form\Type\SuggestionFormType;
6
use LoginCidadao\OAuthBundle\Entity\Client;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use LoginCidadao\CoreBundle\Entity\ClientSuggestion;
13
14
class AuthorizationController extends Controller
15
{
16
17
    /**
18
     * @Route("/authorizations", name="lc_authorization_list")
19
     * @Template()
20
     */
21
    public function listAction(Request $request)
22
    {
23
        /** @var Client[] $allApps */
24
        $allApps = $this->getDoctrine()->getRepository('LoginCidadaoOAuthBundle:Client')->findAll();
25
        $user = $this->getUser();
26
27
        $apps = [];
28
        // Filtering off authorized apps
29
        foreach ($allApps as $app) {
30
            if ($user->hasAuthorization($app) || !$app->isVisible()) {
31
                continue;
32
            }
33
            $apps[] = $app;
34
        }
35
36
        $suggestion = $this->handleSuggestion($request);
37
        if ($suggestion instanceof RedirectResponse) {
38
            return $suggestion;
39
        } else {
40
            $form = $suggestion->createView();
41
        }
42
43
        $suggestions = $this->getCurrentUserSuggestions();
44
        $defaultClientUid = $this->container->getParameter('oauth_default_client.uid');
45
46
        return compact('user', 'apps', 'form', 'suggestions', 'defaultClientUid');
47
    }
48
49
    /**
50
     * @param Request $request
51
     * @return \Symfony\Component\Form\Form|\Symfony\Component\HttpFoundation\RedirectResponse
52
     */
53
    private function handleSuggestion(Request $request)
54
    {
55
        $suggestion = new ClientSuggestion();
56
        $form = $this->createForm(new SuggestionFormType(), $suggestion);
57
58
        $em = $this->getDoctrine()->getManager();
59
        $form->handleRequest($request);
60
        if ($form->isValid()) {
61
            $suggestion->setPerson($this->getUser());
0 ignored issues
show
Documentation introduced by
$this->getUser() is of type null|object, but the function expects a object<LoginCidadao\CoreBundle\Entity\Person>.

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...
62
63
            $em->persist($suggestion);
64
            $em->flush();
65
66
            $translator = $this->get('translator');
67
            $this->addFlash(
68
                'success',
69
                $translator->trans('client.suggestion.registered')
70
            );
71
72
            return $this->redirect($this->generateUrl('lc_authorization_list'));
73
        }
74
75
        return $form;
76
    }
77
78
    private function getCurrentUserSuggestions()
79
    {
80
        $suggestions = $this->getDoctrine()->getRepository('LoginCidadaoCoreBundle:ClientSuggestion')
81
            ->findBy(['person' => $this->getUser()], ['createdAt' => 'desc'], 6);
82
83
        return $suggestions;
84
    }
85
}
86