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

SiteController::showCategoryAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
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\Form\CommentType;
15
use AppBundle\Form\SearchType;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
24
class SiteController extends Controller
25
{
26
    /**
27
     * @Route("/", name="homepage")
28
     */
29
    public function indexAction(Request $request)
30
    {
31
        $em = $this->getDoctrine()->getManager();
32
        $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles();
33
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
34
    }
35
36
    /**
37
     * @Route("/menu", name="menu")
38
     */
39 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...
40
    {
41
        $em = $this->getDoctrine()->getManager();
42
        $categoryEntity = $em->getRepository('AppBundle\Entity\Category');
43
        $categories = $categoryEntity->childrenHierarchy();
44
        return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]);
45
    }
46
47
    /**
48
     * @Route("/show_category/{slug}", name="show_category")
49
     */
50
    public function showCategoryAction(Request $request, $slug)
51
    {
52
        $em = $this->getDoctrine()->getManager();
53
        $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($slug);
54
55
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
56
    }
57
58
    /**
59
     * @Route("/show_estate/{slug}", name="show_estate")
60
     */
61
    public function showEstateAction(Request $request, $slug)
62
    {
63
        $em = $this->getDoctrine()->getManager();
64
        $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug);
65
        // comment form
66
        $comment = new Comment();
67
        $commentForm = $this->createForm(CommentType::class, $comment, [
68
            'method' => 'POST',
69
        ])
70
            ->add('addComment', SubmitType::class, ['label' => 'Save',
71
                'attr' => ['class' => 'btn btn-primary']
72
            ]);
73
        if ('POST' === $request->getMethod()) {
74
            $commentForm->handleRequest($request);
75
            if ($commentForm->get('addComment')->isClicked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
76
                if ($commentForm->isSubmitted() && $commentForm->isValid()) {
77
                    $comment->setEstate($estate[0]);
78
                    if ($this->getUser()->hasRole('ROLE_ADMIN')) {
79
                       $comment->setEnabled(true);
80
                        } else {
81
                            $comment->setEnabled(false);
82
                        }
83
                    $em = $this->getDoctrine()->getManager();
84
                    $em->persist($comment);
85
                    $em->flush();
86
                    $this->get('session')->getFlashBag()->add('success', 'site.flush_comment');
87
                    return $this->redirectToRoute('show_estate', array('slug' => $estate[0]->getSlug()));
88
                }
89
            }
90
        }
91
        return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate[0],
92
            'commentForm' => $commentForm->createView()));
93
    }
94
95
    /**
96
     * @Route("/search", name="site_search")
97
     * */
98
    public function searchAction(Request $request)
99
    {
100
        $finalCategories = $this->container->get('app.final_category_finder')->findFinalCategories();
101
        $searchForm = $this->createForm(SearchType::class, null,  array(
102
            'action' => $this->generateUrl('site_search'),
103
            'categories_choices' => $finalCategories
104
        ))
105
            ->add('search', SubmitType::class, ['label' => 'Искать',
106
                'attr' => ['class' => 'btn btn-default']
107
            ]);
108
109
            $searchForm->handleRequest($request);
110
            if ($searchForm->isValid() && $searchForm->isSubmitted()) {
111
                return $this->redirectToRoute('success_search');
112
            }
113
114
        return $this->render('@App/site/search.html.twig', array(
115
            'form' => $searchForm->createView(),
116
        ));
117
    }
118
119
    /**
120
     * @Route("/success_search", name="success_search")
121
     */
122
    public function successSearchAction(Request $request)
123
    {
124
        return new Response('ok');
125
    }
126
}