Passed
Push — master ( 8a455a...42a228 )
by Craig
07:01
created

UpgraderController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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\Controller;
15
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
21
use Zikula\Bundle\CoreBundle\YamlDumper;
22
use Zikula\Component\Wizard\FormHandlerInterface;
23
use Zikula\Component\Wizard\Wizard;
24
use Zikula\Component\Wizard\WizardCompleteInterface;
25
26
/**
27
 * Class UpgraderController
28
 */
29
class UpgraderController extends AbstractController
30
{
31
    public const ZIKULACORE_MINIMUM_UPGRADE_VERSION = '1.4.3';
32
33
    public function __construct(ContainerInterface $container)
34
    {
35
        parent::__construct($container);
36
        $this->router = $container->get('router');
37
        $this->form = $this->container->get('form.factory');
38
    }
39
40
    public function upgradeAction(Request $request, $stage): Response
41
    {
42
        $currentVersion = $this->container->getParameter(ZikulaKernel::CORE_INSTALLED_VERSION_PARAM);
43
        if (version_compare($currentVersion, ZikulaKernel::VERSION, '=')) {
44
            $stage = 'complete';
45
        }
46
        // not installed?
47
        if (false === $this->container->getParameter('installed')) {
48
            return new RedirectResponse($this->router->generate('install'));
49
        }
50
51
        // check php
52
        $ini_warnings = $this->controllerHelper->initPhp();
53
        if (count($ini_warnings) > 0) {
54
            $request->getSession()->getFlashBag()->add('warning', implode('<hr>', $ini_warnings));
55
        }
56
57
        $yamlDumper = new YamlDumper($this->container->get('kernel')->getRootDir() . '/config', 'custom_parameters.yml');
58
        $yamlDumper->setParameter('upgrading', true);
59
        $request->setLocale($this->container->getParameter('locale'));
60
61
        // begin the wizard
62
        $wizard = new Wizard($this->container, dirname(__DIR__) . '/Resources/config/upgrade_stages.yml');
63
        $currentStage = $wizard->getCurrentStage($stage);
64
        if ($currentStage instanceof WizardCompleteInterface) {
65
            $yamlDumper->setParameter('upgrading', false);
66
67
            return $currentStage->getResponse($request);
68
        }
69
        $templateParams = $this->controllerHelper->getTemplateGlobals($currentStage);
70
        $templateParams['headertemplate'] = '@ZikulaCoreInstaller/upgradeheader.html.twig';
71
        if ($wizard->isHalted()) {
72
            $request->getSession()->getFlashBag()->add('danger', $wizard->getWarning());
73
74
            return $this->renderResponse('@ZikulaCoreInstaller/error.html.twig', $templateParams);
75
        }
76
77
        // handle the form
78
        if ($currentStage instanceof FormHandlerInterface) {
79
            $form = $this->form->create($currentStage->getFormType(), null, $currentStage->getFormOptions());
0 ignored issues
show
Bug introduced by
The method create() does not exist on Symfony\Component\Form\Form. Did you maybe mean createView()? ( Ignorable by Annotation )

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

79
            /** @scrutinizer ignore-call */ 
80
            $form = $this->form->create($currentStage->getFormType(), null, $currentStage->getFormOptions());

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...
80
            $form->handleRequest($request);
81
            if ($form->isSubmitted() && $form->isValid()) {
82
                $currentStage->handleFormResult($form);
83
                $params = ['stage' => $wizard->getNextStage()->getName(), '_locale' => $this->container->getParameter('locale')];
84
                $url = $this->router->generate('upgrade', $params);
85
86
                return new RedirectResponse($url);
87
            }
88
            $templateParams['form'] = $form->createView();
89
        }
90
91
        return $this->renderResponse($currentStage->getTemplateName(), $templateParams);
92
    }
93
}
94