Completed
Push — master ( da9ea2...cfe8fe )
by Rougin
08:08
created

Tools   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 97.83%

Importance

Changes 9
Bugs 4 Features 0
Metric Value
wmc 20
lcom 0
cbo 0
dl 0
loc 210
ccs 90
cts 92
cp 0.9783
rs 10
c 9
b 4
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A strip_table_schema() 0 4 1
A hasLayout() 0 7 2
C ignite() 0 94 8
A isCommandEnabled() 0 4 2
A isDoctrineEnabled() 0 4 1
A isWildfireEnabled() 0 4 1
A stripTableSchema() 0 6 2
B removeLibrary() 0 34 3
1
<?php
2
3
namespace Rougin\Combustor\Common;
4
5
use Rougin\Combustor\Common\File;
6
7
/**
8
 * Tools
9
 *
10
 * Provides a list of multi-purpose functions for Combustor.
11
 * 
12
 * @package Combustor
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class Tools
16
{
17
    /**
18
     * Checks whether the header and footer file exists.
19
     *
20
     * @return bool
21
     */
22 3
    public static function hasLayout()
23
    {
24 3
        $header = APPPATH . 'views/layout/header.php';
25 3
        $footer = APPPATH . 'views/layout/footer.php';
26
27 3
        return file_exists($header) && file_exists($footer);
28
    }
29
30
    /**
31
     * "Ignites" the post installation process.
32
     * 
33
     * @return void
34
     */
35 24
    public static function ignite()
36
    {
37
        // Gets data from application/config/config.php
38 24
        $config = file_get_contents(APPPATH . 'config/config.php');
39
40 24
        $search = ['$config[\'composer_autoload\'] = FALSE;'];
41 24
        $replace = ['$config[\'composer_autoload\'] = realpath(\'vendor\') . \'/autoload.php\';'];
42
43
        // Removes the index.php from $config['index_page'].
44 24
        if (strpos($config, '$config[\'index_page\'] = \'index.php\';') !== false) {
45 24
            array_push($search, '$config[\'index_page\'] = \'index.php\';');
46 24
            array_push($replace, '$config[\'index_page\'] = \'\';');
47 24
        }
48
49
        // Adds an encryption key from the configuration.
50 24
        if (strpos($config, '$config[\'encryption_key\'] = \'\';') !== false) {
51 24
            array_push($search, '$config[\'encryption_key\'] = \'\';');
52 24
            array_push($replace, '$config[\'encryption_key\'] = \''.md5('rougin').'\';');
53 24
        }
54
55 24
        $config = str_replace($search, $replace, $config);
56 24
        file_put_contents(APPPATH . 'config/config.php', $config);
57
58
        // Gets data from application/config/autoload.php
59 24
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
60 24
        $lines = explode(PHP_EOL, $autoload);
61
62
        // Gets the currently included libraries.
63 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
64
65 24
        preg_match_all($pattern, $lines[60], $match);
66
67 24
        $libraries = explode(', ', end($match[1]));
68
69
        // Includes "session" library.
70 24
        if ( ! in_array('\'session\'', $libraries)) {
71 24
            array_push($libraries, '\'session\'');
72 24
        }
73
74 24
        $libraries = array_filter($libraries);
75
76
        // Includes the added libraries all back to autoload.php.
77 24
        $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
78 24
        $replacement = '$autoload[\'libraries\'] = array('.implode(', ', $libraries).');';
79
80 24
        $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
81
82
        // Gets the currently included helpers
83 24
        $pattern = '/\$autoload\[\'helper\'\] = array\((.*?)\)/';
84
85 24
        preg_match_all($pattern, $lines[85], $match);
86
87 24
        $defaultHelpers = [ '\'form\'', '\'url\'', ];
88 24
        $helpers = explode(', ', end($match[1]));
89
90 24
        foreach ($defaultHelpers as $helper) {
91 24
            if ( ! in_array($helper, $helpers)) {
92 24
                array_push($helpers, $helper);
93 24
            }    
94 24
        }
95
96 24
        $helpers = array_filter($helpers);
97
98
        // Include the added helpers all back to autoload.php
99 24
        $pattern = '/\$autoload\[\'helpers\'\] = array\([^)]*\);/';
100 24
        $replacement = '$autoload[\'helpers\'] = array('.implode(', ', $helpers).');';
101
102 24
        preg_replace($pattern, $replacement, $lines[60]);
103
104 24
        file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
105
106
        // Creates a new .htaccess file if it does not exists.
107 24
        if ( ! file_exists('.htaccess')) {
108 24
            $template = __DIR__ . '/../Templates/Htaccess.template';
109
110 24
            $file = fopen('.htaccess', 'wb');
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111 24
            $contents = file_get_contents($template);
112
113 24
            file_put_contents('.htaccess', $contents);
114 24
            chmod('.htaccess', 0777);
115 24
        }
116
117
        // Creates a configuration for the Pagination library.
118 24
        if ( ! file_exists(APPPATH . 'config/pagination.php')) {
119 24
            $pagination = APPPATH . 'config/pagination.php';
120 24
            $template = __DIR__ . '/../Templates/Pagination.template';
121
122 24
            $file = fopen($pagination, 'wb');
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123 24
            $contents = file_get_contents($template);
124
125 24
            file_put_contents($pagination, $contents);
126 24
            chmod($pagination, 0664);
127 24
        }
128 24
    }
129
130
    /**
131
     * Checks whether the command is enabled or not in the current environment.
132
     *
133
     * @return bool
134
     */
135 3
    public static function isCommandEnabled()
136
    {
137 3
        return self::isWildfireEnabled() || self::isDoctrineEnabled();
138
    }
139
140
    /**
141
     * Checks if Doctrine exists.
142
     *
143
     * @return bool
144
     */
145 3
    public static function isDoctrineEnabled()
146
    {
147 3
        return file_exists(APPPATH . 'libraries/Doctrine.php');
148
    }
149
150
    /**
151
     * Checks if Wildfire exists.
152
     *
153
     * @return bool
154
     */
155 3
    public static function isWildfireEnabled()
156
    {
157 3
        return file_exists(APPPATH . 'libraries/Wildfire.php');
158
    }
159
160
    /**
161
     * Strips the table schema from the table name.
162
     * 
163
     * @param  string $table
164
     * @return string
165
     */
166 9
    public static function stripTableSchema($table)
167
    {
168 9
        return (strpos($table, '.') !== false)
169 9
            ? substr($table, strpos($table, '.') + 1)
170 9
            : $table;
171
    }
172
173
    /**
174
     * Removes the specified library in the application.
175
     * 
176
     * @param  string $type
177
     * @return string
178
     */
179 6
    public static function removeLibrary($type)
180
    {
181 6
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
182
183 6
        $lines = explode(PHP_EOL, $autoload);
184 6
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
185
186 6
        preg_match_all($pattern, $lines[60], $match);
187
188 6
        $libraries = explode(', ', end($match[1]));
189
190 6
        if (in_array('\'' . $type . '\'', $libraries)) {
191 6
            $position = array_search('\'' . $type . '\'', $libraries);
192
193 6
            unset($libraries[$position]);
194
195 6
            $libraries = array_filter($libraries);
196
197 6
            $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
198 6
            $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
199
200 6
            $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
201
202 6
            file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
203 6
        }
204
205 6
        if ($type == 'doctrine') {
206 3
            system('composer remove doctrine/orm');
207 3
        }
208
209 6
        unlink(APPPATH . 'libraries/' . ucfirst($type) . '.php');
210
211 6
        return ucfirst($type) . ' is now successfully removed!';
212
    }
213
214
    /**
215
     * Strips the table schema from the table name.
216
     * 
217
     * @param  string $table
218
     * @return string
219
     */
220
    public static function strip_table_schema($table)
221
    {
222
        return self::stripTableSchema($table);
223
    }
224
}
225