Completed
Push — master ( b2b032...858bee )
by Pavel
09:16 queued 06:10
created

ModuleController::editModuleAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 19
loc 19
ccs 9
cts 11
cp 0.8182
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 2.024
1
<?php
2
3
namespace AppBundle\Controller\Admin;
4
5
use AppBundle\Entity\Module;
6
use AppBundle\Form\ModuleType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
class ModuleController extends Controller
15
{
16
    /**
17
     * @Route("/admin/module/new", name="create_module")
18
     * @Template("@App/admin/module/createModule.html.twig")
19
     */
20 1 View Code Duplication
    public function createModuleAction(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...
21
    {
22 1
        $em = $this->getDoctrine()->getManager();
23
24 1
        $module = new Module();
25 1
        $form = $this->createForm(ModuleType::class, $module);
26
27 1
        $form->handleRequest($request);
28
29 1
        if ($form->isValid()) {
30
            $em->persist($module);
31
            $em->flush();
32
33
            return $this->redirectToRoute('create_question', array('idModule' => $module->getId()));
34
35
        }
36
37 1
        return ['form' => $form->createView()];
38
    }
39
40
    /**
41
     * @Route("/admin/module/edit/{id}", name="edit_module")
42
     * @Template("@App/admin/module/editModule.html.twig")
43
     */
44 1 View Code Duplication
    public function editModuleAction(Request $request, $id)
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...
45
    {
46 1
        $em = $this->getDoctrine()->getManager();
47
48 1
        $module = $em->getRepository('AppBundle:Module')
49 1
            ->find($id);
50 1
        $form = $this->createForm(ModuleType::class, $module);
51
52 1
        $form->handleRequest($request);
53
54 1
        if ($form->isValid()) {
55
            $em->flush();
56
57
            return $this->redirectToRoute('show_module');
58
        }
59
60 1
        return ['module' => $module,
61 1
                'form' => $form->createView()];
62
    }
63
64
    /**
65
     * @Route("/admin/module/remove/{id}", name="remove_module")
66
     * @Method("DELETE")
67
     */
68 1
    public function removeModuleAction($id)
69
    {
70 1
        $em = $this->getDoctrine()->getManager();
71
72 1
        $module = $em->getRepository('AppBundle:Module')
73 1
            ->find($id);
74 1
        $users = $em->getRepository('AppBundle:User')
75 1
            ->findChoiceModules($module->getId());
76 1
        foreach ($users as $item) {
77
                $item->removeChosenModule($module->getId());
78 1
        }
79
80 1
        $em->remove($module);
81 1
        $em->flush();
82
83 1
        return $this->redirectToRoute('show_module');
84
85
    }
86
87
    /**
88
     * @Route("/admin/module", name="show_module")
89
     * @Template("@App/admin/module/showmodule.html.twig")
90
     */
91 1
    public function showModuleAction()
92
    {
93 1
        $em = $this->getDoctrine()->getManager();
94
95 1
        $module = $em->getRepository('AppBundle:Module')
96 1
            ->findBy([],['createdAt' => 'DESC']);
97
98 1
        $form_delete = [];
99
100 1
        foreach ($module as $item) {
101 1
            $form_delete[$item->getId()] = $this->createFormDelete($item->getId())->createView();
102 1
        }
103
104
        return [
105 1
            'modules' => $module,
106
            'form_remove' => $form_delete
107 1
        ];
108
109
    }
110
111
    /**
112
     * @return \Symfony\Component\Form\Form
113
     */
114 1 View Code Duplication
    private function createFormDelete($id)
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...
115
    {
116 1
        return $this->createFormBuilder()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Form\FormConfigBuilder as the method add() does only exist in the following sub-classes of Symfony\Component\Form\FormConfigBuilder: Symfony\Component\Form\FormBuilder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
117 1
            ->setAction($this->generateUrl('remove_module', ['id' => $id]))
118 1
            ->setMethod('DELETE')
119 1
            ->add('submit', SubmitType::class, [
120 1
                'label' => ' ',
121
                'attr' => [
122 1
                    'class' => 'glyphicon glyphicon-remove btn-link',
123
                    'onclick' => 'return confirm("Are you sure?")'
124 1
                ]
125 1
            ])
126 1
            ->getForm();
127
    }
128
}
129