Completed
Pull Request — master (#4224)
by Craig
04:46
created

WizardHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 46
c 2
b 0
f 0
dl 0
loc 111
rs 10
wmc 15

4 Methods

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