Completed
Pull Request — master (#2)
by Kate
02:44
created

SiteController::addEstateToFavoritesAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 11
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 6
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")estates
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
            $paginator = $this->get('knp_paginator');
140
            $pagination = $paginator->paginate(
141
                $estates,
142
                $request->query->getInt('page', 1),
143
                5
144
            );
145
            return $this->render('AppBundle:site:index.html.twig', array('pagination' => $pagination));
146
        }
147
148
        return $this->render('@App/site/search.html.twig', array(
149
            'form' => $searchForm->createView(),
150
        ));
151
    }
152
153
    /**
154
     * @Route(name="show_menu_item")
155
     */
156
    public function showMenuItemAction(Request $request)
157
    {
158
        $em = $this->getDoctrine()->getManager();
159
        $menuitems = $em->getRepository('AppBundle:MenuItem')->findAll();
160
        return $this->render('AppBundle:includes:menu_items.html.twig', array('items' => $menuitems));
161
    }
162
163
    /**
164
     * @Route("/description_menu/{id}", name="show_description_menu_item")
165
     * @ParamConverter("MenuItem", options={"mapping": {"id": "id"}})
166
     */
167
    public function showDescriptionMenuItem(Request $request, MenuItem $menuItem)
168
    {
169
        return $this->render('AppBundle:site:show_description_menu_item.html.twig', array('item' => $menuItem));
170
    }
171
172
    /**
173
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_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 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...
178
    {
179
        $em = $this->getDoctrine()->getManager();
180
        if (!$user->hasEstate($estate)) {
181
            $user->addEstate($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("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites")
191
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
192
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
193
     */
194 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...
195
    {
196
        $em = $this->getDoctrine()->getManager();
197
        if ($user->hasEstate($estate)) {
198
            $user->removeEstate($estate);
199
            $em->persist($user);
200
            $em->flush();
201
        }
202
203
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
204
    }
205
206
    /**
207
     * @Route("/pdf/{estate}", name = "pdf_estate")
208
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
209
     */
210
    public function pdfEstateAction(Estate $estate, Request $request)
211
    {
212
        $html = $this->renderView('@App/site/pdf.html.twig', array('estate' => $estate));
213
214
        return new Response(
215
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html, array('images' => true)), 200,
216
            array(
217
                'Content-Type' => 'application/pdf',
218
                'Content-Disposition' => 'attachment; filename="file.pdf"'
219
            )
220
        );
221
    }
222
223
224
}