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

AdminController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

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 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\Estate;
6
use AppBundle\Entity\File;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\Request;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use AppBundle\Form\EstateType;
12
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
14
use Symfony\Component\HttpFoundation\Response;
15
16
17
/**
18
 * @Route("/admin")
19
 */
20
class AdminController extends Controller
21
{
22
    /**
23
     * @Route("/", name="admin_index")
24
     */
25
    public function indexAction(Request $request)
26
    {
27
        // replace this example code with whatever you need
28
        return $this->render('AppBundle::admin/index.html.twig', array(
29
            'base_dir' => realpath($this->getParameter('kernel.root_dir') . '/..'),
30
        ));
31
    }
32
33
    /**
34
     * @Route("/estates", name="admin_estates")
35
     * @Method("GET")
36
     */
37
    public function estatesAction(Request $request)
38
    {
39
        $estates = $this->getDoctrine()->getRepository('AppBundle:Estate')->findAll();
40
        $paginator = $this->get('knp_paginator');
41
        $pagination = $paginator->paginate(
42
            $estates,
43
            $request->query->getInt('page', 1),
44
            20
45
        );
46
47
        return $this->render('@App/admin/estates.html.twig', array('pagination' => $pagination));
48
    }
49
50
    /**
51
     * @Route("/estate/show/{slug}", name="admin_estate_show")
52
     * @Method("GET")
53
     */
54
    public function estateShowAction(Estate $estate, $slug)
0 ignored issues
show
Unused Code introduced by
The parameter $estate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $estate = $this->getDoctrine()->getRepository('AppBundle:Estate')->findOneBy(array('slug' => $slug));
57
        $deleteForm = $this->createDeleteForm($estate);
58
        return $this->render('@App/admin/show_estate.html.twig', array(
59
            'estate'        => $estate,
60
            'delete_form' => $deleteForm->createView(),
61
        ));
62
    }
63
64
    /**
65
     * @Route("/estate/edit/{slug}", name="admin_estate_edit")
66
     * @Method({"GET", "POST"})
67
     */
68
    public function estateEditAction(Estate $estate, Request $request)
69
    {
70
        $entityManager = $this->getDoctrine()->getManager();
71
        $editForm = $this->createForm(EstateType::class, $estate);
72
        $deleteForm = $this->createDeleteForm($estate);
73
        $editForm->handleRequest($request);
74
        if ($editForm->isSubmitted() && $editForm->isValid()) {
75
            $estate = $this->get('app.file_manager')->fileManager($estate);
76
            $entityManager->persist($estate);
77
            $entityManager->flush();
78
            return $this->redirectToRoute('admin_estate_show', array('slug' => $estate->getSlug()));
79
        }
80
        return $this->render('@App/admin/edit_estate.html.twig', array(
81
            'estate'        => $estate,
82
            'edit_form'   => $editForm->createView(),
83
            'delete_form' => $deleteForm->createView(),
84
        ));
85
    }
86
87
    /**
88
     * @Route("/estate/delete/{slug}", name="admin_estate_delete")
89
     * @Method("DELETE")
90
     */
91
    public function estateDeleteAction(Request $request, Estate $estate)
92
    {
93
        $form = $this->createDeleteForm($estate);
94
        $form->handleRequest($request);
95
96
        if ($form->isSubmitted() && $form->isValid()) {
97
            $entityManager = $this->getDoctrine()->getManager();
98
99
            $entityManager->remove($estate);
100
            $entityManager->flush();
101
        }
102
        return $this->redirectToRoute('admin_index');
103
    }
104
105
    private function createDeleteForm(Estate $estate)
106
    {
107
        return $this->createFormBuilder()
108
            ->setAction($this->generateUrl('admin_estate_delete', array('slug' => $estate->getSlug())))
109
            ->setMethod('DELETE')
110
            ->getForm()
111
            ;
112
    }
113
114
115
    /**
116
     * @Route("/estate/new", name="admin_estate_new")
117
     * @Method({"GET", "POST"})
118
     */
119
    public function newEstateAction(Request $request)
120
    {
121
        $entityManager = $this->getDoctrine()->getManager();
122
        $estate = new Estate();
123
        //$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...
124
        $form = $this->createForm(EstateType::class, $estate)->add('saveAndCreateNew', SubmitType::class);
125
        $form->handleRequest($request);
126
        if ($form->isSubmitted() && $form->isValid()) {
127
            $estate = $this->get('app.file_manager')->fileManager($estate);
128
            $entityManager->persist($estate);
129
            $entityManager->flush();
130
            $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...
131
                ? 'admin_estate_new'
132
                : 'admin_index';
133
            return $this->redirectToRoute($nextAction);
134
        }
135
        return $this->render('@App/admin/new_estate.html.twig', array(
136
            'estate' => $estate,
137
            'form' => $form->createView(),
138
        ));
139
    }
140
}
141