Configurator::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
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 DP\Core\DistributionBundle\Configurator;
13
14
use DP\Core\DistributionBundle\Configurator\Step\StepInterface;
15
use Symfony\Component\Yaml\Yaml;
16
17
/**
18
 * Configurator.
19
 *
20
 * @author Marc Weistroff <[email protected]>
21
 */
22
class Configurator
23
{
24
    protected $filename;
25
    protected $installSteps;
26
    protected $updateSteps;
27
    protected $parameters;
28
    protected $kernelDir;
29
30
    public function __construct($kernelDir)
31
    {
32
        $this->kernelDir = $kernelDir;
33
        $this->filename = $kernelDir.'/config/parameters.yml';
34
35
        $this->installSteps = array();
36
        $this->updateSteps = array();
37
        $this->parameters = $this->read();
38
    }
39
40
    /**
41
     * @param StepInterface $step
42
     */
43
    public function addStep(StepInterface $step)
44
    {
45
        if ($step->isInstallStep()) {
46
            $this->installSteps[] = $step;
47
        }
48
        if ($step->isUpdateStep()) {
49
            $this->updateSteps[] = $step;
50
        }
51
    }
52
53
    /**
54
     * @param integer $step
55
     *
56
     * @return StepInterface
57
     */
58
    public function getInstallStep($step)
59
    {
60
        if (isset($this->installSteps[$step])) {
61
            return $this->installSteps[$step];
62
        }
63
    }
64
    
65
    /**
66
     * @param integer $step
67
     *
68
     * @return StepInterface
69
     */
70
    public function getUpdateStep($step)
71
    {
72
        if (isset($this->updateSteps[$step])) {
73
            return $this->updateSteps[$step];
74
        }
75
    }
76
77
    /**
78
     * @return integer
79
     */
80
    public function getInstallStepCount()
81
    {
82
        return count($this->installSteps);
83
    }
84
    
85
    /**
86
     * @return integer
87
     */
88
    public function getUpdateStepCount()
89
    {
90
        return count($this->updateSteps);
91
    }
92
    
93
    /**
94
     * @return array
95
     */
96
    public function getRequirements()
97
    {
98
        $requirements = [];
99
        
100
        foreach ($this->installSteps as $step) {
101
            $requirements = array_merge($requirements, $this->getStepRequirements($step));
102
        }
103
104
        $requirements = array_merge($requirements, $this->getCoreRequirements());
105
106
        return array('requirements' => $requirements, 'error' => in_array(false, $requirements));
107
    }
108
109
    private function getCoreRequirements()
110
    {
111
        $requirements = [];
112
113
        // Vérification de la présence de l'extension php socket
114
        $requirements['configurator.socket_extension'] = true;
115
        if (!function_exists('socket_create')) {
116
            $requirements['configurator.socket_extension'] = false;
117
        }
118
119
        // Vérification de la présence de l'extension php intl
120
        $requirements['configurator.intl_extension'] = true;
121
        if (!defined('INTL_MAX_LOCALE_LEN')) {
122
            $requirements['configurator.intl_extension'] = false;
123
        }
124
125
        return $requirements;
126
    }
127
128
    private function getStepRequirements(StepInterface $step)
129
    {
130
        $requirements = [];
131
132
        foreach ($step->checkRequirements() as $label => $state) {
133
            $requirements[$label] = $state;
134
        }
135
136
        return $requirements;
137
    }
138
139
    /**
140
     * Reads parameters from file.
141
     *
142
     * @return array
143
     */
144
    protected function read()
145
    {
146
        $filename = $this->filename;
147
        if (!$this->isFileWritable() && file_exists($this->getCacheFilename())) {
148
            $filename = $this->getCacheFilename();
149
        }
150
151
        $ret = Yaml::parse($filename);
152
        if (false === $ret || array() === $ret) {
153
            throw new \InvalidArgumentException(sprintf('The %s file is not valid.', $filename));
154
        }
155
156
        if (isset($ret['parameters']) && is_array($ret['parameters'])) {
157
            return $ret['parameters'];
158
        } else {
159
            return array();
160
        }
161
    }
162
163
    /**
164
     * Writes parameters to parameters.yml or temporary in the cache directory.
165
     *
166
     * @return integer
167
     */
168
    public function write()
169
    {
170
        $filename = $this->isFileWritable() ? $this->filename : $this->getCacheFilename();
171
172
        return file_put_contents($filename, $this->render());
173
    }
174
175
    /**
176
     * Renders parameters as a string.
177
     *
178
     * @return string
179
     */
180
    public function render()
181
    {
182
        return Yaml::dump(array('parameters' => $this->parameters));
183
    }
184
    
185
    public function getConfigParameters()
186
    {
187
        return $this->read();
188
    }
189
190
    public function isFileWritable()
191
    {
192
        return is_writable($this->filename);
193
    }
194
195
    public function clean()
196
    {
197
        if (file_exists($this->getCacheFilename())) {
198
            @unlink($this->getCacheFilename());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
199
        }
200
    }
201
202
    /**
203
     * @return array
204
     */
205
    public function getParameters()
206
    {
207
        return $this->parameters;
208
    }
209
210
    /**
211
     * @param array $parameters
212
     */
213
    public function mergeParameters($parameters)
214
    {
215
        $this->parameters = array_merge($this->parameters, $parameters);
216
    }
217
    
218
    public function getWhitelistFilepath()
219
    {
220
        return $this->kernelDir . '/../installer_whitelist.txt';
221
    }
222
    
223
    public function getKernelDir()
224
    {
225
        return $this->kernelDir;
226
    }
227
228
    /**
229
     * getCacheFilename
230
     *
231
     * @return string
232
     */
233
    protected function getCacheFilename()
234
    {
235
        return $this->kernelDir.'/cache/parameters.yml';
236
    }
237
}
238