StaticConfigurationDumper::dump()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
1
<?php namespace Comodojo\Installer\Scripts;
2
3
use \Comodojo\Exception\InstallerException;
4
5
/**
6
 * Comodojo Installer
7
 *
8
 * @package     Comodojo Framework
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     GPL-3.0+
11
 *
12
 * LICENSE:
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 */
27
28
class StaticConfigurationDumper {
29
30
    private $settings = array(
31
        'COMODOJO_DATABASE_MODEL' => 'MYSQL',
32
        'COMODOJO_DATABASE_HOST' => 'localhost',
33
        'COMODOJO_DATABASE_PORT' => 3306,
34
        'COMODOJO_DATABASE_NAME' => 'comodojo',
35
        'COMODOJO_DATABASE_USER' => 'comodojo',
36
        'COMODOJO_DATABASE_PASS' => 'comodojo',
37
        'COMODOJO_DATABASE_PREFIX' => "cmdj_"
38
    );
39
    
40
    public function __construct( $parameters = array() ) {
41
        
42
        $this->set('COMODOJO_REAL_PATH', COMODOJO_INSTALLER_WORKING_DIRECTORY);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
43
        $this->set('COMODOJO_STATIC_CONFIG', COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_STATIC_CONFIG);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
44
        $this->set('COMODOJO_LOCAL_CACHE', COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_LOCAL_CACHE);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
45
        $this->set('COMODOJO_LOCAL_LOGS', COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_LOCAL_LOGS);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
46
        $this->set('COMODOJO_LOCAL_DATABASE', COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_LOCAL_DATABASE);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
47
        $this->set('COMODOJO_APP_ASSETS', COMODOJO_INSTALLER_APP_ASSETS);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
48
        $this->set('COMODOJO_THEME_ASSETS', COMODOJO_INSTALLER_THEME_ASSETS);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
49
        
50
        $this->set('COMODOJO_AUTH_KEY', self::generateKey());
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
51
        $this->set('COMODOJO_PRIV_KEY', self::generateKey());
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
52
        
53
        
54
        foreach( $parameters as $parameter => $value ) {
55
            
56
            $this->set($parameter, $value);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Comodojo\Installe...ticConfigurationDumper>.

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...
57
            
58
        }
59
        
60
    }
61
62
    public function __set($setting, $value) {
63
64
        $this->$settings[$setting] = $value;
0 ignored issues
show
Bug introduced by
The variable $settings does not exist. Did you mean $setting?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
65
66
        return $this;
67
68
    }
69
70
    public function __get($setting) {
71
72
        if (array_key_exists($setting, $this->$settings)) {
0 ignored issues
show
Bug introduced by
The variable $settings does not exist. Did you mean $setting?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
73
74
            return $this->$settings[$setting];
0 ignored issues
show
Bug introduced by
The variable $settings does not exist. Did you mean $setting?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
75
76
        }
77
78
        return null;
79
80
    }
81
82
    public function __isset($setting) {
83
84
        return isset($this->$settings[$setting]);
0 ignored issues
show
Bug introduced by
The variable $settings does not exist. Did you mean $setting?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
85
86
    }
87
88
    public function dump() {
89
90
        $config_file = COMODOJO_INSTALLER_WORKING_DIRECTORY.'/'.COMODOJO_INSTALLER_STATIC_CONFIG.'/'.comodojo-config.php;
91
92
        $template = $this->loadConfigurationTemplate();
93
94
        foreach ($this->settings as $setting => $value) {
95
96
            $template = str_replace('_'.$setting.'_', $value, $template);
97
98
        }
99
100
        $action = file_put_contents($config_file, $template, LOCK_EX);
101
102
        if ( $action === false ) throw new InstallerException("Cannot write comodojo-config file!");
103
104
        return true;
105
106
    }
107
108
    private function loadConfigurationTemplate() {
109
110
        $template_file = realpath(dirname(__FILE__)."/../../")."/comodojo-config.template";
111
112
        $template = file_get_contents($template_file);
113
114
        if ( $template === false ) throw new InstallerException("Cannot read comodojo-config template!");
115
116
        return $template;
117
118
    }
119
    
120
    private function generateKey() {
121
        
122
        return md5(uniqid(rand(), true), 0);
123
        
124
    }
125
    
126
    private function generateSalt() {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
127
        
128
        return uniqid(mt_rand(), true);
129
        
130
    }
131
132
}
133