GroupController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %
Metric Value
dl 12
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Vivait\AuthBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\Request;
8
use Vivait\AuthBundle\Entity\Group;
9
use Doctrine\ORM\Query;
10
11
class GroupController extends Controller
12
{
13
	
14 View Code Duplication
	public function indexAction() {
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...
15
		################################################  SETTINGS  ################################################
16
		$repo = 'VivaitAuthBundle:Group';
17
		$twig = 'VivaitAuthBundle:Default:groups.html.twig';
18
		############################################################################################################
19
		$db = $this->getDoctrine()
20
			->getRepository($repo)
21
			->findAll();
22
23
		$params = array();
24
		return $this->render($twig, array('db' => $db, 'params' => $params));
25
	}
26
  
27
	public function editAction(Request $request) {
28
		################################################  SETTINGS  ################################################
29
		$name             = 'group';
30
		$repo             = 'VivaitAuthBundle:Group';
31
		$formtpl['title'] = 'Add/Edit ' . ucfirst($name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$formtpl was never initialized. Although not strictly required by PHP, it is generally a good practice to add $formtpl = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
32
		$obj              = new Group();
33
		$key              = $request->query->get('id', 0);
34
		$foreign_objs     = array(
35
			#	array(
36
			#		'repo'   => 'VivaBravoBundle:Product',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
37
			#		'key'    => $request->query->get('pid', 0),
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
38
			#		'method' => 'setProduct',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
39
			#		'name'   => 'product'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
40
		);
41
		############################################################################################################
42
43
44 View Code Duplication
		if(!$key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
			### CREATING A NEW OBJECT ###
46
47
			#if there are foreign objects that should be bound to this object, bind them all here
48
			foreach($foreign_objs as $fo) {
49
				$foreign_obj = $this->getDoctrine()
50
					->getRepository($fo['repo'])
51
					->find($fo['key']);
52
				if(!$foreign_obj) {
53
					$this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $fo['name']));
54
					return $this->redirect($request->query->get('parent', $request->request->get('parent', $request->headers->get('referer'))));
55
				}
56
				call_user_func(array($obj, $fo['method'], $foreign_obj));
57
			}
58
		} else {
59
			### EDITING AN EXISTING OBJECT ###
60
			$obj = $this->getDoctrine()
61
				->getRepository($repo)
62
				->find($key);
63
64
			if(!$obj) {
65
				$this->get('session')->getFlashBag()->add('error', sprintf("Could not find the %s", $name));
66
			}
67
		}
68
		
69
70
		##############################################  CREATE FORM  ###############################################
71
        	$form = $this->createFormBuilder($obj)
72
			->add('name',	'text',array('label' => 'Name'))
73
			->add('role',	'text',array('label' => 'Role'))
74
			->add('users',	'entity', array(
75
				'class' => 'VivaitAuthBundle:User',
76
				'property' => 'fullname',
77
				'multiple' => true,
78
				'required' => true,
79
				'label' => 'Users',
80
				'by_reference' => false											#USE THIS ALONG WITH A $owning->addInverse($this); IN THE addOwning() FUNCTION TO TRIGGER AN UPDATE WHEN ON INVERSE SIDE
81
			))
82
			->getForm();
83
		############################################################################################################
84
		
85 View Code Duplication
		if($request->isMethod('POST')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
86
			$form->bind($request);
87
			if($form->isValid()) {
88
				$em = $this->getDoctrine()->getManager();
89
				
90
				$em->persist($obj);
91
				$em->flush();
92
				$this->get('session')->getFlashBag()->add('success', sprintf('The %s has been %s successfully', $name, $key ? 'modified' : 'created'));
93
				return $this->render('VivaitBootstrapBundle:Default:redirect.html.twig', array('redirect' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')))));
94
			}
95
		}
96
		if(isset($form)) {
97
			$formtpl['form'] = $form->createView();
98
		}
99
		$formtpl['action']   = $this->generateUrl($this->container->get('request')->get('_route'),$request->query->all());
100
101
		return $this->render('VivaitBootstrapBundle:Default:form.html.twig', array(
102
			'form' => array_merge($formtpl, array('parent' => $request->query->get('parent', $request->request->get('parent', $request->headers->get('referer')))))));
103
	}
104
105 View Code Duplication
	public function deleteAction(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...
106
		################################################  SETTINGS  ################################################
107
		$name         = 'group';
108
		$repo         = 'VivaitAuthBundle:Group';
109
		$id           = $request->query->get('id', 0);
110
		$msg_notfound = "The $name could not be found";
111
		$msg_success  = "The $name has been removed";
112
		############################################################################################################
113
114
		$obj = $this->getDoctrine()
115
			->getRepository($repo)
116
			->find($id);
117
118
		if(!$obj) {
119
			$this->get('session')->getFlashBag()->add('error', $msg_notfound);
120
		} else {
121
			$em = $this->getDoctrine()->getManager();
122
			$em->remove($obj);
123
			$em->flush();
124
			$this->get('session')->getFlashBag()->add('success', $msg_success);
125
		}
126
127
		return $this->redirect($request->headers->get('referer'));
128
    }
129
130
}
131