Completed
Pull Request — master (#2)
by Andrew
02:53
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
    public function indexAction(Request $request)
34
    {
35
        $em = $this->getDoctrine()->getManager();
36
        $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles();
37
        $breadcrumbs = $this->get("white_october_breadcrumbs");
38
        $breadcrumbs->addItem("Главная");
39
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
40
    }
41
42
    /**
43
     * @Route("/menu", name="menu")
44
     */
45 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...
46
    {
47
        $em = $this->getDoctrine()->getManager();
48
        $categoryEntity = $em->getRepository('AppBundle\Entity\Category');
49
        $categories = $categoryEntity->childrenHierarchy();
50
51
        return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]);
52
    }
53
54
    /**
55
     * @Route("/show_category/{slug}", name="show_category")
56
     * @ParamConverter("category", class="AppBundle\Entity\Category", options={"mapping": {"slug": "title"}})
57
     */
58 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...
59
    {
60
        $em = $this->getDoctrine()->getManager();
61
        $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($category->getTitle());
62
        $this->container->get('app.breadcrumps_maker')->makeBreadcrumps($category);
63
64
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
65
    }
66
67
    /**
68
     * @Route("/show_estate/{slug}", name="show_estate")
69
     */
70 View Code Duplication
    public function showEstateAction(Request $request, $slug)
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...
71
    {
72
        $em = $this->getDoctrine()->getManager();
73
        $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug);
74
        $this->container->get('app.breadcrumps_maker')->makeBreadcrumps($estate->getCategory(), $estate);
75
76
        return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate));
77
    }
78
79
    /**
80
     * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
81
     * @Route("/comment/{slug}/new", name = "comment_new")
82
     * @Method("POST")
83
     * @ParamConverter("estate", options={"mapping": {"slug": "slug"}})
84
     */
85
    public function commentNewAction(Estate $estate, Request $request)
86
    {
87
        $entityManager = $this->getDoctrine()->getManager();
88
        $comment = new Comment();
89
        $form = $this->createForm(CommentType::class, $comment);
90
        $form->handleRequest($request);
91
        if ($form->isSubmitted() && $form->isValid()) {
92
            if ($this->getUser()->hasRole('ROLE_ADMIN')) {
93
                $comment->setEnabled(true);
94
            } else {
95
                $comment->setEnabled(false);
96
            }
97
            $comment->setEstate($estate);
98
            $entityManager->persist($comment);
99
            $entityManager->flush();
100
            $this->get('session')->getFlashBag()->add('success', 'site.flush_comment');
101
            return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
102
        }
103
104
        return $this->render('@App/site/_comment_form.html.twig', array(
105
            'estate' => $estate,
106
            'form' => $form->createView(),
107
        ));
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(name="show_menu_item")
138
     */
139
    public function showMenuItemAction(Request $request)
140
    {
141
        $em = $this->getDoctrine()->getManager();
142
        $menuitems = $em->getRepository('AppBundle:MenuItem')->findAll();
143
        return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems));
144
    }
145
146
    /**
147
     * @Route("/description_menu/{id}", name="show_description_menu_item")
148
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
149
     */
150
    public function showDescriptionMenuItem(Request $request, MenuItem $menuItem)
151
    {
152
        return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem));
153
    }
154
155
    /**
156
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites")
157
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
158
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
159
     */
160 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...
161
    {
162
        $em = $this->getDoctrine()->getManager();
163
        if (!$user->hasEstate($estate)) {
164
            $user->addEstate($estate);
165
            $em->persist($user);
166
            $em->flush();
167
        }
168
169
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
170
    }
171
172
    /**
173
     * @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites")
174
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
175
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
176
     */
177 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...
178
    {
179
        $em = $this->getDoctrine()->getManager();
180
        if ($user->hasEstate($estate)) {
181
            $user->removeEstate($estate);
182
            $em->persist($user);
183
            $em->flush();
184
        }
185
186
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
187
    }
188
189
    /**
190
     * @Route("/pdf/{estate}", name = "pdf_estate")
191
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
192
     */
193
    public function pdfEstateAction(Estate $estate, Request $request)
194
    {
195
        $html = $this->renderView('@App/site/pdf.html.twig', array('estate' => $estate));
196
197
        return new Response(
198
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array('images' => true)), 200,
199
            array(
200
                'Content-Type' => 'application/pdf',
201
                'Content-Disposition' => 'attachment; filename="file.pdf"'
202
            )
203
        );
204
    }
205
206
207
}