Completed
Pull Request — master (#2)
by Andrew
05:50
created

SiteController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 206
Duplicated Lines 27.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 68.93%

Importance

Changes 31
Bugs 4 Features 15
Metric Value
wmc 19
c 31
b 4
f 15
lcom 1
cbo 6
dl 57
loc 206
ccs 71
cts 103
cp 0.6893
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A searchResultAction() 0 21 3
A indexAction() 14 14 1
A menuAction() 7 8 1
A showCategoryAction() 14 14 1
A showEstateAction() 0 8 1
A searchAction() 0 11 1
A showMenuItemAction() 0 6 1
A showDescriptionMenuItem() 0 4 1
A addEstateToFavoritesAction() 11 11 2
A pdfEstateAction() 0 12 1
A deleteEstateFromFavoritesAction() 11 11 2
B commentNewAction() 0 24 4

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
 * Created by PhpStorm.
4
 * User: kate
5
 * Date: 17.02.16
6
 * Time: 9:30
7
 */
8
9
namespace AppBundle\Controller;
10
11
use AppBundle\Entity\Comment;
12
use AppBundle\Entity\MenuItem;
13
use AppBundle\Entity\Estate;
14
use AppBundle\Entity\User;
15
use AppBundle\Entity\Category;
16
use AppBundle\Form\CommentType;
17
use AppBundle\Form\SearchType;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
20
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
21
use Symfony\Component\HttpFoundation\Request;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
24
use Symfony\Component\HttpFoundation\Response;
25
26
27
class SiteController extends Controller
28
{
29
    /**
30
     * @Route("/", name="homepage")
31
     */
32 1 View Code Duplication
    public function indexAction(Request $request)
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...
33
    {
34 1
        $em = $this->getDoctrine()->getManager();
35 1
        $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles();
36 1
        $paginator = $this->get('knp_paginator');
37 1
        $pagination = $paginator->paginate(
38 1
            $estates,
39 1
            $request->query->getInt('page', 1),
40
            5
41 1
        );
42 1
        $breadcrumbs = $this->get("white_october_breadcrumbs");
43 1
        $breadcrumbs->addItem("site.main");
44 1
        return $this->render("AppBundle::site/index.html.twig", array('pagination' => $pagination));
45
    }
46
47
    /**
48
     * @Route("/menu", name="menu")
49
     */
50 4 View Code Duplication
    public function menuAction(Request $request)
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...
51
    {
52 4
        $em = $this->getDoctrine()->getManager();
53 4
        $categoryEntity = $em->getRepository('AppBundle\Entity\Category');
54 4
        $categories = $categoryEntity->childrenHierarchy();
55
56 4
        return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]);
57
    }
58
59
    /**
60
     * @Route("/show_category/{slug}", name="show_category")
61
     * @ParamConverter("category", class="AppBundle\Entity\Category", options={"mapping": {"slug": "title"}})
62
     */
63 1 View Code Duplication
    public function showCategoryAction(Request $request, Category $category)
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...
64
    {
65 1
        $em = $this->getDoctrine()->getManager();
66 1
        $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($category->getTitle());
67 1
        $paginator = $this->get('knp_paginator');
68 1
        $pagination = $paginator->paginate(
69 1
            $estates,
70 1
            $request->query->getInt('page', 1),
71
            5
72 1
        );
73 1
        $this->container->get('app.breadcrumps_maker')->makeBreadcrumps($category);
74
75 1
        return $this->render("AppBundle::site/index.html.twig", array('pagination' => $pagination));
76 1
    }
77
78
    /**
79
     * @Route("/show_estate/{slug}", name="show_estate")
80
     */
81 1
    public function showEstateAction(Request $request, $slug)
82
    {
83 1
        $em = $this->getDoctrine()->getManager();
84 1
        $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug);
85 1
        $this->container->get('app.breadcrumps_maker')->makeBreadcrumps($estate->getCategory(), $estate);
86
87 1
        return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate));
88
    }
89
90
    /**
91
     * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
92
     * @Route("/comment/{slug}/new", name = "comment_new")
93
     * @Method("POST")estates
94
     * @ParamConverter("estate", options={"mapping": {"slug": "slug"}})
95
     */
96 1
    public function commentNewAction(Estate $estate, Request $request)
97
    {
98
        $entityManager = $this->getDoctrine()->getManager();
99
        $comment = new Comment();
100
        $form = $this->createForm(CommentType::class, $comment);
101
        $form->handleRequest($request);
102
        if ($form->isSubmitted() && $form->isValid()) {
103
            if ($this->getUser()->hasRole('ROLE_ADMIN')) {
104
                $comment->setEnabled(true);
105 1
            } else {
106
                $comment->setEnabled(false);
107
            }
108
            $comment->setEstate($estate);
109
            $entityManager->persist($comment);
110
            $entityManager->flush();
111
            $this->get('session')->getFlashBag()->add('success', 'site.flush_comment');
112
            return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
113
        }
114
115
        return $this->render('@App/site/_comment_form.html.twig', array(
116
            'estate' => $estate,
117
            'form' => $form->createView(),
118
        ));
119
    }
120
121
    /**
122
     * @Route("/search", name="site_search")
123
     * */
124 4
    public function searchAction(Request $request)
125
    {
126 4
        $finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories();
127 4
        $searchForm = $this->createForm(SearchType::class, null, array(
128 4
            'action' => $this->generateUrl('site_search_result'),
129 4
            'categories_choices' => $finalCategories));
130
131 4
        return $this->render('@App/site/search.html.twig', array(
132 4
            'form' => $searchForm->createView(),
133 4
        ));
134
    }
135
136
    /**
137
     * @Route("/search/result", name="site_search_result")
138
     * */
139
    public function searchResultAction(Request $request)
140
    {
141
        $finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories();
142
        $searchForm = $this->createForm(SearchType::class, null, array(
143
            'action' => $this->generateUrl('site_search_result'),
144
            'categories_choices' => $finalCategories));
145
146
        $searchForm->handleRequest($request);
147
        if ($searchForm->isValid() && $searchForm->isSubmitted()) {
148
            $estates = $this->get('app.search')->searchEstate($searchForm->getData());
149
            $paginator = $this->get('knp_paginator');
150
            $pagination = $paginator->paginate(
151
                $estates,
152
                $request->query->getInt('page', 1),
153
                5
154
            );
155
            return $this->render('AppBundle:site:index.html.twig', array('pagination' => $pagination));
156
        }
157
158
        return $this->redirectToRoute('homepage');
159
    }
160
161
    /**
162
     * @Route("/menu_item", name="show_menu_item")
163
     */
164 5
    public function showMenuItemAction(Request $request)
165
    {
166 5
        $em = $this->getDoctrine()->getManager();
167 5
        $menuitems = $em->getRepository('AppBundle:MenuItem')->findAll();
168 5
        return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems));
169
    }
170
171
    /**
172
     * @Route("/description_menu/{id}", name="show_description_menu_item")
173
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
174
     */
175 1
    public function showDescriptionMenuItem(Request $request, MenuItem $menuItem)
176
    {
177 1
        return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem));
178
    }
179
180
    /**
181
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites")
182
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
183
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
184
     */
185 1 View Code Duplication
    public function addEstateToFavoritesAction(Estate $estate, User $user, Request $request)
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...
186
    {
187 1
        $em = $this->getDoctrine()->getManager();
188 1
        if (!$user->hasEstate($estate)) {
189 1
            $user->addEstate($estate);
190 1
            $em->persist($user);
191 1
            $em->flush();
192 1
        }
193
194 1
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
195
    }
196
197
    /**
198
     * @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites")
199
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
200
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
201
     */
202 1 View Code Duplication
    public function deleteEstateFromFavoritesAction(Estate $estate, User $user, Request $request)
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...
203
    {
204 1
        $em = $this->getDoctrine()->getManager();
205 1
        if ($user->hasEstate($estate)) {
206 1
            $user->removeEstate($estate);
207 1
            $em->persist($user);
208 1
            $em->flush();
209 1
        }
210
211 1
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
212
    }
213
214
    /**
215
     * @Route("/pdf/{estate}", name = "pdf_estate")
216
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
217
     */
218 1
    public function pdfEstateAction(Estate $estate, Request $request)
219
    {
220 1
        $html = $this->renderView('@App/site/pdf.html.twig', array('estate' => $estate));
221
222 1
        return new Response(
223 1
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array('images' => true)), 200,
224
            array(
225 1
                'Content-Type' => 'application/pdf',
226
                'Content-Disposition' => 'attachment; filename="file.pdf"'
227 1
            )
228 1
        );
229
    }
230
231
232
}