Completed
Push — sf2.7 ( 18de34...b252b6 )
by Laurent
18:26
created

CompanyController::editAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 14
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Controller\Settings;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use AppBundle\Entity\Company;
11
use AppBundle\Form\Type\CompanyType;
12
13
/**
14
 * Company controller.
15
 *
16
 * @Route("/admin/settings/company")
17
 */
18 View Code Duplication
class CompanyController extends Controller
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...
19
{
20
    /**
21
     * Lists all Company entities.
22
     *
23
     * @Route("/", name="admin_company")
24
     * @Method("GET")
25
     * @Template()
26
     */
27
    public function indexAction()
28
    {
29
        $em = $this->getDoctrine()->getManager();
30
        $entities = $em->getRepository('AppBundle:Company')->findAll();
31
        
32
        return array(
33
            'entities'  => $entities,
34
            'ctEntity' => count($entities),
35
        );
36
    }
37
38
    /**
39
     * Finds and displays a Company entity.
40
     *
41
     * @Route("/{id}/show", name="admin_company_show", requirements={"id"="\d+"})
42
     * @Method("GET")
43
     * @Template()
44
     */
45
    public function showAction(Company $company)
46
    {
47
        $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete');
48
49
        return array(
50
            'company' => $company,
51
            'delete_form' => $deleteForm->createView(),
52
        );
53
    }
54
55
    /**
56
     * Displays a form to create a new Company entity.
57
     *
58
     * @Route("/new", name="admin_company_new")
59
     * @Method("GET")
60
     * @Template()
61
     */
62
    public function newAction()
63
    {
64
        $company = new Company();
65
        $form = $this->createForm(new CompanyType(), $company);
66
67
        return array(
68
            'company' => $company,
69
            'form'   => $form->createView(),
70
        );
71
    }
72
73
    /**
74
     * Creates a new Company entity.
75
     *
76
     * @Route("/create", name="admin_company_create")
77
     * @Method("POST")
78
     * @Template("AppBundle:Settings/Company:new.html.twig")
79
     */
80
    public function createAction(Request $request)
81
    {
82
        $company = new Company();
83
        $form = $this->createForm(new CompanyType(), $company);
84
        if ($form->handleRequest($request)->isValid()) {
85
            $em = $this->getDoctrine()->getManager();
86
            $em->persist($company);
87
            $em->flush();
88
89
            return $this->redirect($this->generateUrl('admin_company_show', array('id' => $company->getId())));
90
        }
91
92
        return array(
93
            'company' => $company,
94
            'form'   => $form->createView(),
95
        );
96
    }
97
98
    /**
99
     * Displays a form to edit an existing Company entity.
100
     *
101
     * @Route("/{id}/edit", name="admin_company_edit", requirements={"id"="\d+"})
102
     * @Method("GET")
103
     * @Template()
104
     */
105
    public function editAction(Company $company)
106
    {
107
        $editForm = $this->createForm(new CompanyType(), $company, array(
108
            'action' => $this->generateUrl('admin_company_update', array('id' => $company->getId())),
109
            'method' => 'PUT',
110
        ));
111
        $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete');
112
113
        return array(
114
            'company' => $company,
115
            'edit_form'   => $editForm->createView(),
116
            'delete_form' => $deleteForm->createView(),
117
        );
118
    }
119
120
    /**
121
     * Edits an existing Company entity.
122
     *
123
     * @Route("/{id}/update", name="admin_company_update", requirements={"id"="\d+"})
124
     * @Method("PUT")
125
     * @Template("AppBundle:Company:edit.html.twig")
126
     */
127
    public function updateAction(Company $company, Request $request)
128
    {
129
        $editForm = $this->createForm(new CompanyType(), $company, array(
130
            'action' => $this->generateUrl('admin_company_update', array('id' => $company->getId())),
131
            'method' => 'PUT',
132
        ));
133
        if ($editForm->handleRequest($request)->isValid()) {
134
            $this->getDoctrine()->getManager()->flush();
135
136
            return $this->redirect($this->generateUrl('admin_company_edit', array('id' => $company->getId())));
137
        }
138
        $deleteForm = $this->createDeleteForm($company->getId(), 'admin_company_delete');
139
140
        return array(
141
            'company' => $company,
142
            'edit_form'   => $editForm->createView(),
143
            'delete_form' => $deleteForm->createView(),
144
        );
145
    }
146
147
    /**
148
     * Deletes a Company entity.
149
     *
150
     * @Route("/{id}/delete", name="admin_company_delete", requirements={"id"="\d+"})
151
     * @Method("DELETE")
152
     */
153
    public function deleteAction(Company $company, Request $request)
154
    {
155
        $form = $this->createDeleteForm($company->getId(), 'admin_company_delete');
156
        if ($form->handleRequest($request)->isValid()) {
157
            $em = $this->getDoctrine()->getManager();
158
            $em->remove($company);
159
            $em->flush();
160
        }
161
162
        return $this->redirect($this->generateUrl('admin_company'));
163
    }
164
165
    /**
166
     * Create Delete form
167
     *
168
     * @param integer                       $id
169
     * @param string                        $route
170
     * @return \Symfony\Component\Form\Form
171
     */
172
    protected function createDeleteForm($id, $route)
173
    {
174
        return $this->createFormBuilder(null, array('attr' => array('id' => 'delete')))
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Symfony\Component\Form\FormConfigBuilder. Did you maybe mean getFormConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
175
            ->setAction($this->generateUrl($route, array('id' => $id)))
176
            ->setMethod('DELETE')
177
            ->getForm()
178
        ;
179
    }
180
181
}
182