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

ControllerHelper::renderResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
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\Filesystem\Exception\IOException;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
19
use Zikula\Bundle\CoreBundle\YamlDumper;
20
use Zikula\Component\Wizard\AbortStageException;
21
22
class ControllerHelper
23
{
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
29
    public function __construct(TranslatorInterface $translator)
30
    {
31
        $this->translator = $translator;
32
    }
33
34
    /**
35
     * Set up PHP for Zikula installation.
36
     */
37
    public function initPhp(): array
38
    {
39
        $warnings = [];
40
        if (false === ini_set('default_charset', 'UTF-8')) {
41
            $currentSetting = ini_get('default_charset');
42
            $warnings[] = $this->translator->trans('Could not use %command% to set the %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [
43
                '%command%' => 'ini_set',
44
                '%setting%' => 'default_charset',
45
                '%requiredValue%' => 'UTF-8',
46
                '%currentValue%' => $currentSetting
47
            ]);
48
        }
49
        if (false === mb_regex_encoding('UTF-8')) {
50
            $currentSetting = mb_regex_encoding();
51
            $warnings[] = $this->translator->trans('Could not set %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [
52
                '%setting%' => 'mb_regex_encoding',
53
                '%requiredValue%' => 'UTF-8',
54
                '%currentValue%' => $currentSetting
55
            ]);
56
        }
57
        if (false === ini_set('memory_limit', '128M')) {
58
            $currentSetting = ini_get('memory_limit');
59
            $warnings[] = $this->translator->trans('Could not use %command% to set the %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [
60
                '%command%' => 'ini_set',
61
                '%setting%' => 'memory_limit',
62
                '%requiredValue%' => '128M',
63
                '%currentValue%' => $currentSetting
64
            ]);
65
        }
66
        if (false === ini_set('max_execution_time', '86400')) {
67
            // 86400 = 24 hours
68
            $currentSetting = ini_get('max_execution_time');
69
            if ($currentSetting > 0) {
70
                // 0 = unlimited time
71
                $warnings[] = $this->translator->trans('Could not use %command% to set the %setting% to the value of %requiredValue%. The install or upgrade process may fail at your current setting of %currentValue%.', [
72
                    '%command%' => 'ini_set',
73
                    '%setting%' => 'max_execution_time',
74
                    '%requiredValue%' => '86400',
75
                    '%currentValue%' => $currentSetting
76
                ]);
77
            }
78
        }
79
80
        return $warnings;
81
    }
82
83
    /**
84
     * @return array|bool
85
     */
86
    public function requirementsMet()
87
    {
88
        // several other requirements are checked before Symfony is loaded.
89
        // @see /var/SymfonyRequirements.php
90
        // @see \Zikula\Bundle\CoreInstallerBundle\Util\ZikulaRequirements::runSymfonyChecks
91
        $results = [];
92
93
        $x = explode('.', str_replace('-', '.', PHP_VERSION));
94
        $phpVersion = "{$x[0]}.{$x[1]}.{$x[2]}";
95
        $results['phpsatisfied'] = version_compare($phpVersion, ZikulaKernel::PHP_MINIMUM_VERSION, '>=');
96
        $results['pdo'] = extension_loaded('pdo');
97
        $supportsUnicode = preg_match('/^\p{L}+$/u', 'TheseAreLetters');
98
        $results['pcreUnicodePropertiesEnabled'] = (isset($supportsUnicode) && (bool)$supportsUnicode);
99
        $requirementsMet = true;
100
        foreach ($results as $check) {
101
            if (!$check) {
102
                $requirementsMet = false;
103
                break;
104
            }
105
        }
106
        if ($requirementsMet) {
107
            return true;
108
        }
109
        $results['phpversion'] = PHP_VERSION;
110
        $results['phpcoreminversion'] = ZikulaKernel::PHP_MINIMUM_VERSION;
111
112
        return $results;
113
    }
114
115
    /**
116
     * Write admin credentials to param file as encoded values.
117
     *
118
     * @throws AbortStageException
119
     */
120
    public function writeEncodedAdminCredentials(YamlDumper $yamlManager, array $data = []): void
121
    {
122
        foreach ($data as $k => $v) {
123
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
124
        }
125
        $params = array_merge($yamlManager->getParameters(), $data);
126
        try {
127
            $yamlManager->setParameters($params);
128
        } catch (IOException $exception) {
129
            throw new AbortStageException($this->translator->trans('Cannot write parameters to %fileName% file.', ['%fileName%' => 'services_custom.yaml']));
130
        }
131
    }
132
}
133