Completed
Pull Request — master (#2)
by Kate
04:26
created

AdminDistrictController::newDistrictAction()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 20
ccs 0
cts 19
cp 0
rs 9.2
cc 4
eloc 15
nc 3
nop 1
crap 20
1
<?php
2
3
namespace AppBundle\Controller\Admin;
4
5
use AppBundle\Entity\District;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use AppBundle\Form\DistrictType;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
13
use Symfony\Component\HttpFoundation\Response;
14
15
16
/**
17
 * @Route("/admin")
18
 */
19
class AdminDistrictController extends Controller
20
{
21
    /**
22
     * @Route("/districts", name="admin_districts")
23
     * @Method("GET")
24
     */
25
    public function districtsAction(Request $request)
26
    {
27
        $districts = $this->getDoctrine()->getRepository('AppBundle:District')->findAll();
28
        return $this->render('@App/admin/district/districts.html.twig', array('districts' => $districts));
29
    }
30
31
    /**
32
     * @Route("/district/show/{slug}", name="admin_district_show")
33
     * @Method("GET")
34
     * @ParamConverter("district", options={"mapping": {"slug": "slug"}})
35
     */
36
    public function estateShowAction(District $district, Request $request)
37
    {
38
        $deleteForm = $this->createDeleteForm($district);
39
        return $this->render('@App/admin/district/show_district.html.twig', array(
40
            'district'        => $district,
41
            'delete_form' => $deleteForm->createView(),
42
        ));
43
    }
44
45
    /**
46
     * @Route("/district/new", name="admin_district_new")
47
     * @Method({"GET", "POST"})
48
     */
49
    public function newDistrictAction(Request $request)
50
    {
51
        $entityManager = $this->getDoctrine()->getManager();
52
        $district = new District();
53
        //$this->denyAccessUnlessGranted('create', $estate);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% 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...
54
        $form = $this->createForm(DistrictType::class, $district)->add('saveAndCreateNew', SubmitType::class);
55
        $form->handleRequest($request);
56
        if ($form->isSubmitted() && $form->isValid()) {
57
            $entityManager->persist($district);
58
            $entityManager->flush();
59
            $nextAction = $form->get('saveAndCreateNew')->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...
60
                ? 'admin_district_new'
61
                : 'admin_districts';
62
            return $this->redirectToRoute($nextAction);
63
        }
64
        return $this->render('@App/admin/district/new_district.html.twig', array(
65
            'district' => $district,
66
            'form' => $form->createView(),
67
        ));
68
    }
69
70
    /**
71
     * @Route("/district/edit/{slug}", name="admin_district_edit")
72
     * @Method({"GET", "POST"})
73
     * @ParamConverter("district", options={"mapping": {"slug": "slug"}})
74
     */
75
    public function estateEditAction(District $district, Request $request)
76
    {
77
        $entityManager = $this->getDoctrine()->getManager();
78
        $editForm = $this->createForm(DistrictType::class, $district);
79
        $deleteForm = $this->createDeleteForm($district);
80
        $editForm->handleRequest($request);
81
        if ($editForm->isSubmitted() && $editForm->isValid()) {
82
            $entityManager->persist($district);
83
            $entityManager->flush();
84
            return $this->redirectToRoute('admin_districts');
85
        }
86
        return $this->render('@App/admin/district/edit_district.html.twig', array(
87
            'district'        => $district,
88
            'edit_form'   => $editForm->createView(),
89
            'delete_form' => $deleteForm->createView(),
90
        ));
91
    }
92
93
    /**
94
     * @Route("/district/delete/{slug}", name="admin_district_delete")
95
     * @Method("DELETE")
96
     * @ParamConverter("district", options={"mapping": {"slug": "slug"}})
97
     */
98 View Code Duplication
    public function DistrictDeleteAction(Request $request, District $district)
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...
99
    {
100
        $form = $this->createDeleteForm($district);
101
        $form->handleRequest($request);
102
        if ($form->isSubmitted() && $form->isValid()) {
103
            $entityManager = $this->getDoctrine()->getManager();
104
105
            $entityManager->remove($district);
106
            $entityManager->flush();
107
        }
108
        return $this->redirectToRoute('admin_districts');
109
    }
110
111 View Code Duplication
    private function createDeleteForm(District $district)
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...
112
    {
113
        return $this->createFormBuilder()
114
            ->setAction($this->generateUrl('admin_district_delete', array('slug' => $district->getSlug())))
115
            ->setMethod('DELETE')
116
            ->getForm()
117
            ;
118
    }
119
}
120