Completed
Pull Request — master (#45)
by Laurent
04:03
created

ArticleController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 161
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 1
cbo 6
dl 161
loc 161
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A showAction() 9 9 1
A sortAction() 6 6 1
A deleteAction() 12 12 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
 * ArticleController controller des articles.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\Article;
23
use AppBundle\Form\Type\ArticleType;
24
25
/**
26
 * Article controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/articles")
31
 */
32 View Code Duplication
class ArticleController extends AbstractController
0 ignored issues
show
Duplication introduced by
This class 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...
33
{
34
    /**
35
     * Lists all Article entities.
36
     *
37
     * @Route("/", name="articles")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction(Request $request)
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $qb = $em->getRepository('AppBundle:Article')->getArticles();
45
        $this->addQueryBuilderSort($qb, 'article');
46
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 5);
47
        
48
        return array(
49
            'paginator' => $paginator,
50
        );
51
    }
52
53
    /**
54
     * Finds and displays a Article entity.
55
     *
56
     * @Route("/{slug}/show", name="articles_show")
57
     * @Method("GET")
58
     * @Template()
59
     */
60
    public function showAction(Article $article)
61
    {
62
        $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete');
63
64
        return array(
65
            'article' => $article,
66
            'delete_form' => $deleteForm->createView(),
67
        );
68
    }
69
70
    /**
71
     * Displays a form to create a new Article entity.
72
     *
73
     * @Route("/new", name="articles_new")
74
     * @Method("GET")
75
     * @Template()
76
     */
77
    public function newAction()
78
    {
79
        $article = new Article();
80
        $form = $this->createForm(new ArticleType(), $article);
81
82
        return array(
83
            'article' => $article,
84
            'form'   => $form->createView(),
85
        );
86
    }
87
88
    /**
89
     * Creates a new Article entity.
90
     *
91
     * @Route("/create", name="articles_create")
92
     * @Method("POST")
93
     * @Template("AppBundle:Article:new.html.twig")
94
     */
95
    public function createAction(Request $request)
96
    {
97
        $article = new Article();
98
        $form = $this->createForm(new ArticleType(), $article);
99
        if ($form->handleRequest($request)->isValid()) {
100
            $em = $this->getDoctrine()->getManager();
101
            $em->persist($article);
102
            $em->flush();
103
104
            return $this->redirectToRoute('articles_show', array('slug' => $article->getSlug()));
105
        }
106
107
        return array(
108
            'article' => $article,
109
            'form'   => $form->createView(),
110
        );
111
    }
112
113
    /**
114
     * Displays a form to edit an existing Article entity.
115
     *
116
     * @Route("/{slug}/edit", name="articles_edit")
117
     * @Method("GET")
118
     * @Template()
119
     */
120
    public function editAction(Article $article)
121
    {
122
        $editForm = $this->createForm(new ArticleType(), $article, array(
123
            'action' => $this->generateUrl('articles_update', array('slug' => $article->getSlug())),
124
            'method' => 'PUT',
125
        ));
126
        $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete');
127
128
        return array(
129
            'article' => $article,
130
            'edit_form'   => $editForm->createView(),
131
            'delete_form' => $deleteForm->createView(),
132
        );
133
    }
134
135
    /**
136
     * Edits an existing Article entity.
137
     *
138
     * @Route("/{slug}/update", name="articles_update")
139
     * @Method("PUT")
140
     * @Template("AppBundle:Article:edit.html.twig")
141
     */
142
    public function updateAction(Article $article, Request $request)
143
    {
144
        $editForm = $this->createForm(new ArticleType(), $article, array(
145
            'action' => $this->generateUrl('articles_update', array('slug' => $article->getSlug())),
146
            'method' => 'PUT',
147
        ));
148
        if ($editForm->handleRequest($request)->isValid()) {
149
            $this->getDoctrine()->getManager()->flush();
150
151
            return $this->redirectToRoute('articles_edit', array('slug' => $article->getSlug()));
152
        }
153
        $deleteForm = $this->createDeleteForm($article->getId(), 'articles_delete');
154
155
        return array(
156
            'article' => $article,
157
            'edit_form'   => $editForm->createView(),
158
            'delete_form' => $deleteForm->createView(),
159
        );
160
    }
161
162
163
    /**
164
     * Save order.
165
     *
166
     * @Route("/order/{field}/{type}", name="articles_sort")
167
     */
168
    public function sortAction($field, $type)
169
    {
170
        $this->setOrder('article', $field, $type);
171
172
        return $this->redirect($this->generateUrl('articles'));
173
    }
174
    /**
175
     * Deletes a Article entity.
176
     *
177
     * @Route("/{id}/delete", name="articles_delete", requirements={"id"="\d+"})
178
     * @Method("DELETE")
179
     */
180
    public function deleteAction(Article $article, Request $request)
181
    {
182
        $form = $this->createDeleteForm($article->getId(), 'articles_delete');
183
        if ($form->handleRequest($request)->isValid()) {
184
            $em = $this->getDoctrine()->getManager();
185
            $article->setActive(false);
186
            $em->persist($article);
187
            $em->flush();
188
        }
189
190
        return $this->redirectToRoute('articles');
191
    }
192
}
193