Completed
Pull Request — master (#4224)
by Craig
05:10
created

WizardHelper::renderResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreInstallerBundle\Helper;
15
16
use Symfony\Component\Form\FormFactoryInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Routing\RouterInterface;
21
use Twig\Environment;
22
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
23
use Zikula\Bundle\CoreBundle\Response\PlainResponse;
24
use Zikula\Bundle\CoreBundle\YamlDumper;
25
use Zikula\Component\Wizard\FormHandlerInterface;
26
use Zikula\Component\Wizard\StageContainerInterface;
27
use Zikula\Component\Wizard\StageInterface;
28
use Zikula\Component\Wizard\Wizard;
29
use Zikula\Component\Wizard\WizardCompleteInterface;
30
31
class WizardHelper
32
{
33
    /**
34
     * @var RouterInterface
35
     */
36
    private $router;
37
38
    /**
39
     * @var StageContainerInterface
40
     */
41
    private $stageContainer;
42
43
    /**
44
     * @var Environment
45
     */
46
    private $twig;
47
48
    /**
49
     * @var FormFactoryInterface
50
     */
51
    private $formFactory;
52
53
    /**
54
     * @var string
55
     */
56
    private $locale;
57
58
    public function __construct(
59
        RouterInterface $router,
60
        StageContainerInterface $stageContainer,
61
        Environment $twig,
62
        FormFactoryInterface $formFactory,
63
        string $locale
64
    ) {
65
        $this->router = $router;
66
        $this->stageContainer = $stageContainer;
67
        $this->twig = $twig;
68
        $this->formFactory = $formFactory;
69
        $this->locale = $locale;
70
    }
71
72
    /**
73
     * Executes the wizard for installation or upgrade process.
74
     */
75
    public function processWizard(Request $request, string $stage, $mode = 'install', YamlDumper $yamlDumper = null): Response
76
    {
77
        if (!in_array($mode, ['install', 'upgrade'])) {
78
            $mode = 'install';
79
        }
80
81
        $session = $request->hasSession() ? $request->getSession() : null;
82
83
        // check php
84
        $iniWarnings = $this->initPhp();
0 ignored issues
show
Bug introduced by
The method initPhp() does not exist on Zikula\Bundle\CoreInstal...dle\Helper\WizardHelper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        /** @scrutinizer ignore-call */ 
85
        $iniWarnings = $this->initPhp();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
        if (null !== $session && 0 < count($iniWarnings)) {
86
            $session->getFlashBag()->add('warning', implode('<hr />', $iniWarnings));
87
        }
88
89
        // begin the wizard
90
        $wizard = new Wizard($this->stageContainer, dirname(__DIR__) . '/Resources/config/' . $mode . '_stages.yaml');
91
        $currentStage = $wizard->getCurrentStage($stage);
92
        if ($currentStage instanceof WizardCompleteInterface) {
93
            if ('upgrade' === $mode && null !== $yamlDumper) {
94
                $yamlDumper->setParameter('upgrading', false);
95
            }
96
97
            return $currentStage->getResponse($request);
98
        }
99
100
        $templateParams = $this->getTemplateGlobals($currentStage);
101
        $templateParams['headertemplate'] = '@ZikulaCoreInstaller/' . $mode . 'Header.html.twig';
102
        if ($wizard->isHalted()) {
103
            if (null !== $session) {
104
                $session->getFlashBag()->add('danger', $wizard->getWarning());
105
            }
106
107
            return $this->renderResponse('@ZikulaCoreInstaller/error.html.twig', $templateParams);
108
        }
109
110
        // handle the form
111
        if ($currentStage instanceof FormHandlerInterface) {
112
            $form = $this->formFactory->create($currentStage->getFormType(), null, $currentStage->getFormOptions());
113
            $form->handleRequest($request);
114
            if ($form->isSubmitted() && $form->isValid()) {
115
                $currentStage->handleFormResult($form);
116
                $params = [
117
                    'stage' => $wizard->getNextStage()->getName(),
118
                    '_locale' => $this->locale
119
                ];
120
121
                $url = $this->router->generate($mode, $params);
122
123
                return new RedirectResponse($url);
124
            }
125
            $templateParams['form'] = $form->createView();
126
        }
127
128
        return $this->renderResponse($currentStage->getTemplateName(), $templateParams);
129
    }
130
131
    private function renderResponse(string $view, array $parameters = [], Response $response = null): Response
132
    {
133
        if (null === $response) {
134
            $response = new PlainResponse();
135
        }
136
        $response->setContent($this->twig->render($view, $parameters));
137
138
        return $response;
139
    }
140
141
    private function getTemplateGlobals(StageInterface $currentStage): array
142
    {
143
        $globals = [
144
            'version' => ZikulaKernel::VERSION,
145
            'currentstage' => $currentStage->getName()
146
        ];
147
148
        return array_merge($globals, $currentStage->getTemplateParams());
149
    }
150
}
151