Failed Conditions
Push — issue#768 ( 76d4a9 )
by Guilherme
11:58
created

ClientController::getClientRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\CoreBundle\Controller\Dev;
4
5
use Knp\Component\Pager\Paginator;
6
use LoginCidadao\CoreBundle\Helper\GridHelper;
7
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\Routing\Annotation\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\HttpFoundation\Request;
12
use LoginCidadao\OAuthBundle\Entity\Client;
13
14
/**
15
 * @Route("/dev/client")
16
 */
17
class ClientController extends Controller
18
{
19
    /**
20
     * @Route("/new", name="lc_dev_client_new")
21
     * @Template()
22
     */
23
    public function newAction(Request $request)
24
    {
25
        $client = new Client();
26
        $form = $this->createForm('LoginCidadao\CoreBundle\Form\Type\ClientFormType', $client);
27
28
        $form->handleRequest($request);
29
        $messages = '';
30
        if ($form->isValid()) {
31
            $client->getOwners()->add($this->getUser());
32
            $client->setAllowedGrantTypes(Client::getAllGrants());
33
            $em = $this->getDoctrine()->getManager();
34
            $em->persist($client);
35
            $em->flush();
36
37
            return $this->redirectToRoute('lc_dev_client_edit', ['id' => $client->getId()]);
38
        }
39
40
        return [
41
            'form' => $form->createView(),
42
            'messages' => $messages,
43
        ];
44
    }
45
46
    /**
47
     * @Route("/", name="lc_dev_client")
48
     * @Template()
49
     */
50
    public function indexAction(Request $request)
51
    {
52
        $query = $this->getClientRepository()->getOwnedByPersonQuery($this->getUser());
53
54
        /** @var Paginator $paginator */
55
        $paginator = $this->get('knp_paginator');
56
        $pagination = $paginator->paginate($query, $request->query->getInt('page', 1), 10);
57
58
        return ['pagination' => $pagination];
59
    }
60
61
    /**
62
     * @Route("/{id}/edit", name="lc_dev_client_edit")
63
     * @Template()
64
     * @throws \Doctrine\ORM\NonUniqueResultException
65
     */
66
    public function editAction(Request $request, $id)
67
    {
68
        $client = $this->getClientRepository()->findOneOwned($this->getUser(), $id);
69
        if (!$client) {
70
            return $this->redirect($this->generateUrl('lc_dev_client_new'));
71
        }
72
73
        $form = $this->createForm('LoginCidadao\CoreBundle\Form\Type\ClientFormType', $client);
74
        $form->handleRequest($request);
75
        $messages = '';
76
        if ($form->isValid()) {
77
            $metadata = $form->get('metadata')->getData();
78
            $client->setAllowedGrantTypes(Client::getAllGrants());
79
            $client->setMetadata($metadata);
0 ignored issues
show
Bug introduced by
The method setMetadata() does not exist on LoginCidadao\OAuthBundle\Model\ClientInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to LoginCidadao\OAuthBundle\Model\ClientInterface. ( Ignorable by Annotation )

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

79
            $client->/** @scrutinizer ignore-call */ 
80
                     setMetadata($metadata);
Loading history...
80
            $metadata->setClient($client);
81
82
            $clientManager = $this->container->get('fos_oauth_server.client_manager');
83
            $clientManager->updateClient($client);
84
            $translator = $this->get('translator');
85
            $this->get('session')->getFlashBag()->add('success', $translator->trans('Updated successfully!'));
86
87
            return $this->redirectToRoute('lc_dev_client_edit', compact('id'));
88
        }
89
90
        return $this->render('LoginCidadaoCoreBundle:Dev\Client:new.html.twig', [
91
            'form' => $form->createView(),
92
            'client' => $client,
93
            'messages' => $messages,
94
        ]);
95
    }
96
97
    /**
98
     * @return ClientRepository
99
     */
100
    private function getClientRepository()
101
    {
102
        /** @var ClientRepository $repo */
103
        $repo = $this->get('lc.client.repository');
104
105
        return $repo;
106
    }
107
    /**
108
     * @Route("/grid", name="lc_dev_client_grid")
109
     * @Template()
110
     */
111
    public function gridAction(Request $request)
112
    {
113
        $em   = $this->getDoctrine()->getManager();
114
        $sql  = $em->getRepository('LoginCidadaoOAuthBundle:Client')->createQueryBuilder('c')
115
            ->where(':person MEMBER OF c.owners')
116
            ->setParameter('person', $this->getUser())
117
            ->addOrderBy('c.id', 'desc');
118
        $grid = new GridHelper();
119
        $grid->setId('client-grid');
120
        $grid->setPerPage(5);
121
        $grid->setMaxResult(5);
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
        $grid->setInfiniteGrid(true);
124
        $grid->setRoute('lc_dev_client_grid');
125
        return array('grid' => $grid->createView($request));
126
    }
127
128
    /**
129
     * @Route("/grid/developer/filter", name="lc_dev_client_grid_developer_filter")
130
     * @Template()
131
     */
132
    public function gridDeveloperFilterAction(Request $request)
133
    {
134
        $grid = new GridHelper();
135
        $grid->setId('developer-filter-grid');
136
        $grid->setPerPage(5);
137
        $grid->setMaxResult(5);
138
        $parms = $request->get('ac_data');
139
        if (isset($parms['username'])) {
140
            $em  = $this->getDoctrine()->getManager();
141
            $sql = $em->getRepository('LoginCidadaoCoreBundle:Person')->createQueryBuilder('u');
142
            $sql->select('u');
143
            $sql->where('1=1');
144
            $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');
145
            $sql->setParameter('1',
146
                '%'.addcslashes($parms['username'], '\\%_').'%');
147
            $sql->addOrderBy('u.id', 'desc');
148
            $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

148
            /** @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...
149
        }
150
        $grid->setInfiniteGrid(true);
151
        $grid->setRouteParams(array('ac_data'));
152
        $grid->setRoute('lc_dev_client_grid_developer_filter');
153
        return array('grid' => $grid->createView($request));
154
    }
155
156
    /**
157
     * @Route("/grid/developer", name="lc_dev_client_grid_developer")
158
     * @Template()
159
     */
160
    public function gridDeveloperAction(Request $request)
161
    {
162
        $grid = new GridHelper();
163
        $grid->setId('developer-grid');
164
        $grid->setPerPage(5);
165
        $grid->setMaxResult(5);
166
        $parms = $request->get('ac_data');
167
        if (isset($parms['person_id']) && !empty($parms['person_id'])) {
168
            $em  = $this->getDoctrine()->getManager();
169
            $sql = $em->getRepository('LoginCidadaoCoreBundle:Person')->createQueryBuilder('p');
170
            $sql->where('p.id in(:id)')->setParameter('id', $parms['person_id']);
171
            $sql->addOrderBy('p.id', 'desc');
172
            $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

172
            /** @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...
173
        }
174
        $grid->setInfiniteGrid(true);
175
        $grid->setRouteParams(array('ac_data'));
176
        $grid->setRoute('lc_dev_client_grid_developer');
177
        return array('grid' => $grid->createView($request));
178
    }
179
}
180