Completed
Pull Request — master (#2)
by Andrew
03:33
created

SiteController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 174
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 21
Bugs 1 Features 12
Metric Value
wmc 17
c 21
b 1
f 12
lcom 1
cbo 4
dl 29
loc 174
ccs 0
cts 127
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 8 1
A menuAction() 7 7 1
A showCategoryAction() 0 14 2
B showEstateAction() 0 36 5
A searchAction() 0 22 3
A addEstateToFavoritesAction() 11 11 2
A deleteEstateFromFavoritesAction() 11 11 2
B pdfEstateAction() 0 25 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
 * Created by PhpStorm.
4
 * User: kate
5
 * Date: 17.02.16
6
 * Time: 9:30
7
 */
8
9
namespace AppBundle\Controller;
10
11
12
use AppBundle\Entity\Comment;
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\Form\Extension\Core\Type\SubmitType;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
25
26
class SiteController extends Controller
27
{
28
    /**
29
     * @Route("/", name="homepage")
30
     */
31
    public function indexAction(Request $request)
32
    {
33
        $em = $this->getDoctrine()->getManager();
34
        $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles();
35
        $breadcrumbs = $this->get("white_october_breadcrumbs");
36
        $breadcrumbs->addItem("Homepage");
37
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
38
    }
39
40
    /**
41
     * @Route("/menu", name="menu")
42
     */
43 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...
44
    {
45
        $em = $this->getDoctrine()->getManager();
46
        $categoryEntity = $em->getRepository('AppBundle\Entity\Category');
47
        $categories = $categoryEntity->childrenHierarchy();
48
        return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]);
49
    }
50
51
    /**
52
     * @Route("/show_category/{slug}", name="show_category")
53
     * @ParamConverter("category", class="AppBundle\Entity\Category", options={"mapping": {"slug": "title"}})
54
     */
55
    public function showCategoryAction(Request $request, Category $category)
56
    {
57
        $em = $this->getDoctrine()->getManager();
58
        $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($category->getTitle());
59
        $breadcrumbs = $this->get("white_october_breadcrumbs");
60
        $node = $category;
61
        while ($node) {
62
            $breadcrumbs->prependItem($node->getTitle());
63
            $node = $node->getParent();
64
        }
65
        $breadcrumbs->prependItem("Homepage");
66
67
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
68
    }
69
70
    /**
71
     * @Route("/show_estate/{slug}", name="show_estate")
72
     */
73
    public function showEstateAction(Request $request, $slug)
74
    {
75
        $em = $this->getDoctrine()->getManager();
76
        $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug);
77
        $breadcrumbs = $this->get("white_october_breadcrumbs");
78
        $node = $estate->getCategory();
79
        $breadcrumbs->prependItem($estate->getTitle());
80
        while ($node) {
81
            $breadcrumbs->prependItem($node->getTitle());
82
            $node = $node->getParent();
83
        }
84
        $breadcrumbs->prependItem("Homepage");
85
        $comment = new Comment();
86
        $commentForm = $this->createForm(CommentType::class, $comment, [
87
            'method' => 'POST',
88
        ])
89
            ->add('addComment', SubmitType::class, ['label' => 'common.save',
90
                'attr' => ['class' => 'btn btn-primary']
91
            ]);
92
            $commentForm->handleRequest($request);
93
            if ($commentForm->isSubmitted() && $commentForm->isValid()) {
94
                $comment->setEstate($estate);
95
                if ($this->getUser()->hasRole('ROLE_ADMIN')) {
96
                    $comment->setEnabled(true);
97
                } else {
98
                    $comment->setEnabled(false);
99
                }
100
                $em = $this->getDoctrine()->getManager();
101
                $em->persist($comment);
102
                $em->flush();
103
                $this->get('session')->getFlashBag()->add('success', 'site.flush_comment');
104
                return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
105
            }
106
        return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate,
107
            'commentForm' => $commentForm->createView()));
108
    }
109
110
    /**
111
     * @Route("/search", name="site_search")
112
     * */
113
    public function searchAction(Request $request)
114
    {
115
        $finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories();
116
        $searchForm = $this->createForm(SearchType::class, null, array(
117
            'action' => $this->generateUrl('site_search'),
118
            'categories_choices' => $finalCategories
119
        ))
120
            ->add('search', SubmitType::class, ['label' => 'Искать',
121
                'attr' => ['class' => 'btn btn-default']
122
            ]);
123
124
        $searchForm->handleRequest($request);
125
        if ($searchForm->isValid() && $searchForm->isSubmitted()) {
126
            $estates = $this->get('app.search')->searchEstate($searchForm->getData());
127
            // return new Response();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
            return $this->render('AppBundle:site:index.html.twig', array('estates' => $estates));
129
        }
130
131
        return $this->render('@App/site/search.html.twig', array(
132
            'form' => $searchForm->createView(),
133
        ));
134
    }
135
136
    /**
137
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites")
138
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
139
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
140
     */
141 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...
142
    {
143
        $em = $this->getDoctrine()->getManager();
144
        if (!$user->hasEstate($estate)) {
145
            $user->addEstate($estate);
146
            $em->persist($user);
147
            $em->flush();
148
        }
149
150
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
151
    }
152
153
    /**
154
     * @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites")
155
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
156
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
157
     */
158 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...
159
    {
160
        $em = $this->getDoctrine()->getManager();
161
        if ($user->hasEstate($estate)) {
162
            $user->removeEstate($estate);
163
            $em->persist($user);
164
            $em->flush();
165
        }
166
167
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
168
    }
169
170
    /**
171
     * @Route("/pdf/{estate}", name = "pdf_estate")
172
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
173
     */
174
    public function pdfEstateAction(Estate $estate, Request $request)
175
    {
176
        $comment = new Comment();
177
        $commentForm = $this->createForm(CommentType::class, $comment, [
178
            'method' => 'POST',
179
        ])
180
            ->add('addComment', SubmitType::class, ['label' => 'common.save',
181
                'attr' => ['class' => 'btn btn-primary']
182
            ]);
183
        $html = $this->renderView('@App/site/pdf.html.twig', array(
184
            'estate'  => $estate,
185
            'commentForm' => $commentForm->createView()
186
        ));
187
188
        return new Response(
189
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
190
                'images' => true,
191
            )),
192
            200,
193
            array(
194
                'Content-Type'          => 'application/pdf',
195
                'Content-Disposition'   => 'attachment; filename="file.pdf"'
196
            )
197
        );
198
    }
199
}