Completed
Pull Request — master (#2)
by Andrew
02:57
created

SiteController::commentNewAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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