Failed Conditions
Pull Request — master (#142)
by Zac
03:56
created

TestGroupApiController::deleteGroupAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Overwatch\TestBundle\Controller;
4
5
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6
use Overwatch\TestBundle\Entity\TestGroup;
7
use Overwatch\UserBundle\Entity\User;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
12
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * TestGroupApiController
19
 * Handles API requests made for TestGroups
20
 * 
21
 * @Route("/api/groups")
22
 */
23
class TestGroupApiController extends Controller
0 ignored issues
show
Coding Style introduced by
The property $_em is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
24
{
25
    private $_em;
26
    
27
    public function setContainer(ContainerInterface $container = null)
28
    {
29
        parent::setContainer($container);
30
        $this->_em = $this->getDoctrine()->getManager();
31
    }
32
    
33
    /**
34
     * Creates a new group
35
     * 
36
     * @Route("")
37
     * @Method({"POST"})
38
     * @Security("has_role('ROLE_SUPER_ADMIN')")
39
     * @ApiDoc(
40
     *     resource=true,
41
     *     parameters={
42
     *         {"name"="name", "description"="A user-friendly name for the group", "required"=true, "format"="Group 1", "dataType"="string"},
43
     *     },
44
     *     tags={
45
     *         "Super Admin" = "#ff1919"
46
     *     }
47
     * )
48
     */
49
    public function createGroupAction(Request $request)
50
    {
51
        if ($request->request->get('name') === null) {
52
            return new JsonResponse('You must provide a name for the new group', JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
53
        }
54
        
55
        $group = new TestGroup;
56
        $group
57
            ->setName($request->request->get('name'))
58
        ;
59
        
60
        $this->_em->persist($group);
61
        $this->_em->flush();
62
        
63
        return new JsonResponse($group, JsonResponse::HTTP_CREATED);
64
    }
65
    
66
    /**
67
     * Returns a list of all groups the current user has access to
68
     * 
69
     * @Route("")
70
     * @Method({"GET"})
71
     * @Security("has_role('ROLE_USER')")
72
     * @ApiDoc(
73
     *     tags={
74
     *         "Super Admin" = "#ff1919",
75
     *         "Admin" = "#ffff33",
76
     *         "User" = "#75ff47"
77
     *     }
78
     * )
79
     */
80
    public function getAllGroupsAction()
81
    {
82
        if ($this->isGranted('ROLE_SUPER_ADMIN')) {
83
            $groups = $this->_em->getRepository('OverwatchTestBundle:TestGroup')->findAll();
84
        } else {
85
            $groups = $this->getUser()->getGroups()->toArray();
86
        }
87
        
88
        return new JsonResponse($groups);
89
    }
90
    
91
    /**
92
     * Returns the details of the specified group
93
     * 
94
     * @Route("/{id}")
95
     * @Method({"GET"})
96
     * @Security("is_granted('view', group)")
97
     * @ApiDoc(
98
     *     requirements={
99
     *         {"name"="id", "description"="The ID of the group to return", "dataType"="integer", "requirement"="\d+"}
100
     *     },
101
     *     tags={
102
     *         "Super Admin" = "#ff1919",
103
     *         "Admin" = "#ffff33",
104
     *         "User" = "#75ff47"
105
     *     }
106
     * )
107
     */
108
    public function getGroupAction(TestGroup $group)
109
    {
110
        return new JsonResponse($group);
111
    }
112
    
113
    /**
114
     * Updates the given group
115
     * 
116
     * @Route("/{id}")
117
     * @Method({"PUT"})
118
     * @Security("has_role('ROLE_SUPER_ADMIN')")
119
     * @ApiDoc(
120
     *     parameters={
121
     *         {"name"="name", "description"="A user-friendly name for the group", "required"=false, "format"="Group 1", "dataType"="string"}
122
     *     },
123
     *     requirements={
124
     *         {"name"="id", "description"="The ID of the group to edit", "dataType"="integer", "requirement"="\d+"}
125
     *     },
126
     *     tags={
127
     *         "Super Admin" = "#ff1919"
128
     *     }
129
     * ) 
130
     */
131
    public function updateGroupAction(Request $request, TestGroup $group)
132
    {
133
        if ($request->request->has('name')) {
134
            $group->setName($request->request->get('name'));
135
            
136
            $this->_em->flush();
137
        }
138
        
139
        return new JsonResponse($group);
140
    }
141
    
142
    /**
143
     * Deletes the given group
144
     * 
145
     * @Route("/{id}")
146
     * @Method({"DELETE"})
147
     * @Security("has_role('ROLE_SUPER_ADMIN')")
148
     * @ApiDoc(
149
     *     requirements={
150
     *         {"name"="id", "description"="The ID of the group to delete", "dataType"="integer", "requirement"="\d+"}
151
     *     },
152
     *     tags={
153
     *         "Super Admin" = "#ff1919"
154
     *     }
155
     * )
156
     */
157
    public function deleteGroupAction(TestGroup $group)
158
    {
159
        if ($group->getUsers()->count() + $group->getTests()->count() !== 0) {
160
            return new JsonResponse('This group still has users and/or tests in it. You must remove them before continuing.', JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
161
        }
162
        
163
        $this->_em->remove($group);
164
        $this->_em->flush();
165
        
166
        return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
167
    }
168
    
169
    /**
170
     * Adds the given user to the given group
171
     * 
172
     * @Route("/{groupId}/user/{userId}")
173
     * @Method({"POST"})
174
     * @Security("has_role('ROLE_SUPER_ADMIN')")
175
     * @ParamConverter("group", class="OverwatchTestBundle:TestGroup", options={"id" = "groupId"})
176
     * @ParamConverter("user", class="OverwatchUserBundle:User", options={"id" = "userId"})
177
     * @ApiDoc(
178
     *     resource=true,
179
     *     requirements={
180
     *         {"name"="userId", "description"="The ID of the user", "dataType"="integer", "requirement"="\d+"},
181
     *         {"name"="groupId", "description"="The ID of the group", "dataType"="integer", "requirement"="\d+"}
182
     *     },
183
     *     tags={
184
     *         "Super Admin" = "#ff1919"
185
     *     }
186
     * )
187
     */
188
    public function addUserToGroupAction(TestGroup $group, User $user)
189
    {
190
        $group->addUser($user);
191
        $this->_em->flush();
192
        
193
        return new JsonResponse($user, JsonResponse::HTTP_CREATED);
194
    }
195
    
196
    /**
197
     * Removes the given user from the given group
198
     * 
199
     * @Route("/{groupId}/user/{userId}")
200
     * @Method({"DELETE"})
201
     * @Security("has_role('ROLE_SUPER_ADMIN')")
202
     * @ParamConverter("group", class="OverwatchTestBundle:TestGroup", options={"id" = "groupId"})
203
     * @ParamConverter("user", class="OverwatchUserBundle:User", options={"id" = "userId"})
204
     * @ApiDoc(
205
     *     requirements={
206
     *         {"name"="userId", "description"="The ID of the user", "dataType"="integer", "requirement"="\d+"},
207
     *         {"name"="groupId", "description"="The ID of the group", "dataType"="integer", "requirement"="\d+"}
208
     *     },
209
     *     tags={
210
     *         "Super Admin" = "#ff1919"
211
     *     }
212
     * )
213
     */
214
    public function removeUserFromGroupAction(TestGroup $group, User $user)
215
    {
216
        $group->removeUser($user);
217
        $this->_em->flush();
218
        
219
        return new JsonResponse($user);
220
    }
221
}
222