Completed
Pull Request — master (#2)
by Andrew
02:56
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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
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
12
use AppBundle\Entity\Comment;
13
use AppBundle\Entity\Estate;
14
use AppBundle\Entity\User;
15
use AppBundle\Form\CommentType;
16
use AppBundle\Form\SearchType;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
25
class SiteController extends Controller
26
{
27
    /**
28
     * @Route("/", name="homepage")
29
     */
30
    public function indexAction(Request $request)
31
    {
32
        $em = $this->getDoctrine()->getManager();
33
        $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles();
34
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
35
    }
36
37
    /**
38
     * @Route("/menu", name="menu")
39
     */
40 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...
41
    {
42
        $em = $this->getDoctrine()->getManager();
43
        $categoryEntity = $em->getRepository('AppBundle\Entity\Category');
44
        $categories = $categoryEntity->childrenHierarchy();
45
        return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]);
46
    }
47
48
    /**
49
     * @Route("/show_category/{slug}", name="show_category")
50
     */
51
    public function showCategoryAction(Request $request, $slug)
52
    {
53
        $em = $this->getDoctrine()->getManager();
54
        $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($slug);
55
56
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
57
    }
58
59
    /**
60
     * @Route("/show_estate/{slug}", name="show_estate")
61
     */
62
    public function showEstateAction(Request $request, $slug)
63
    {
64
        $em = $this->getDoctrine()->getManager();
65
        $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug);
66
        // comment form
67
        $comment = new Comment();
68
        $commentForm = $this->createForm(CommentType::class, $comment, [
69
            'method' => 'POST',
70
        ])
71
            ->add('addComment', SubmitType::class, ['label' => 'common.save',
72
                'attr' => ['class' => 'btn btn-primary']
73
            ]);
74
            $commentForm->handleRequest($request);
75
            if ($commentForm->isSubmitted() && $commentForm->isValid()) {
76
                $comment->setEstate($estate);
77
                if ($this->getUser()->hasRole('ROLE_ADMIN')) {
78
                    $comment->setEnabled(true);
79
                } else {
80
                    $comment->setEnabled(false);
81
                }
82
                $em = $this->getDoctrine()->getManager();
83
                $em->persist($comment);
84
                $em->flush();
85
                $this->get('session')->getFlashBag()->add('success', 'site.flush_comment');
86
                return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
87
            }
88
        return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate,
89
            'commentForm' => $commentForm->createView()));
90
    }
91
92
    /**
93
     * @Route("/search", name="site_search")
94
     * */
95
    public function searchAction(Request $request)
96
    {
97
        $finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories();
98
        $searchForm = $this->createForm(SearchType::class, null, array(
99
            'action' => $this->generateUrl('site_search'),
100
            'categories_choices' => $finalCategories
101
        ))
102
            ->add('search', SubmitType::class, ['label' => 'Искать',
103
                'attr' => ['class' => 'btn btn-default']
104
            ]);
105
106
        $searchForm->handleRequest($request);
107
        if ($searchForm->isValid() && $searchForm->isSubmitted()) {
108
            $estates = $this->get('app.search')->searchEstate($searchForm->getData());
109
            // 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...
110
            return $this->render('AppBundle:site:index.html.twig', array('estates' => $estates));
111
        }
112
113
        return $this->render('@App/site/search.html.twig', array(
114
            'form' => $searchForm->createView(),
115
        ));
116
    }
117
118
    /**
119
     * @Route("/add_favorites/{estate}/{user}", name = "add_estate_to_favorites")
120
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
121
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
122
     */
123 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...
124
    {
125
        $em = $this->getDoctrine()->getManager();
126
        if (!$user->hasEstate($estate)) {
127
            $user->addEstate($estate);
128
            $em->persist($user);
129
            $em->flush();
130
        }
131
132
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
133
    }
134
135
    /**
136
     * @Route("/delete_favorites/{estate}/{user}", name = "delete_estate_from_favorites")
137
     * @ParamConverter("estate", class="AppBundle\Entity\Estate", options={"mapping": {"estate": "slug"}})
138
     * @ParamConverter("user", class="AppBundle\Entity\User", options={"mapping": {"user": "id"}})
139
     */
140 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...
141
    {
142
        $em = $this->getDoctrine()->getManager();
143
        if ($user->hasEstate($estate)) {
144
            $user->removeEstate($estate);
145
            $em->persist($user);
146
            $em->flush();
147
        }
148
149
        return $this->redirectToRoute('show_estate', array('slug' => $estate->getSlug()));
150
    }
151
}