Completed
Push — Recipes ( 432d36 )
by Laurent
03:22
created

AbstractInstallController::validInstall()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 10
nc 3
nop 4
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 GIT: <git_id>
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    Form request
32
     * @param string                                    $entity     Entity name
33
     * @param string                                    $entityPath Entity Namespace
34
     * @param string                                    $typePath   Type of Namespace
35
     * @param int|string                                $number     Step number
36
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|
37
     *     array<string,string|null|Settings|\Symfony\Component\Form\FormView> Rendered view
38
     */
39
    public function stepAction(Request $request, $entity, $entityPath, $typePath, $number)
40
    {
41
        $etm = $this->getDoctrine()->getManager();
42
        $ctEntity = count($etm->getRepository('AppBundle:'.$entity)->findAll());
43
        $entityNew = $etm->getClassMetadata($entityPath)->newInstance();
44
        $message = null;
45
        
46
        if ($ctEntity > 0 && $request->getMethod() == 'GET' && is_int($number)) {
47
            $message = 'gestock.install.st'.$number.'.yet_exist';
48
        }
49
        $form = $this->createForm($typePath, $entityNew, ['action' => $this->generateUrl('gs_install_st'.$number),]);
50
        if (is_int($number)) {
51
            $return = ['message' => $message, 'form' => $form->createView(),];
52
        } else {
53
            $return = [strtolower($entity) => $entityNew, 'form' => $form->createView(),];
54
        }
55
56
        if ($form->handleRequest($request)->isValid()) {
57
            $return = $this->validInstall($form, $etm, $number);
0 ignored issues
show
Bug introduced by
The call to validInstall() misses a required argument $number.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$etm is of type object<Doctrine\Common\Persistence\ObjectManager>, but the function expects a object<Symfony\Component\Form\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$number is of type integer|string, but the function expects a object<AppBundle\Control...\Install\ObjectManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
59
            $this->addFlash('info', 'gestock.install.st'.$number.'.flash');
60
        }
61
        
62
        return $return;
63
    }
64
65
    /**
66
     * Valid install step.
67
     *
68
     * @param object                                     $entityNew Entity
69
     * @param \Symfony\Component\Form\Form               $form      Form of Entity
70
     * @param \Doctrine\Common\Persistence\ObjectManager $etm       Entity Manager
71
     * @param integer                                     $number   Number of step install
72
     * @return array Route after valid or not
73
     */
74
    private function validInstall($entityNew, $form, ObjectManager $etm, $number)
75
    {
76
        $etm->persist($entityNew);
77
        $etm->flush();
78
79
        if ($form->get('save')->isClicked()) {
80
            $return = $this->redirect($this->generateUrl('gs_install_st4'));
81
        } elseif ($form->get('addmore')->isClicked()) {
82
            $return = $this->redirect($this->generateUrl('gs_install_st'.$number));
83
        } else {
84
            $return = $this->redirect($this->generateUrl('gs_install_st'.$number));
85
        }
86
87
        return $return;
88
    }
89
}
90