Completed
Pull Request — master (#2)
by Andrew
04:30
created

SiteController::menuAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 7
Ratio 87.5 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 7
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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("Главная");
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 1
            }
108
            $comment->setEstate($estate);
109
            $entityManager->persist($comment);
110 1
            $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 1
        ));
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 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
        $em = $this->getDoctrine()->getManager();
205
        if ($user->hasEstate($estate)) {
206
            $user->removeEstate($estate);
207
            $em->persist($user);
208
            $em->flush();
209
        }
210
211
        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
}