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

AdminController::newEstateAction()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 5
Bugs 0 Features 5
Metric Value
c 5
b 0
f 5
dl 0
loc 31
ccs 0
cts 30
cp 0
rs 8.439
cc 6
eloc 24
nc 5
nop 1
crap 42
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 Gedmo\Uploadable\FileInfo\FileInfoArray;
14
15
16
/**
17
 * @Route("/admin")
18
 */
19
class AdminController extends Controller
20
{
21
    /**
22
     * @Route("/", name="admin_index")
23
     */
24
    public function indexAction(Request $request)
25
    {
26
        // replace this example code with whatever you need
27
        return $this->render('AppBundle::admin/index.html.twig', array(
28
            'base_dir' => realpath($this->getParameter('kernel.root_dir') . '/..'),
29
        ));
30
    }
31
32
    /**
33
     * @Route("/newestate", name="admin_new_estate")
34
     * @Method({"GET", "POST"})
35
     */
36
    public function newEstateAction(Request $request)
37
    {
38
        $entityManager = $this->getDoctrine()->getManager();
39
        $estate = new Estate();
40
        //$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...
41
        $form = $this->createForm(EstateType::class, $estate)->add('saveAndCreateNew', SubmitType::class);
42
        $form->handleRequest($request);
43
        if ($form->isSubmitted() && $form->isValid()) {
44
            $uploadableManager = $this->container->get('stof_doctrine_extensions.uploadable.manager');
45
            $files = $request->files->get('app_bundle_estate_type');
46
            if ($files['imageFile'][0] !== null) {
47
                foreach ($files['imageFile'] as $imageData) {
48
                    $image = new File();
49
                    $uploadableManager->markEntityToUpload($image, $imageData);
50
                    $image->setEstate($estate);
51
                    $estate->addFile($image);
52
                    $entityManager->persist($image);
53
                }
54
            }
55
            $entityManager->persist($estate);
56
            $entityManager->flush();
57
            $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...
58
                ? 'admin_new_estate'
59
                : 'admin_index';
60
            return $this->redirectToRoute($nextAction);
61
        }
62
        return $this->render('@App/admin/newestate.html.twig', array(
63
            'estate' => $estate,
64
            'form' => $form->createView(),
65
        ));
66
    }
67
}
68