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

MassActionController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
c 3
b 0
f 1
lcom 1
cbo 7
dl 0
loc 45
ccs 0
cts 30
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B massAction() 0 38 4
1
<?php
2
3
namespace AppBundle\Controller\Admin;
4
5
use AppBundle\Entity\ModuleUser;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class MassActionController extends Controller
13
{
14
    /**
15
     * @Route("/admin/mass-action", name="mass_action")
16
     * @Template("@App/admin/massAction/massAction.html.twig")
17
     */
18
    public function massAction(Request $request)
19
    {
20
        $em = $this->getDoctrine()->getManager();
21
22
        $modules = $em->getRepository('AppBundle:Module')
23
            ->findAll();
24
25
        if ($module = $request->get('module')) {
26
            $users = $em->getRepository('AppBundle:User')
27
                ->findChoiceModules($module);
28
29
            return new Response(json_encode(['users' => $users]));
30
        }
31
32
        if ($choice = $request->get('users_choice')) {
33
            $hiddenModule = $request->get('moduleHidden');
34
            $moduleHidden = $em->getRepository('AppBundle:Module')
35
                ->find($hiddenModule);
36
37
            for ($i = 0; $i < count($choice); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
38
                $moduleUser = new ModuleUser();
39
                $moduleUser->setModule($moduleHidden);
40
                $user = $em->getRepository('AppBundle:User')
41
                    ->find($choice[$i]);
42
                $moduleUser->setUser($user);
43
                $user->removeChosenModule($hiddenModule);
44
                $em->persist($moduleUser);
45
            }
46
47
            $em->flush();
48
49
            return $this->redirectToRoute('mass_action');
50
        }
51
52
        return [
53
            'modules' => $modules
54
        ];
55
    }
56
}
57