IssueController::createDeleteForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * This file is part of the GitControlBundle package.
4
 *
5
 * (c) Paul Schweppe <[email protected]>
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 VersionControl\GitControlBundle\Controller\Issues;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use VersionControl\GitControlBundle\Controller\Base\BaseProjectController;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
18
use VersionControl\GitControlBundle\Entity\Issues\IssueInterface;
19
use VersionControl\GitControlBundle\Entity\Issues\IssueCommentInteface;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use VersionControl\GitControlBundle\Repository\Issues\IssueRepositoryInterface;
22
use VersionControl\GitControlBundle\Annotation\ProjectAccess;
23
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
24
25
/**
26
 * Issue controller.
27
 *
28
 * @Route("project/{id}/issue")
29
 */
30
class IssueController extends BaseProjectController
31
{
32
    /**
33
     * @var IssueRepositoryInterface
34
     */
35
    protected $issueRepository;
36
37
    /**
38
     * @var \VersionControl\GitControlBundle\Repository\Issues\IssueRepositoryManager;
39
     */
40
    protected $issueManager;
41
42
    /**
43
     * Lists all Issue entities.
44
     *
45
     * @Route("s/", name="issues")
46
     * @Method("GET")
47
     * @Template()
48
     * @ProjectAccess(grantType="VIEW")
49
     */
50
    public function indexAction($id, Request $request)
51
    {
52
        $keyword = $request->query->get('keyword', false);
53
        $filter = $request->query->get('filter', 'open');
54
55
        $data = $this->issueRepository->findIssues($keyword, $filter);
56
        //$query = $em->getRepository('VersionControlGitControlBundle:Issue')->findByProjectAndStatus($this->project,$filter,$keyword,null,true)->getQuery();
57
58
        $paginator = $this->get('knp_paginator');
59
        $pagination = $paginator->paginate(
60
            $data,
61
            $request->query->getInt('page', 1)/*page number*/,
62
            15/*limit per page*/
63
        );
64
65
        //$openIssuesCount = $em->getRepository('VersionControlGitControlBundle:Issue')->countIssuesForProjectWithStatus($this->project,'open',$keyword);
66
        //$closedIssuesCount = $em->getRepository('VersionControlGitControlBundle:Issue')->countIssuesForProjectWithStatus($this->project,'closed',$keyword);
67
        $openIssuesCount = $this->issueRepository->countFindIssues($keyword, 'open');
68
        $closedIssuesCount = $this->issueRepository->countFindIssues($keyword, 'closed');
69
70
        return array_merge($this->viewVariables, array(
71
            //'entities' => $entities,
72
            'openIssuesCount' => $openIssuesCount,
73
            'closedIssuesCount' => $closedIssuesCount,
74
            'pagination' => $pagination,
75
76
        ));
77
    }
78
79
    /**
80
     * Lists latest Issue entities.
81
     *
82
     * @Route("s/latest/", name="issues_latest")
83
     * @Method("GET")
84
     * @Template()
85
     * @ProjectAccess(grantType="VIEW")
86
     */
87
    public function latestAction($id, Request $request)
88
    {
89
        $data = $this->issueRepository->findIssues('', 'open');
90
91
        $paginator = $this->get('knp_paginator');
92
        $pagination = $paginator->paginate(
93
            $data,
94
            $request->query->getInt('page', 1)/*page number*/,
95
            5/*limit per page*/
96
        );
97
98
        //$openIssuesCount = $em->getRepository('VersionControlGitControlBundle:Issue')->countIssuesForProjectWithStatus($project,'open',false);
99
100
        return array_merge($this->viewVariables, array(
101
            //'openIssuesCount' => $openIssuesCount,
102
            'pagination' => $pagination,
103
104
        ));
105
    }
106
107
    /**
108
     * Creates a new Issue entity.
109
     *
110
     * @Route("/", name="issue_create")
111
     * @Method("POST")
112
     * @Template("VersionControlGitControlBundle:Issues/Issue:new.html.twig")
113
     * @ProjectAccess(grantType="VIEW")
114
     */
115
    public function createAction(Request $request, $id)
116
    {
117
        $issueEntity = $this->issueRepository->newIssue();
118
        $form = $this->createCreateForm($issueEntity);
119
        $form->handleRequest($request);
120
121
        if ($form->isValid()) {
122
            $issueEntity = $this->issueRepository->createIssue($issueEntity);
123
            $this->get('session')->getFlashBag()->add('notice', 'Issue #'.$issueEntity->getId().' successfully updated');
124
125
            return $this->redirect($this->generateUrl('issues', array('id' => $this->project->getId())));
126
        }
127
128
        return array_merge($this->viewVariables, array(
129
            'entity' => $issueEntity,
130
            'form' => $form->createView(),
131
        ));
132
    }
133
134
    /**
135
     * Creates a form to create a Issue entity.
136
     *
137
     * @param Issue $entity The entity
138
     *
139
     * @return \Symfony\Component\Form\Form The form
140
     */
141
    private function createCreateForm(IssueInterface $entity)
142
    {
143
        $issueFormType = $this->issueManager->getIssueFormType();
144
        $form = $this->createForm($issueFormType, $entity, array(
145
            'action' => $this->generateUrl('issue_create', array('id' => $this->project->getId())),
146
            'method' => 'POST',
147
            'data_class' => get_class($entity), // Where we store our entities
148
149
        ));
150
151
        $form->add('submit', SubmitType::class, array('label' => 'Create'));
152
153
        return $form;
154
    }
155
156
    /**
157
     * Displays a form to create a new Issue entity.
158
     *
159
     * @Route("/new/", name="issue_new")
160
     * @Method("GET")
161
     * @Template()
162
     * @ProjectAccess(grantType="VIEW")
163
     */
164
    public function newAction($id)
165
    {
166
        $issueEntity = $this->issueRepository->newIssue();
167
        $form = $this->createCreateForm($issueEntity);
168
169
        return array_merge($this->viewVariables, array(
170
            'entity' => $issueEntity,
171
            'form' => $form->createView(),
172
        ));
173
    }
174
175
    /**
176
     * Finds and displays a Issue entity.
177
     *
178
     * @Route("/{issueId}", name="issue_show")
179
     * @Method("GET")
180
     * @Template()
181
     * @ProjectAccess(grantType="VIEW")
182
     */
183
    public function showAction($id, $issueId)
184
    {
185
        $issueEntity = $this->issueRepository->findIssueById($issueId);
186
187
        if (!$issueEntity) {
188
            throw $this->createNotFoundException('Unable to find Issue entity.');
189
        }
190
191
        $deleteForm = $this->createDeleteForm($issueId);
192
193
        //@TODO: Needs to update
194
        //$issueComment = new IssueComment();
195
        $issueComment = $this->issueRepository->newIssueComment();
0 ignored issues
show
Bug introduced by
The method newIssueComment() does not exist on VersionControl\GitContro...ssueRepositoryInterface. Did you maybe mean newIssue()? ( Ignorable by Annotation )

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

195
        /** @scrutinizer ignore-call */ 
196
        $issueComment = $this->issueRepository->newIssueComment();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
        $issueComment->setIssue($issueEntity);
197
        $commentForm = $this->createCommentForm($issueComment, $issueId);
198
199
        return array_merge($this->viewVariables, array(
200
            'entity' => $issueEntity,
201
            'delete_form' => $deleteForm->createView(),
202
            'comment_form' => $commentForm->createView(),
203
        ));
204
    }
205
206
    /**
207
     * Displays a form to edit an existing Issue entity.
208
     *
209
     * @Route("/edit/{issueId}", name="issue_edit")
210
     * @Method("GET")
211
     * @Template()
212
     * @ProjectAccess(grantType="VIEW")
213
     */
214
    public function editAction($id, $issueId)
215
    {
216
        $issueEntity = $this->issueRepository->findIssueById($issueId);
217
218
        $editForm = $this->createEditForm($issueEntity);
219
        $deleteForm = $this->createDeleteForm($issueId);
220
221
        return array_merge($this->viewVariables, array(
222
            'issue' => $issueEntity,
223
            'edit_form' => $editForm->createView(),
224
            'delete_form' => $deleteForm->createView(),
225
        ));
226
    }
227
228
    /**
229
     * Creates a form to edit a Issue entity.
230
     *
231
     * @param Issue $entity The entity
232
     *
233
     * @return \Symfony\Component\Form\Form The form
234
     */
235
    private function createEditForm(IssueInterface $entity)
236
    {
237
        $issueFormEditType = $this->issueManager->getIssueEditFormType();
238
        $form = $this->createForm($issueFormEditType, $entity, array(
239
            'action' => $this->generateUrl('issue_update', array('id' => $this->project->getId(), 'issueId' => $entity->getId())),
240
            'method' => 'PUT',
241
            'data_class' => get_class($entity),
242
        ));
243
244
        $form->add('submit', SubmitType::class, array('label' => 'Update'));
245
246
        return $form;
247
    }
248
    /**
249
     * Edits an existing Issue entity.
250
     *
251
     * @Route("/{issueId}", name="issue_update")
252
     * @Method("PUT")
253
     * @Template("VersionControlGitControlBundle:Issues/Issue:edit.html.twig")
254
     * @ProjectAccess(grantType="VIEW")
255
     */
256
    public function updateAction(Request $request, $id, $issueId)
257
    {
258
        $issueEntity = $this->issueRepository->findIssueById($issueId);
259
260
        $deleteForm = $this->createDeleteForm($id);
261
        $editForm = $this->createEditForm($issueEntity);
262
        $editForm->handleRequest($request);
263
264
        if ($editForm->isValid()) {
265
            $this->issueRepository->updateIssue($issueEntity);
266
267
            $this->get('session')->getFlashBag()->add('notice', 'Issue #'.$issueEntity->getId().' successfully updated');
268
269
            return $this->redirect($this->generateUrl('issue_edit', array('id' => $this->project->getId(), 'issueId' => $issueId)));
270
        }
271
272
        return array_merge($this->viewVariables, array(
273
            'entity' => $entity,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $entity seems to be never defined.
Loading history...
274
            'edit_form' => $editForm->createView(),
275
            'delete_form' => $deleteForm->createView(),
276
        ));
277
    }
278
    /**
279
     * Deletes a Issue entity.
280
     *
281
     * @Route("/", name="issue_delete")
282
     * @Method("DELETE")
283
     * @ProjectAccess(grantType="EDIT")
284
     */
285
    public function deleteAction(Request $request, $id)
286
    {
287
        $form = $this->createDeleteForm($id);
288
        $form->handleRequest($request);
289
290
        if ($form->isValid()) {
291
            $issueEntity = $this->issueRepository->findIssueById($issueId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $issueId seems to be never defined.
Loading history...
292
293
            if (!$issueEntity) {
294
                throw $this->createNotFoundException('Unable to find Issue entity.');
295
            }
296
        }
297
298
        return $this->redirect($this->generateUrl('issue'));
299
    }
300
301
    /**
302
     * Displays a form to edit an existing Issue entity.
303
     *
304
     * @Route("/close/{issueId}", name="issue_close")
305
     * @Method("GET")
306
     * @ProjectAccess(grantType="EDIT")
307
     */
308
    public function closeAction($id, $issueId)
309
    {
310
        $issueEntity = $this->issueRepository->closeIssue($issueId);
311
312
        if (!$issueEntity) {
313
            throw $this->createNotFoundException('Unable to find Issue entity.');
314
        }
315
316
        $this->get('session')->getFlashBag()->add('notice', 'Issue #'.$issueEntity->getId().' has been closed');
317
318
        return $this->redirect($this->generateUrl('issues', array('id' => $this->project->getId())));
319
    }
320
321
    /**
322
     * Displays a form to edit an existing Issue entity.
323
     *
324
     * @Route("/open/{issueId}", name="issue_open")
325
     * @Method("GET")
326
     * @ProjectAccess(grantType="EDIT")
327
     */
328
    public function openAction($id, $issueId)
329
    {
330
        $issueEntity = $this->issueRepository->reOpenIssue($issueId);
331
332
        if (!$issueEntity) {
333
            throw $this->createNotFoundException('Unable to find Issue entity.');
334
        }
335
336
        $this->get('session')->getFlashBag()->add('notice', 'Issue #'.$issueEntity->getId().' has been re-opened');
337
338
        return $this->redirect($this->generateUrl('issue_show', array('id' => $this->project->getId(), 'issueId' => $issueEntity->getId())));
339
    }
340
341
    /**
342
     * Displays a form to edit an existing Issue entity.
343
     *
344
     * @Route("/hook/", name="issue_hook")
345
     */
346
    public function hookAction($id)
347
    {
348
        $this->gitCommands = $this->get('version_control.git_command')->setProject($this->project);
349
        $message = $this->gitCommands->getLastMessageLog();
350
351
        //close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved
352
        //'/#(\d+)/'
353
        $matches = array();
354
        if (preg_match('/(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved) #(\d+)/i', $message, $matches)) {
355
            foreach ($matches as $issueId) {
356
                if (is_numeric($issueId)) {
357
                    $issueEntity = $this->issueRepository->findIssueById($issueId);
0 ignored issues
show
Unused Code introduced by
The assignment to $issueEntity is dead and can be removed.
Loading history...
358
                    if ($issue) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $issue does not exist. Did you maybe mean $issueEntity?
Loading history...
359
                        $this->issueRepository->closeIssue($issueId);
360
                    }
361
                }
362
            }
363
        }
364
365
        return new JsonResponse(array('success' => true));
366
    }
367
368
    /**
369
     * Creates a form to delete a Issue entity by id.
370
     *
371
     * @param mixed $id The entity id
372
     *
373
     * @return \Symfony\Component\Form\Form The form
374
     */
375
    private function createDeleteForm($id)
376
    {
377
        return $this->createFormBuilder()
378
            ->setAction($this->generateUrl('issue_delete', array('id' => $id)))
379
            ->setMethod('DELETE')
380
            ->add('submit', SubmitType::class, array('label' => 'Delete'))
381
            ->getForm()
382
        ;
383
    }
384
385
    /**
386
     * Creates a new Issue comment entity.
387
     *
388
     * @Route("/comment/{issueId}", name="issuecomment_create")
389
     * @Method("POST")
390
     * @Template("VersionControlGitControlBundle:Issues/Issue:show.html.twig")
391
     * @ProjectAccess(grantType="EDIT")
392
     */
393
    public function createCommentAction(Request $request, $id, $issueId)
394
    {
395
        $issueComment = $this->issueRepository->newIssueComment();
396
        $issueEntity = $this->issueRepository->findIssueById($issueId);
397
398
        $commentForm = $this->createCommentForm($issueComment, $issueId);
399
        $commentForm->handleRequest($request);
400
401
        if ($commentForm->isValid()) {
402
            $issueComment->setIssue($issueEntity);
403
            $issueComment = $this->issueRepository->createIssueComment($issueComment);
0 ignored issues
show
Unused Code introduced by
The assignment to $issueComment is dead and can be removed.
Loading history...
Bug introduced by
The method createIssueComment() does not exist on VersionControl\GitContro...ssueRepositoryInterface. Did you maybe mean createIssue()? ( Ignorable by Annotation )

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

403
            /** @scrutinizer ignore-call */ 
404
            $issueComment = $this->issueRepository->createIssueComment($issueComment);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
404
            //$issueId = $issueComment->getIssue()->getId();
405
406
            if ($commentForm->get('createClose')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

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

406
            if ($commentForm->get('createClose')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
407
                $this->issueRepository->closeIssue($issueId);
408
            }
409
410
           // $em->persist($entity);
411
           // $em->flush();
412
413
            return $this->redirect($this->generateUrl('issue_show', array('id' => $this->project->getId(), 'issueId' => $issueId)));
414
        }
415
416
        $deleteForm = $this->createDeleteForm($issueId);
417
418
        return array_merge($this->viewVariables, array(
419
            'entity' => $issueEntity,
420
            'delete_form' => $deleteForm->createView(),
421
            'comment_form' => $commentForm->createView(),
422
423
        ));
424
    }
425
426
    /**
427
     * Creates a form to create a Issue entity.
428
     *
429
     * @param Issue $entity The entity
430
     *
431
     * @return \Symfony\Component\Form\Form The form
432
     */
433
    private function createCommentForm(IssueCommentInteface $entity, $issueId)
434
    {
435
        $issueCommentType = $this->issueManager->getIssueCommentFormType();
436
        $form = $this->createForm($issueCommentType, $entity, array(
437
            'action' => $this->generateUrl('issuecomment_create', array('id' => $this->project->getId(), 'issueId' => $issueId)),
438
            'method' => 'POST',
439
            'data_class' => get_class($entity),
440
        ));
441
442
        $form->add('create', SubmitType::class, array('label' => 'Create'));
443
        $form->add('createClose', SubmitType::class, array('label' => 'Create & Close'));
444
445
        return $form;
446
    }
447
448
    /**
449
     * Displays a form to edit an existing Issue entity.
450
     *
451
     * @Route("s/search/", name="issue_searchajax")
452
     * @Method("GET")
453
     * @ProjectAccess(grantType="VIEW")
454
     */
455
    public function searchAjaxAction($id, Request $request)
456
    {
457
        $keyword = $request->query->get('keyword', false);
458
        $filter = $request->query->get('filter', 'open');
459
460
        if ($keyword && is_numeric($keyword)) {
461
            $data = array();
462
            //Get issue by id
463
            try {
464
                $issue = $this->issueRepository->findIssueById($keyword);
465
                if ($issue) {
466
                    $data[] = $issue;
467
                }
468
            } catch (\Exception $e) {
469
                //return new JsonResponse(array('success' => false, 'error' => $result));
470
            }
471
        } else {
472
            $data = $this->issueRepository->findIssues($keyword, $filter);
473
        }
474
        //$issueEntities = $em->getRepository('VersionControlGitControlBundle:Issue')->findByProjectAndStatus($project,$filter,$keyword,null,false);
475
476
        if ($data instanceof \Doctrine\ORM\QueryBuilder) {
0 ignored issues
show
introduced by
$data is never a sub-type of Doctrine\ORM\QueryBuilder.
Loading history...
477
            $issueEntities = $data->getQuery()->getResult();
478
        } else {
479
            $issueEntities = $data;
480
        }
481
        $result = [];
482
        foreach ($issueEntities as $issueEntity) {
483
            $result[] = array(
484
                'id' => $issueEntity->getId(), 'title' => $issueEntity->getTitle(), 'description' => $issueEntity->getDescription(), 'status' => $issueEntity->getStatus(), 'author' => $issueEntity->getUser()->getName(),
485
            );
486
        }
487
488
        return new JsonResponse(array('success' => true, 'results' => $result));
489
    }
490
491
    /**
492
     * Displays a form to edit an existing Issue entity.
493
     *
494
     * @Route("/find/{issueId}", name="issue_findajax")
495
     * @Method("GET")
496
     * @TODO Pass in project id
497
     * @ProjectAccess(grantType="VIEW")
498
     */
499
    public function findAjaxAction($id, $issueId = '')
500
    {
501
        //$em = $this->getDoctrine()->getManager();
502
        //$issueEntity = $em->getRepository('VersionControlGitControlBundle:Issue')->find($issueId);
503
        $issueEntity = $this->issueRepository->findIssueById($issueId);
504
        if (!$issueEntity) {
505
            return new JsonResponse(array('success' => false, 'error' => 'No issue exists matching this id.'));
506
        }
507
        $result = array(
508
            'id' => $issueEntity->getId(), 'title' => $issueEntity->getTitle(), 'description' => $issueEntity->getDescription(), 'status' => $issueEntity->getStatus(),
509
        );
510
511
        return new JsonResponse(array('success' => true, 'result' => $result));
512
    }
513
514
    /**
515
     * Finds and displays a Issue entity.
516
     *
517
     * @Route("/modal/{issueId}", name="issue_show_modal")
518
     * @Method("GET")
519
     * @Template()
520
     * @ProjectAccess(grantType="VIEW")
521
     */
522
    public function showModalAction($id, $issueId)
523
    {
524
        $issueEntity = $this->issueRepository->findIssueById($issueId);
525
526
        if (!$issueEntity) {
527
            throw $this->createNotFoundException('Unable to find Issue entity.');
528
        }
529
530
        return array(
531
            'entity' => $issueEntity,
532
            'project' => $this->project,
533
        );
534
    }
535
536
    /**
537
     * @param int $id
538
     */
539
    public function initAction($id, $grantType = 'VIEW')
540
    {
541
        $redirectUrl = parent::initAction($id, $grantType);
542
        if ($redirectUrl) {
543
            return $redirectUrl;
544
        }
545
546
        $em = $this->getDoctrine()->getManager();
547
        $issueIntegrator = $em->getRepository('VersionControlGitControlBundle:ProjectIssueIntegrator')->findOneByProject($this->project);
548
549
        $this->issueManager = $this->get('version_control.issue_repository_manager');
550
        if ($issueIntegrator) {
551
            $this->issueManager->setIssueIntegrator($issueIntegrator);
552
        } else {
553
            $this->issueManager->setProject($this->project);
554
        }
555
        $this->issueRepository = $this->issueManager->getIssueRepository();
556
    }
557
}
558