Completed
Pull Request — master (#4265)
by Craig
05:16
created

ControllerHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 - 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\YamlDumper;
19
use Zikula\Component\Wizard\AbortStageException;
20
21
class ControllerHelper
22
{
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28
    public function __construct(TranslatorInterface $translator)
29
    {
30
        $this->translator = $translator;
31
    }
32
33
    /**
34
     * Set up PHP for Zikula installation.
35
     */
36
    public function initPhp(): array
37
    {
38
        $warnings = [];
39
        if (false === ini_set('default_charset', 'UTF-8')) {
40
            $currentSetting = ini_get('default_charset');
41
            $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%.', [
42
                '%command%' => 'ini_set',
43
                '%setting%' => 'default_charset',
44
                '%requiredValue%' => 'UTF-8',
45
                '%currentValue%' => $currentSetting
46
            ]);
47
        }
48
        if (false === mb_regex_encoding('UTF-8')) {
49
            $currentSetting = mb_regex_encoding();
50
            $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%.', [
51
                '%setting%' => 'mb_regex_encoding',
52
                '%requiredValue%' => 'UTF-8',
53
                '%currentValue%' => $currentSetting
54
            ]);
55
        }
56
        if (false === ini_set('memory_limit', '128M')) {
57
            $currentSetting = ini_get('memory_limit');
58
            $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%.', [
59
                '%command%' => 'ini_set',
60
                '%setting%' => 'memory_limit',
61
                '%requiredValue%' => '128M',
62
                '%currentValue%' => $currentSetting
63
            ]);
64
        }
65
        if (false === ini_set('max_execution_time', '86400')) {
66
            // 86400 = 24 hours
67
            $currentSetting = ini_get('max_execution_time');
68
            if ($currentSetting > 0) {
69
                // 0 = unlimited time
70
                $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%.', [
71
                    '%command%' => 'ini_set',
72
                    '%setting%' => 'max_execution_time',
73
                    '%requiredValue%' => '86400',
74
                    '%currentValue%' => $currentSetting
75
                ]);
76
            }
77
        }
78
79
        return $warnings;
80
    }
81
82
    /**
83
     * Write admin credentials to param file as encoded values.
84
     *
85
     * @throws AbortStageException
86
     */
87
    public function writeEncodedAdminCredentials(YamlDumper $yamlManager, array $data = []): void
88
    {
89
        foreach ($data as $k => $v) {
90
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
91
        }
92
        $params = array_merge($yamlManager->getParameters(), $data);
93
        try {
94
            $yamlManager->setParameters($params);
95
        } catch (IOException $exception) {
96
            throw new AbortStageException($this->translator->trans('Cannot write parameters to %fileName% file.', ['%fileName%' => 'services_custom.yaml']));
97
        }
98
    }
99
}
100