Completed
Push — master ( 6090d6...edf18c )
by Craig
09:38 queued 03:17
created

ControllerHelper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 21.99 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 31
loc 141
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTemplateGlobals() 0 9 1
B initPhp() 31 55 8
B requirementsMet() 0 28 5
A writeEncodedAdminCredentials() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreInstallerBundle\Helper;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\Filesystem\Exception\IOException;
16
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
17
use Zikula\Bundle\CoreBundle\YamlDumper;
18
use Zikula\Common\Translator\TranslatorInterface;
19
use Zikula\Component\Wizard\AbortStageException;
20
use Zikula\Component\Wizard\StageInterface;
21
use Zikula\Core\Exception\FatalErrorException;
22
23
class ControllerHelper
24
{
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * ControllerHelper constructor.
32
     * @param TranslatorInterface $translator
33
     */
34
    public function __construct(TranslatorInterface $translator)
35
    {
36
        $this->translator = $translator;
37
    }
38
39
    /**
40
     * return an array of variables to assign to all installer templates
41
     *
42
     * @return array
43
     */
44
    public function getTemplateGlobals(StageInterface $currentStage)
45
    {
46
        $globals = [
47
            'version' => ZikulaKernel::VERSION,
48
            'currentstage' => $currentStage->getName()
49
        ];
50
51
        return array_merge($globals, $currentStage->getTemplateParams());
52
    }
53
54
    /**
55
     * Set up php for zikula install
56
     *
57
     * @throws FatalErrorException if settings are not capable of performing install or sustaining Zikula
58
     */
59
    public function initPhp()
60
    {
61
        $warnings = [];
62
        if (version_compare(\PHP_VERSION, '5.6.0', '<') && false === ini_set('mbstring.internal_encoding', 'UTF-8')) {
63
            // mbstring.internal_encoding is deprecated in php 5.6.0
64
            $currentSetting = ini_get('mbstring.internal_encoding');
65
            $warnings[] = $this->translator->__f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', [
66
                '%1$s' => 'ini_set',
67
                '%2$s' => 'mbstring.internal_encoding',
68
                '%3$s' => 'UTF-8',
69
                '%4$s' => $currentSetting
70
            ]);
71
        }
72 View Code Duplication
        if (false === ini_set('default_charset', 'UTF-8')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            $currentSetting = ini_get('default_charset');
74
            $warnings[] = $this->translator->__f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', [
75
                '%1$s' => 'ini_set',
76
                '%2$s' => 'default_charset',
77
                '%3$s' => 'UTF-8',
78
                '%4$s' => $currentSetting
79
            ]);
80
        }
81
        if (false === mb_regex_encoding('UTF-8')) {
82
            $currentSetting = mb_regex_encoding();
83
            $warnings[] = $this->translator->__f('Could not set %1$s to the value of %2$s. The install or upgrade process may fail at your current setting of %3$s.', [
84
                '%1$s' => 'mb_regex_encoding',
85
                '%2$s' => 'UTF-8',
86
                '%3$s' => $currentSetting
87
            ]);
88
        }
89 View Code Duplication
        if (false === ini_set('memory_limit', '128M')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $currentSetting = ini_get('memory_limit');
91
            $warnings[] = $this->translator->__f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', [
92
                '%1$s' => 'ini_set',
93
                '%2$s' => 'memory_limit',
94
                '%3$s' => '128M',
95
                '%4$s' => $currentSetting
96
            ]);
97
        }
98 View Code Duplication
        if (false === ini_set('max_execution_time', 86400)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
            // 86400 = 24 hours
100
            $currentSetting = ini_get('max_execution_time');
101
            if ($currentSetting > 0) {
102
                // 0 = unlimited time
103
                $warnings[] = $this->translator->__f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', [
104
                    '%1$s' => 'ini_set',
105
                    '%2$s' => 'max_execution_time',
106
                    '%3$s' => '86400',
107
                    '%4$s' => $currentSetting
108
                ]);
109
            }
110
        }
111
112
        return $warnings;
113
    }
114
115
    public function requirementsMet(ContainerInterface $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        // several other requirements are checked before Symfony is loaded.
118
        // @see app/SymfonyRequirements.php
119
        // @see \Zikula\Bundle\CoreInstallerBundle\Util\ZikulaRequirements::runSymfonyChecks
120
        $results = [];
121
122
        $x = explode('.', str_replace('-', '.', phpversion()));
123
        $phpVersion = "$x[0].$x[1].$x[2]";
124
        $results['phpsatisfied'] = version_compare($phpVersion, ZikulaKernel::PHP_MINIMUM_VERSION, ">=");
125
        $results['pdo'] = extension_loaded('pdo');
126
        $isEnabled = @preg_match('/^\p{L}+$/u', 'TheseAreLetters');
127
        $results['pcreUnicodePropertiesEnabled'] = (isset($isEnabled) && (bool)$isEnabled);
128
        $requirementsMet = true;
129
        foreach ($results as $check) {
130
            if (!$check) {
131
                $requirementsMet = false;
132
                break;
133
            }
134
        }
135
        if ($requirementsMet) {
136
            return true;
137
        }
138
        $results['phpversion'] = phpversion();
139
        $results['phpcoreminversion'] = ZikulaKernel::PHP_MINIMUM_VERSION;
140
141
        return $results;
142
    }
143
144
    /**
145
     * Write admin credentials to param file as encoded values
146
     *
147
     * @param YamlDumper $yamlManager
148
     * @param array $data
149
     * @throws AbortStageException
150
     */
151
    public function writeEncodedAdminCredentials(YamlDumper $yamlManager, array $data)
152
    {
153
        foreach ($data as $k => $v) {
154
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
155
        }
156
        $params = array_merge($yamlManager->getParameters(), $data);
157
        try {
158
            $yamlManager->setParameters($params);
159
        } catch (IOException $e) {
1 ignored issue
show
Bug introduced by
The class Symfony\Component\Filesystem\Exception\IOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
160
            throw new AbortStageException($this->translator->__f('Cannot write parameters to %s file.', ['%s' => 'custom_parameters.yml']));
161
        }
162
    }
163
}
164