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

OrganizationController::showAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OAuthBundle\Controller;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
18
use LoginCidadao\OAuthBundle\Entity\Organization;
19
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;
20
21
/**
22
 * @Route("/organizations")
23
 * @Security("has_role('FEATURE_ORGANIZATIONS')")
24
 */
25
class OrganizationController extends Controller
26
{
27
28
    /**
29
     * @Route("/", name="lc_organizations_list")
30
     * @Template()
31
     */
32
    public function listAction()
33
    {
34
        $checker = $this->get('security.authorization_checker');
35
36
        $myOrganizations    = $this->fetchMyOrganizations();
37
        $otherOrganizations = array();
38
39
        if ($checker->isGranted('ROLE_ORGANIZATIONS_LIST_ALL')) {
40
            $otherOrganizations = $this->fetchOtherOrganizations();
41
        }
42
43
        return compact('myOrganizations', 'otherOrganizations');
44
    }
45
46
    /**
47
     * @Route("/new", name="lc_organizations_new")
48
     * @Template()
49
     * @Security("has_role('ROLE_ORGANIZATIONS_CREATE')")
50
     */
51
    public function newAction(Request $request)
52
    {
53
        $organization = new Organization();
54
55
        $form = $this->createForm('LoginCidadao\OAuthBundle\Form\OrganizationType',
56
            $organization);
57
58
        $form->handleRequest($request);
59
60
        $em = $this->getDoctrine()->getManager();
61
        if ($form->isValid()) {
62
            $organization->getMembers()->add($this->getUser());
63
            $em->persist($organization);
64
            $em->flush();
65
66
            return $this->redirectToRoute('lc_organizations_list');
67
        }
68
69
        return compact('form');
70
    }
71
72
    /**
73
     * @Route("/{id}/edit", name="lc_organizations_edit", requirements={"id" = "\d+"})
74
     * @Template()
75
     * @Security("has_role('ROLE_ORGANIZATIONS_EDIT')")
76
     */
77
    public function editAction(Request $request, $id)
78
    {
79
        $organization = $this->getOr404($id);
80
81
        if (!$organization->getMembers()->contains($this->getUser())) {
82
            $this->denyAccessUnlessGranted('ROLE_ORGANIZATIONS_EDIT_ANY_ORG');
83
        }
84
85
        $form = $this->createForm('LoginCidadao\OAuthBundle\Form\OrganizationType',
86
            $organization);
87
88
        $form->handleRequest($request);
89
90
        $em = $this->getDoctrine()->getManager();
91
        if ($form->isValid()) {
92
            $organization->getValidationSecret();
93
            $em->persist($organization);
94
            $em->flush();
95
96
            return $this->redirectToRoute('lc_organizations_list');
97
        }
98
99
        return compact('form', 'organization');
100
    }
101
102
    /**
103
     * @Route("/{id}", name="lc_organizations_show", requirements={"id" = "\d+"})
104
     * @Template()
105
     */
106
    public function showAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

106
    public function showAction(/** @scrutinizer ignore-unused */ Request $request, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        $organization = $this->getOr404($id);
109
110
        return compact('organization');
111
    }
112
113
    /**
114
     * @Route("/{id}/delete", name="lc_organizations_delete", requirements={"id" = "\d+"})
115
     * @Template()
116
     */
117
    public function deleteAction(Request $request, $id)
118
    {
119
        $organization = $this->getOr404($id);
120
121
        $em   = $this->getDoctrine()->getManager();
122
        $form = $this->createFormBuilder($organization)
123
            ->add('delete', 'submit',
124
                array(
125
                'label' => 'organizations.form.delete.yes',
126
                'attr' => array('class' => 'btn-danger')
127
            ))
128
            ->getForm();
129
130
        $form->handleRequest($request);
131
132
        if ($form->isValid()) {
133
            $em->remove($organization);
134
            $em->flush();
135
136
            $translator = $this->get('translator');
137
            $params     = array('%name%' => $organization->getName());
138
            $message    = $translator->trans('organizations.form.delete.success',
139
                $params);
140
            $this->get('session')->getFlashBag()->add('success', $message);
141
142
            return $this->redirectToRoute('lc_organizations_list');
143
        }
144
145
        return compact('organization', 'form');
146
    }
147
148
    private function getOr404($id)
149
    {
150
        $organization = $this->getDoctrine()
151
            ->getRepository('LoginCidadaoOAuthBundle:Organization')
152
            ->find($id);
153
154
        if ($organization instanceof OrganizationInterface) {
155
            return $organization;
156
        } else {
157
            throw $this->createNotFoundException();
158
        }
159
    }
160
161
    private function fetchMyOrganizations()
162
    {
163
        return $this->getDoctrine()
164
                ->getRepository('LoginCidadaoOAuthBundle:Organization')
165
                ->findByMember($this->getUser());
166
    }
167
168
    /**
169
     * @Security("has_role('ROLE_ORGANIZATIONS_LIST_ALL')")
170
     */
171
    private function fetchOtherOrganizations()
172
    {
173
        return $this->getDoctrine()
174
                ->getRepository('LoginCidadaoOAuthBundle:Organization')
175
                ->findByNotMember($this->getUser());
176
    }
177
}
178