EmailController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 147
Duplicated Lines 36.05 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 53
loc 147
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 16 1
A showAction() 0 14 2
A retryAction() 18 18 2
A resendAction() 18 18 2
A cancelAction() 17 17 2
A deleteAction() 0 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Citrax\Bundle\DatabaseSwiftMailerBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Citrax\Bundle\DatabaseSwiftMailerBundle\Entity\Email;
11
use Citrax\Bundle\DatabaseSwiftMailerBundle\Form\EmailType;
12
13
/**
14
 * Email controller.
15
 *
16
 * @Route("/email-spool")
17
 */
18
class EmailController extends Controller
19
{
20
21
    /**
22
     * Lists all Email entities.
23
     *
24
     * @Route("/{page}", name="email-spool", defaults={"page" = 1}, requirements={"page" = "\d+"})
25
     * @Method("GET")
26
     * @Template()
27
     */
28
    public function indexAction($page)
29
    {
30
        $em = $this->getDoctrine()->getManager();
31
32
        $query = $em->getRepository('CitraxDatabaseSwiftMailerBundle:Email')->getAllEmails();
33
34
        $paginator  = $this->get('knp_paginator');
35
        $pagination = $paginator->paginate(
36
            $query,
37
            $page/*page number*/,
38
            30/*limit per page*/
39
        );
40
        return array(
41
            'entities' => $pagination,
42
        );
43
    }
44
45
    /**
46
     * Finds and displays a Email entity.
47
     *
48
     * @Route("/{id}/show", name="email-spool_show")
49
     * @Method("GET")
50
     * @Template()
51
     */
52
    public function showAction($id)
53
    {
54
        $em = $this->getDoctrine()->getManager();
55
56
        $entity = $em->getRepository('CitraxDatabaseSwiftMailerBundle:Email')->find($id);
57
58
        if (!$entity) {
59
            throw $this->createNotFoundException('Unable to find Email entity.');
60
        }
61
62
        return array(
63
            'entity'      => $entity,
64
        );
65
    }
66
67
    /**
68
     * Retry to send an email
69
     *
70
     * @Route("/{id}/retry", name="email-spool_retry")
71
     * @Method("GET")
72
     */
73 View Code Duplication
    public function retryAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $em = $this->getDoctrine()->getManager();
76
77
        $entity = $em->getRepository('CitraxDatabaseSwiftMailerBundle:Email')->find($id);
78
79
        if (!$entity) {
80
            throw $this->createNotFoundException('Unable to find Email entity.');
81
        }
82
83
        $entity->setStatus(Email::STATUS_FAILED);
84
        $entity->setRetries(0);
85
86
        $em->persist($entity);
87
        $em->flush();
88
89
        return $this->redirect($this->generateUrl('email-spool'));
90
    }
91
92
    /**
93
     * Resend an email
94
     *
95
     * @Route("/{id}/resend", name="email-spool_resend")
96
     * @Method("GET")
97
     */
98 View Code Duplication
    public function resendAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        $em = $this->getDoctrine()->getManager();
101
102
        $entity = $em->getRepository('CitraxDatabaseSwiftMailerBundle:Email')->find($id);
103
104
        if (!$entity) {
105
            throw $this->createNotFoundException('Unable to find Email entity.');
106
        }
107
108
        $entity->setStatus(Email::STATUS_READY);
109
        $entity->setRetries(0);
110
111
        $em->persist($entity);
112
        $em->flush();
113
114
        return $this->redirect($this->generateUrl('email-spool'));
115
    }
116
117
    /**
118
     * Cancel an email sending
119
     *
120
     * @Route("/{id}/cancel", name="email-spool_cancel")
121
     * @Method("GET")
122
     */
123 View Code Duplication
    public function cancelAction($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $em = $this->getDoctrine()->getManager();
126
127
        $entity = $em->getRepository('CitraxDatabaseSwiftMailerBundle:Email')->find($id);
128
129
        if (!$entity) {
130
            throw $this->createNotFoundException('Unable to find Email entity.');
131
        }
132
133
        $entity->setStatus(Email::STATUS_CANCELLED);
134
135
        $em->persist($entity);
136
        $em->flush();
137
138
        return $this->redirect($this->generateUrl('email-spool'));
139
    }
140
141
    /**
142
     * Deletes a Email entity.
143
     *
144
     * @Route("/{id}/delete", name="email-spool_delete")
145
     * @Method("GET")
146
     */
147
    public function deleteAction($id)
148
    {
149
150
        $em = $this->getDoctrine()->getManager();
151
        $entity = $em->getRepository('CitraxDatabaseSwiftMailerBundle:Email')->find($id);
152
153
        if (!$entity) {
154
            throw $this->createNotFoundException('Unable to find Email entity.');
155
        }
156
157
        $em->remove($entity);
158
        $em->flush();
159
160
161
        return $this->redirect($this->generateUrl('email-spool'));
162
    }
163
164
}
165