Completed
Pull Request — master (#336)
by Leny
06:08
created

TemplateController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 183
Duplicated Lines 10.93 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 7
dl 20
loc 183
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 14 1
B newAction() 9 27 2
B settingsAction() 11 28 2
B showAction() 0 38 1
A editAction() 0 15 2
A getNewTemplateType() 0 4 1

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 Victoire\Bundle\TemplateBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Victoire\Bundle\TemplateBundle\Entity\Template;
12
use Victoire\Bundle\TemplateBundle\Event\Menu\TemplateMenuContextualEvent;
13
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
14
15
/**
16
 * Template Controller.
17
 *
18
 * @Route("/victoire-dcms/template")
19
 */
20
class TemplateController extends Controller
21
{
22
    /**
23
     * list of all templates.
24
     *
25
     * @Route("/index", name="victoire_template_index")
26
     * @Configuration\Template()
27
     *
28
     * @return JsonResponse
29
     */
30
    public function indexAction()
31
    {
32
        $templates = $this->get('doctrine.orm.entity_manager')->getRepository('VictoireTemplateBundle:Template')->findByTemplate(null, ['position' => 'ASC']);
33
34
        return new JsonResponse(
35
            [
36
                'success' => true,
37
                'html'    => $this->container->get('victoire_templating')->render(
38
                    'VictoireTemplateBundle:Template:index.html.twig',
39
                    ['templates' => $templates]
40
                ),
41
            ]
42
        );
43
    }
44
45
    /**
46
     * list of all templates.
47
     *
48
     * @param Template $template The template
49
     *
50
     * @Route("/show/{slug}", name="victoire_template_show")
51
     * @ParamConverter("template", class="VictoireTemplateBundle:Template", options={"mapping": {"slug": "slug"}})
52
     *
53
     * @return Response
54
     */
55
    public function showAction(Template $template)
56
    {
57
        //add the view to twig
58
        $this->get('twig')->addGlobal('view', $template);
59
        $template->setReference(new ViewReference($template->getId()));
60
        $event = new TemplateMenuContextualEvent($template);
61
62
        //TODO : il serait bon de faire des constantes pour les noms d'évents
63
        $eventName = 'victoire_core.'.Template::TYPE.'_menu.contextual';
64
65
        $this->get('event_dispatcher')->dispatch($eventName, $event);
66
67
        //the victoire templating
68
        $victoireTemplating = $this->container->get('victoire_templating');
69
        $layout = 'AppBundle:Layout:'.$template->getLayout().'.html.twig';
70
71
        $parameters = [
72
            'view'   => $template,
73
            'id'     => $template->getId(),
74
            'locale' => $template->getLocale(),
75
        ];
76
77
        $this->get('victoire_widget_map.builder')->build($template);
78
        $this->get('victoire_widget_map.widget_data_warmer')->warm(
79
            $this->get('doctrine.orm.entity_manager'),
80
            $template
81
        );
82
83
        $this->container->get('victoire_core.current_view')->setCurrentView($template);
84
85
        //create the response
86
        $response = $victoireTemplating->renderResponse(
87
            $layout,
88
            $parameters
89
        );
90
91
        return $response;
92
    }
93
94
    /**
95
     * create a new Template.
96
     *
97
     * @return JsonResponse
98
     * @Route("/new", name="victoire_template_new")
99
     * @Configuration\Template()
100
     */
101
    public function newAction()
102
    {
103
        $em = $this->getDoctrine()->getManager();
104
        $template = new Template();
105
        $form = $this->container->get('form.factory')->create($this->getNewTemplateType(), $template); //@todo utiliser un service
106
107
        $form->handleRequest($this->get('request'));
108 View Code Duplication
        if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
            $em->persist($template);
110
            $em->flush();
111
112
            return new JsonResponse([
113
                'success'  => true,
114
                'url'      => $this->generateUrl('victoire_template_show', ['slug' => $template->getSlug()]),
115
            ]);
116
        }
117
118
        return new JsonResponse(
119
            [
120
                'success' => true,
121
                'html'    => $this->container->get('victoire_templating')->render(
122
                    'VictoireTemplateBundle:Template:new.html.twig',
123
                    ['form' => $form->createView()]
124
                ),
125
            ]
126
        );
127
    }
128
129
    /**
130
     * define settings of the template.
131
     *
132
     * @param Template $template
133
     *
134
     * @return JsonResponse
135
     * @Route("/{slug}/parametres", name="victoire_template_settings")
136
     * @ParamConverter("template", class="VictoireTemplateBundle:Template", options={"mapping": {"slug": "slug"}})
137
     */
138
    public function settingsAction(Request $request, $template)
139
    {
140
        $em = $this->getDoctrine()->getManager();
141
142
        $form = $this->createForm($this->getNewTemplateType(), $template);
143
        $form->handleRequest($request);
144 View Code Duplication
        if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
145
            $em->persist($template);
146
            $em->flush();
147
148
            return new JsonResponse(
149
                    [
150
                        'success' => true,
151
                        'url'     => $this->generateUrl('victoire_template_show', ['slug' => $template->getSlug()]),
152
                    ]
153
                );
154
        }
155
156
        return new JsonResponse(
157
            [
158
                'success' => true,
159
                'html'    => $this->container->get('victoire_templating')->render(
160
                    'VictoireTemplateBundle:Template:settings.html.twig',
161
                    ['template' => $template, 'form' => $form->createView()]
162
                ),
163
            ]
164
        );
165
    }
166
167
    /**
168
     * edit a Template.
169
     *
170
     * @param Template $template The Template to edit
171
     *
172
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
173
     * @Route("/edit/{slug}", name="victoire_template_edit")
174
     * @Configuration\Template()
175
     * @ParamConverter("template", class="VictoireTemplateBundle:Template")
176
     */
177
    public function editAction(Template $template)
178
    {
179
        $em = $this->getDoctrine()->getManager();
180
        $form = $this->container->get('form.factory')->create($this->getNewTemplateType(), $template);
181
182
        $form->handleRequest($this->get('request'));
183
        if ($form->isValid()) {
184
            $em->persist($template);
185
            $em->flush();
186
187
            return $this->redirect($this->generateUrl('victoire_template_show', ['slug' => $template->getSlug()]));
188
        }
189
190
        return $this->redirect($this->generateUrl('victoire_template_settings', ['slug' => $template->getSlug()]));
191
    }
192
193
    /**
194
     * get "new" Template Type.
195
     *
196
     * @return string
197
     */
198
    protected function getNewTemplateType()
199
    {
200
        return 'victoire_template_type';
201
    }
202
}
203