Completed
Pull Request — master (#2)
by Kate
03:29
created

SiteController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 6
Bugs 0 Features 4
Metric Value
c 6
b 0
f 4
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
22
23
class SiteController extends Controller
24
{
25
    /**
26
     * @Route("/", name="homepage")
27
     */
28
    public function indexAction(Request $request)
29
    {
30
        $em = $this->getDoctrine()->getManager();
31
        $estates = $em->getRepository('AppBundle:Estate')->getEstateExclusiveWithFiles();
32
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
33
    }
34
35
    /**
36
     * @Route("/menu", name="menu")
37
     */
38
    public function menuAction(Request $request)
39
    {
40
        $em = $this->getDoctrine()->getManager();
41
        $categoryEntity = $em->getRepository('AppBundle\Entity\Category');
42
        $categories = $categoryEntity->childrenHierarchy();
43
        return $this->render("AppBundle::includes/menu.html.twig", ['links' => $categories]);
44
    }
45
46
    /**
47
     * @Route("/show_category/{slug}", name="show_category")
48
     */
49
    public function showCategoryAction(Request $request, $slug)
50
    {
51
        $em = $this->getDoctrine()->getManager();
52
        $estates = $em->getRepository('AppBundle\Entity\Estate')->getEstateFromCategory($slug);
53
54
        return $this->render("AppBundle::site/index.html.twig", array('estates' => $estates));
55
    }
56
57
    /**
58
     * @Route("/show_estate/{slug}", name="show_estate")
59
     */
60
    public function showEstateAction(Request $request, $slug)
61
    {
62
        $em = $this->getDoctrine()->getManager();
63
        $estate = $em->getRepository('AppBundle\Entity\Estate')->getEstateWithDistrictComment($slug);
64
        // comment form
65
        $comment = new Comment();
66
        $commentForm = $this->createForm(CommentType::class, $comment, [
67
            'method' => 'POST',
68
        ])
69
            ->add('addComment', SubmitType::class, ['label' => 'Save',
70
                'attr' => ['class' => 'btn btn-primary']
71
            ]);
72
        if ('POST' === $request->getMethod()) {
73
            $commentForm->handleRequest($request);
74
            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...
75
                if ($commentForm->isSubmitted() && $commentForm->isValid()) {
76
                    $comment->setEstate($estate[0]);
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[0]->getSlug()));
87
                }
88
            }
89
        }
90
        return $this->render('AppBundle:site:show_estate.html.twig', array('estate' => $estate[0],
91
            'commentForm' => $commentForm->createView()));
92
    }
93
}