Failed Conditions
Push — issue#763 ( 4f5bf6 )
by Guilherme
08:27
created

ClientController::editAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 28
nc 3
nop 2
dl 0
loc 35
ccs 0
cts 28
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\CoreBundle\Controller\Dev;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\Routing\Annotation\Route;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
use Symfony\Component\HttpFoundation\Request;
9
use LoginCidadao\OAuthBundle\Entity\Client;
10
use LoginCidadao\CoreBundle\Helper\GridHelper;
11
12
/**
13
 * @Route("/dev/client")
14
 */
15
class ClientController extends Controller
16
{
17
18
    /**
19
     * @Route("/new", name="lc_dev_client_new")
20
     * @Template()
21
     */
22
    public function newAction(Request $request)
23
    {
24
        $client = new Client();
25
        $form   = $this->createForm('LoginCidadao\CoreBundle\Form\Type\ClientFormType',
26
            $client);
27
28
        $form->handleRequest($request);
29
        $messages = '';
30
        if ($form->isValid()) {
31
            $clientManager = $this->container->get('fos_oauth_server.client_manager');
0 ignored issues
show
Unused Code introduced by
The assignment to $clientManager is dead and can be removed.
Loading history...
32
            $client->getOwners()->add($this->getUser());
33
            $client->setAllowedGrantTypes(Client::getAllGrants());
34
            $em            = $this->getDoctrine()->getManager();
35
            $em->persist($client);
36
            $em->flush();
37
            return $this->redirect($this->generateUrl('lc_dev_client_edit',
38
                        array(
39
                        'id' => $client->getId()
40
            )));
41
        }
42
        return array(
43
            'form' => $form->createView(),
44
            'messages' => $messages
45
        );
46
    }
47
48
    /**
49
     * @Route("/", name="lc_dev_client")
50
     * @Template()
51
     */
52
    public function indexAction(Request $request)
53
    {
54
        return $this->gridAction($request);
55
    }
56
57
    /**
58
     * @Route("/grid", name="lc_dev_client_grid")
59
     * @Template()
60
     */
61
    public function gridAction(Request $request)
62
    {
63
        $em   = $this->getDoctrine()->getManager();
64
        $sql  = $em->getRepository('LoginCidadaoOAuthBundle:Client')->createQueryBuilder('c')
65
            ->where(':person MEMBER OF c.owners')
66
            ->setParameter('person', $this->getUser())
67
            ->addOrderBy('c.id', 'desc');
68
        $grid = new GridHelper();
69
        $grid->setId('client-grid');
70
        $grid->setPerPage(5);
71
        $grid->setMaxResult(5);
72
        $grid->setQueryBuilder($sql);
0 ignored issues
show
Deprecated Code introduced by
The function LoginCidadao\CoreBundle\...lper::setQueryBuilder() has been deprecated: since version 1.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

72
        /** @scrutinizer ignore-deprecated */ $grid->setQueryBuilder($sql);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
73
        $grid->setInfiniteGrid(true);
74
        $grid->setRoute('lc_dev_client_grid');
75
        return array('grid' => $grid->createView($request));
76
    }
77
78
    /**
79
     * @Route("/grid/developer/filter", name="lc_dev_client_grid_developer_filter")
80
     * @Template()
81
     */
82
    public function gridDeveloperFilterAction(Request $request)
83
    {
84
        $grid = new GridHelper();
85
        $grid->setId('developer-filter-grid');
86
        $grid->setPerPage(5);
87
        $grid->setMaxResult(5);
88
        $parms = $request->get('ac_data');
89
        if (isset($parms['username'])) {
90
            $em  = $this->getDoctrine()->getManager();
91
            $sql = $em->getRepository('LoginCidadaoCoreBundle:Person')->createQueryBuilder('u');
92
            $sql->select('u');
93
            $sql->where('1=1');
94
            $sql->andWhere('u.cpf like ?1 or u.username like ?1 or u.email like ?1 or u.firstName like ?1 or u.surname like ?1');
95
            $sql->setParameter('1',
96
                '%'.addcslashes($parms['username'], '\\%_').'%');
97
            $sql->addOrderBy('u.id', 'desc');
98
            $grid->setQueryBuilder($sql);
0 ignored issues
show
Deprecated Code introduced by
The function LoginCidadao\CoreBundle\...lper::setQueryBuilder() has been deprecated: since version 1.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

98
            /** @scrutinizer ignore-deprecated */ $grid->setQueryBuilder($sql);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
99
        }
100
        $grid->setInfiniteGrid(true);
101
        $grid->setRouteParams(array('ac_data'));
102
        $grid->setRoute('lc_dev_client_grid_developer_filter');
103
        return array('grid' => $grid->createView($request));
104
    }
105
106
    /**
107
     * @Route("/grid/developer", name="lc_dev_client_grid_developer")
108
     * @Template()
109
     */
110
    public function gridDeveloperAction(Request $request)
111
    {
112
        $grid = new GridHelper();
113
        $grid->setId('developer-grid');
114
        $grid->setPerPage(5);
115
        $grid->setMaxResult(5);
116
        $parms = $request->get('ac_data');
117
        if (isset($parms['person_id']) && !empty($parms['person_id'])) {
118
            $em  = $this->getDoctrine()->getManager();
119
            $sql = $em->getRepository('LoginCidadaoCoreBundle:Person')->createQueryBuilder('p');
120
            $sql->where('p.id in(:id)')->setParameter('id', $parms['person_id']);
121
            $sql->addOrderBy('p.id', 'desc');
122
            $grid->setQueryBuilder($sql);
0 ignored issues
show
Deprecated Code introduced by
The function LoginCidadao\CoreBundle\...lper::setQueryBuilder() has been deprecated: since version 1.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
            /** @scrutinizer ignore-deprecated */ $grid->setQueryBuilder($sql);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
123
        }
124
        $grid->setInfiniteGrid(true);
125
        $grid->setRouteParams(array('ac_data'));
126
        $grid->setRoute('lc_dev_client_grid_developer');
127
        return array('grid' => $grid->createView($request));
128
    }
129
130
    /**
131
     * @Route("/{id}/edit", name="lc_dev_client_edit")
132
     * @Template()
133
     */
134
    public function editAction(Request $request, $id)
135
    {
136
        $em     = $this->getDoctrine()->getManager();
137
        $client = $em->getRepository('LoginCidadaoOAuthBundle:Client')->createQueryBuilder('c')
138
                ->where(':person MEMBER OF c.owners')
139
                ->andWhere('c.id = :id')
140
                ->setParameters(array('id' => $id, 'person' => $this->getUser()))
141
                ->getQuery()->getOneOrNullResult();
142
        if (!$client) {
143
            return $this->redirect($this->generateUrl('lc_dev_client_new'));
144
        }
145
146
        $form     = $this->createForm('LoginCidadao\CoreBundle\Form\Type\ClientFormType',
147
            $client);
148
        $form->handleRequest($request);
149
        $messages = '';
150
        if ($form->isValid()) {
151
            $metadata = $form->get('metadata')->getData();
152
            $client->setAllowedGrantTypes(Client::getAllGrants());
153
            $client->setMetadata($metadata);
154
            $metadata->setClient($client);
155
156
            $clientManager = $this->container->get('fos_oauth_server.client_manager');
157
            $clientManager->updateClient($client);
158
            $translator    = $this->get('translator');
159
            $this->get('session')->getFlashBag()->add('success',
160
                $translator->trans('Updated successfully!'));
161
162
            return $this->redirectToRoute('lc_dev_client_edit', compact('id'));
163
        }
164
        return $this->render('LoginCidadaoCoreBundle:Dev\Client:new.html.twig',
165
                array(
166
                'form' => $form->createView(),
167
                'client' => $client,
168
                'messages' => $messages
169
        ));
170
    }
171
}
172