Completed
Push — master ( 46aee1...d8eca0 )
by Zac
44:12 queued 33:10
created

TestGroupApiController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 5 Features 0
Metric Value
wmc 12
c 7
b 5
f 0
lcom 1
cbo 6
dl 0
loc 199
ccs 40
cts 40
cp 1
rs 10

8 Methods

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