Completed
Push — Recipes ( 630f49...c8afb0 )
by Laurent
12:15 queued 03:48
created

AbstractInstallController::stepAction()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
cc 9
nc 32
nop 5
1
<?php
2
/**
3
 * AbstractInstallController Installation controller of the GLSR application.
4
 *
5
 * PHP Version 7
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
 * @see      https://github.com/Dev-Int/glsr
14
 */
15
16
namespace App\Controller\Install;
17
18
use App\Controller\AbstractAppController;
19
use Symfony\Component\HttpFoundation\Request;
20
use Doctrine\Common\Persistence\ObjectManager;
21
22
/**
23
 * AbstractInstall controller.
24
 *
25
 * @category Controller
26
 */
27
abstract class AbstractInstallController extends AbstractAppController
28
{
29
    /**
30
     * Step X of the installation.
31
     * Function adaptable to the different stages of the installation.
32
     *
33
     * @param \Symfony\Component\HttpFoundation\Request $request    Form request
34
     * @param string                                    $entityName Entity name
35
     * @param string                                    $entityPath Entity Namespace
36
     * @param string                                    $typePath   Type of Namespace
37
     * @param int|string                                $number     Step number
38
     *
39
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|
40
     *     array<string,string|null|Settings|\Symfony\Component\Form\FormView> Rendered view
41
     */
42
    public function stepAction(Request $request, $entityName, $entityPath, $typePath, $number)
43
    {
44
        $etm = $this->getDoctrine()->getManager();
45
        $ctEntity = count($etm->getRepository('App:'.$entityName)->findAll());
46
        $entityNew = $etm->getClassMetadata($entityPath)->newInstance();
47
        $message = null;
48
49
        if ($ctEntity > 0 && 'GET' == $request->getMethod() && is_numeric($number) && 5 != $number) {
50
            $message = 'gestock.install.st'.$number.'.yet_exist';
51
        }
52
        $form = $this->createForm($typePath, $entityNew, ['action' => $this->generateUrl('gs_install_st'.$number)]);
53
        if ('Staff\Group' === $entityName) {
54
            $this->addRolesAction($form, $entityNew);
0 ignored issues
show
Compatibility introduced by
$form of type object<Symfony\Component\Form\FormInterface> is not a sub-type of object<Symfony\Component\Form\Form>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
55
        }
56
        if (is_int($number)) {
57
            $return = ['message' => $message, 'form' => $form->createView()];
58
        } else {
59
            $return = ['message' => $message,
60
                $this->nameToVariable($entityName) => $entityNew,
61
                'form' => $form->createView(), ];
62
        }
63
        if ('Settings\Diverse\Material' === $entityName) {
64
            $articles = $etm->getRepository('App:Settings\Article')->findAll();
65
            $return['articles'] = $articles;
66
        }
67
68
        if ($form->handleRequest($request)->isValid()) {
69
            $return = $this->validInstall($entityNew, $form, $etm, $number, $entityName);
0 ignored issues
show
Compatibility introduced by
$form of type object<Symfony\Component\Form\FormInterface> is not a sub-type of object<Symfony\Component\Form\Form>. It seems like you assume a concrete implementation of the interface Symfony\Component\Form\FormInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
        }
71
72
        return $return;
73
    }
74
75
    /**
76
     * Valid install step.
77
     *
78
     * @param object                                     $entityNew  Entity
79
     * @param \Symfony\Component\Form\Form               $form       Form of Entity
80
     * @param \Doctrine\Common\Persistence\ObjectManager $etm        Entity Manager
81
     * @param int|string                                 $number     Number of step install
82
     * @param string                                     $entityName Entity name
83
     *
84
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse Route after valid or not
85
     */
86
    private function validInstall($entityNew, $form, ObjectManager $etm, $number, $entityName)
87
    {
88
        $options = [];
89
        $return = '';
90
        if ('Settings\Diverse\Material' === $entityName) {
91
            $articles = $etm->getRepository('App:Settings\Article')->findAll();
92
            $options = ['articles' => $articles];
93
        }
94
        $etm->persist($entityNew);
95
        $etm->flush();
96
        if (is_numeric($number)) {
97
            $this->addFlash('info', 'gestock.install.st'.$number.'.flash');
98
        } else {
99
            $this->addFlash('info', 'gestock.install.st5.flash');
100
        }
101
102
        if (null !== $form->get('save') || null !== $form->get('addmore')) {
103
            if ($form->get('save')->isClicked() && is_numeric($number)) {
104
                $numberNext = $number++;
105
                $return = $this->redirect($this->generateUrl('gs_install_st'.$numberNext, $options));
106
            } elseif ($form->get('save')->isClicked() && !is_numeric($number)) {
107
                $return = $this->redirect($this->generateUrl('gs_install_st5'));
108
            } elseif ($form->get('addmore')->isClicked()) {
109
                $return = $this->redirect($this->generateUrl('gs_install_st'.$number, $options));
110
            }
111
        } else {
112
            $return = $this->redirect($this->generateUrl('gs_install_st'.$number, $options));
113
        }
114
115
        return $return;
116
    }
117
118
    /**
119
     * Transform a Namespace to string of class.
120
     *
121
     * @param string $name Namespace to transform
122
     *
123
     * @return string
124
     */
125
    private function nameToVariable($name)
126
    {
127
        $array = explode('\\', $name);
128
        $return = strtolower(end($array));
129
130
        return $return;
131
    }
132
}
133