Failed Conditions
Pull Request — master (#823)
by Guilherme
12:36 queued 04:55
created

getOrganizationRepository()   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 4
cp 0
crap 2
rs 10
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 LoginCidadao\OAuthBundle\Entity\OrganizationRepository;
14
use LoginCidadao\OAuthBundle\Form\OrganizationType;
15
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
21
use LoginCidadao\OAuthBundle\Entity\Organization;
22
use LoginCidadao\OAuthBundle\Model\OrganizationInterface;
23
24
/**
25
 * @Route("/organizations")
26
 * @Security("has_role('FEATURE_ORGANIZATIONS')")
27
 */
28
class OrganizationController extends Controller
29
{
30
31
    /**
32
     * @Route("/", name="lc_organizations_list")
33
     * @Template()
34
     */
35
    public function listAction()
36
    {
37
        $checker = $this->get('security.authorization_checker');
38
39
        $myOrganizations = $this->fetchMyOrganizations();
40
        $otherOrganizations = [];
41
42
        if ($checker->isGranted('ROLE_ORGANIZATIONS_LIST_ALL')) {
43
            $otherOrganizations = $this->fetchOtherOrganizations();
44
        }
45
46
        return compact('myOrganizations', 'otherOrganizations');
47
    }
48
49
    /**
50
     * @Route("/new", name="lc_organizations_new")
51
     * @Template()
52
     * @Security("has_role('ROLE_ORGANIZATIONS_CREATE')")
53
     */
54
    public function newAction(Request $request)
55
    {
56
        $organization = new Organization();
57
58
        $form = $this->createForm(OrganizationType::class, $organization);
59
60
        $form->handleRequest($request);
61
62
        $em = $this->getDoctrine()->getManager();
63
        if ($form->isValid()) {
64
            $organization->getMembers()[] = $this->getUser();
65
            $em->persist($organization);
66
            $em->flush();
67
68
            return $this->redirectToRoute('lc_organizations_list');
69
        }
70
71
        return compact('form');
72
    }
73
74
    /**
75
     * @Route("/{id}/edit", name="lc_organizations_edit", requirements={"id" = "\d+"})
76
     * @Template()
77
     * @Security("has_role('ROLE_ORGANIZATIONS_EDIT')")
78
     */
79
    public function editAction(Request $request, $id)
80
    {
81
        $organization = $this->getOr404($id);
82
83
        if (!$organization->getMembers()->contains($this->getUser())) {
84
            $this->denyAccessUnlessGranted('ROLE_ORGANIZATIONS_EDIT_ANY_ORG');
85
        }
86
87
        $form = $this->createForm(OrganizationType::class, $organization);
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($id)
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', SubmitType::class, [
124
                'label' => 'organizations.form.delete.yes',
125
                'attr' => ['class' => 'btn-danger'],
126
            ])
127
            ->getForm();
128
129
        $form->handleRequest($request);
130
131
        if ($form->isValid()) {
132
            $em->remove($organization);
133
            $em->flush();
134
135
            $translator = $this->get('translator');
136
            $params = ['%name%' => $organization->getName()];
137
            $message = $translator->trans('organizations.form.delete.success', $params);
138
            $this->get('session')->getFlashBag()->add('success', $message);
139
140
            return $this->redirectToRoute('lc_organizations_list');
141
        }
142
143
        return compact('organization', 'form');
144
    }
145
146
    private function getOr404($id)
147
    {
148
        $organization = $this->getDoctrine()
149
            ->getRepository('LoginCidadaoOAuthBundle:Organization')
150
            ->find($id);
151
152
        if ($organization instanceof OrganizationInterface) {
153
            return $organization;
154
        } else {
155
            throw $this->createNotFoundException();
156
        }
157
    }
158
159
    private function fetchMyOrganizations()
160
    {
161
        return $this->getOrganizationRepository()->findByMember($this->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $person of LoginCidadao\OAuthBundle...ository::findByMember() does only seem to accept LoginCidadao\CoreBundle\Model\PersonInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

161
        return $this->getOrganizationRepository()->findByMember(/** @scrutinizer ignore-type */ $this->getUser());
Loading history...
162
    }
163
164
    /**
165
     * @Security("has_role('ROLE_ORGANIZATIONS_LIST_ALL')")
166
     */
167
    private function fetchOtherOrganizations()
168
    {
169
        return $this->getOrganizationRepository()->findByNotMember($this->getUser());
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $person of LoginCidadao\OAuthBundle...tory::findByNotMember() does only seem to accept LoginCidadao\CoreBundle\Model\PersonInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

169
        return $this->getOrganizationRepository()->findByNotMember(/** @scrutinizer ignore-type */ $this->getUser());
Loading history...
170
    }
171
172
    private function getOrganizationRepository()
173
    {
174
        /** @var OrganizationRepository $repo */
175
        $repo = $this->getDoctrine()->getRepository('LoginCidadaoOAuthBundle:Organization');
176
177
        return $repo;
178
    }
179
}
180