Completed
Push — sf2.7 ( 758063...e98806 )
by Laurent
03:16
created

CompanyController::createDeleteForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * CompanyController controller de configuration de l'établissement.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version   since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller\Settings;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\Company;
23
use AppBundle\Form\Type\CompanyType;
24
25
/**
26
 * Company controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/settings/company")
31
 */
32 View Code Duplication
class CompanyController extends AbstractController
1 ignored issue
show
Duplication introduced by
This class 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...
33
{
34
    /**
35
     * Lists all Company entities.
36
     *
37
     * @Route("/", name="admin_company")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $entities = $em->getRepository('AppBundle:Company')->findAll();
45
        
46
        return array(
47
            'entities'  => $entities,
48
            'ctEntity' => count($entities),
49
        );
50
    }
51
52
    /**
53
     * Finds and displays a Company entity.
54
     *
55
     * @Route("/{id}/show", name="admin_company_show", requirements={"id"="\d+"})
56
     * @Method("GET")
57
     * @Template()
58
     */
59
    public function showAction(Company $company)
60
    {
61
        $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete');
62
63
        return array(
64
            'company' => $company,
65
            'delete_form' => $deleteForm->createView(),
66
        );
67
    }
68
69
    /**
70
     * Displays a form to create a new Company entity.
71
     *
72
     * @Route("/new", name="admin_company_new")
73
     * @Method("GET")
74
     * @Template()
75
     */
76
    public function newAction()
77
    {
78
        $company = new Company();
79
        $form = $this->createForm(new CompanyType(), $company);
80
81
        return array(
82
            'company' => $company,
83
            'form'   => $form->createView(),
84
        );
85
    }
86
87
    /**
88
     * Creates a new Company entity.
89
     *
90
     * @Route("/create", name="admin_company_create")
91
     * @Method("POST")
92
     * @Template("AppBundle:Settings/Company:new.html.twig")
93
     */
94
    public function createAction(Request $request)
95
    {
96
        $company = new Company();
97
        $form = $this->createForm(new CompanyType(), $company);
98
        if ($form->handleRequest($request)->isValid()) {
99
            $em = $this->getDoctrine()->getManager();
100
            $em->persist($company);
101
            $em->flush();
102
103
            return $this->redirectToRoute('admin_company_show', array('id' =>$company->getId()));
104
        }
105
106
        return array(
107
            'company' => $company,
108
            'form'   => $form->createView(),
109
        );
110
    }
111
112
    /**
113
     * Displays a form to edit an existing Company entity.
114
     *
115
     * @Route("/{id}/edit", name="admin_company_edit", requirements={"id"="\d+"})
116
     * @Method("GET")
117
     * @Template()
118
     */
119
    public function editAction(Company $company = null)
120
    {
121
        $editForm = $this->createForm(new CompanyType(), $company, array(
122
            'action' => $this->generateUrl('admin_company_update', array('id' => $company->getId())),
0 ignored issues
show
Bug introduced by
It seems like $company is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
123
            'method' => 'PUT',
124
        ));
125
        $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete');
126
127
        return array(
128
            'company' => $company,
129
            'edit_form'   => $editForm->createView(),
130
            'delete_form' => $deleteForm->createView(),
131
        );
132
    }
133
134
    /**
135
     * Edits an existing Company entity.
136
     *
137
     * @Route("/{id}/update", name="admin_company_update", requirements={"id"="\d+"})
138
     * @Method("PUT")
139
     * @Template("AppBundle:Company:edit.html.twig")
140
     */
141
    public function updateAction(Request $request, Company $company = null)
142
    {
143
        $editForm = $this->createForm(new CompanyType(), $company, array(
144
            'action' => $this->generateUrl('admin_company_update', array('id' => $company->getId())),
0 ignored issues
show
Bug introduced by
It seems like $company is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
145
            'method' => 'PUT',
146
        ));
147
        if ($editForm->handleRequest($request)->isValid()) {
148
            $this->getDoctrine()->getManager()->flush();
149
            $this->addFlash('info', 'gestock.settings.edit_ok');
150
151
            return $this->redirectToRoute('admin_company_show', array('id' => $company->getId()));
152
        }
153
        $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete');
154
155
        return array(
156
            'company' => $company,
157
            'edit_form'   => $editForm->createView(),
158
            'delete_form' => $deleteForm->createView(),
159
        );
160
    }
161
162
    /**
163
     * Deletes a Company entity.
164
     *
165
     * @Route("/{id}/delete", name="admin_company_delete", requirements={"id"="\d+"})
166
     * @Method("DELETE")
167
     */
168
    public function deleteAction(Request $request, Company $company = null)
169
    {
170
        $form = $this->createDeleteForm($company->getId(), 'admin_company_delete');
0 ignored issues
show
Bug introduced by
It seems like $company is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
171
        if ($form->handleRequest($request)->isValid()) {
172
            $em = $this->getDoctrine()->getManager();
173
            $em->remove($company);
174
            $em->flush();
175
        }
176
177
        return $this->redirectToRoute('admin_company');
178
    }
179
}
180