Completed
Push — master ( 08e6ab...54f6d7 )
by Laurent
03:52
created

AbstractInstallController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 54
c 4
b 1
f 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C stepAction() 0 39 8
1
<?php
2
/**
3
 * AbstractInstallController controller d'installation de l'application GLSR.
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\Install;
16
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * AbstractInstall controller
22
 *
23
 * @category Controller
24
 */
25
abstract class AbstractInstallController extends Controller
26
{
27
    /**
28
     * Etape X de l'installation.
29
     * Fonction adaptable aux différentes étapes de l'installation.
30
     *
31
     * @param \Symfony\Component\HttpFoundation\Request $request    Requète du formulaire
32
     * @param string                                    $entity     Nom de l'Entity
33
     * @param string                                    $entityPath Namespace de l'Entity
34
     * @param string                                    $typePath   Namespace du Type
35
     * @param int|string                                $number     Numéro de l'étape
36
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|
37
     *     array<string,string|null|Settings|\Symfony\Component\Form\FormView> Rendue de la page
38
     */
39
    public function stepAction(Request $request, $entity, $entityPath, $typePath, $number)
40
    {
41
        $etm = $this->getDoctrine()->getManager();
42
        ${'ct'.$entity} = count($etm->getRepository('AppBundle:'.$entity)->findAll());
43
        ${strtolower($entity)} = $etm->getClassMetadata($entityPath)->newInstance();
44
        $message = null;
45
        
46
        if (${'ct'.$entity} > 0 && $request->getMethod() == 'GET' && is_int($number)) {
47
            $message = 'gestock.install.st'.$number.'.yet_exist';
48
        }
49
        $form = $this->createForm(new $typePath(), ${strtolower($entity)}, array(
50
            'action' => $this->generateUrl('gs_install_st'.$number)
51
        ));
52
        if (is_int($number)) {
53
            $return = array('message' => $message, 'form' => $form->createView(),);
54
        } else {
55
            $return = array(
56
                strtolower($entity) => ${strtolower($entity)},
57
                'form' => $form->createView(),
58
            );
59
        }
60
61
        if ($form->handleRequest($request)->isValid()) {
62
            $etm = $this->getDoctrine()->getManager();
63
            $etm->persist(${strtolower($entity)});
64
            $etm->flush();
65
66
            if ($form->get('save')->isSubmitted()) {
67
                $return = $this->redirect($this->generateUrl('gs_install_st4'));
68
            } elseif ($form->get('addmore')->isSubmitted()) {
69
                $return = $this->redirect($this->generateUrl('gs_install_st'.$number));
70
            } else {
71
                $return = $this->redirect($this->generateUrl('gs_install_st'.$number));
72
            }
73
            $this->addFlash('info', 'gestock.install.st'.$number.'.flash');
74
        }
75
        
76
        return $return;
77
    }
78
}
79