Plugin::loadInstallerConfig()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 8.8571
cc 3
eloc 21
nc 2
nop 1
1
<?php namespace Comodojo\Installer;
2
3
use Composer\Composer;
4
use Composer\IO\IOInterface;
5
use Composer\Plugin\PluginInterface;
6
use Comodojo\Configuration\Installer as PackageInstaller;
7
8
/**
9
 *
10
 *
11
 * @package     Comodojo Framework
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @author      Marco Castiello <[email protected]>
14
 * @license     GPL-3.0+
15
 *
16
 * LICENSE:
17
 *
18
 * This program is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License as
20
 * published by the Free Software Foundation, either version 3 of the
21
 * License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30
 */
31
32
class Plugin implements PluginInterface {
33
34
    public function activate(Composer $composer, IOInterface $io) {
35
36
        $this->loadInstallerConfig($composer);
37
38
        if ( !$this->loadStaticConfiguration($composer) ) {
0 ignored issues
show
Unused Code introduced by
The call to Plugin::loadStaticConfiguration() has too many arguments starting with $composer.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
40
            $this->getIO()->write('<comment>Comodojo configuration not (yet) available.</comment>');
0 ignored issues
show
Bug introduced by
The method getIO() does not seem to exist on object<Comodojo\Installer\Plugin>.

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...
41
            
42
            $installer = new Installer($io, $composer);
0 ignored issues
show
Bug introduced by
The call to Installer::__construct() misses a required argument $package_installer.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$io is of type object<Composer\IO\IOInterface>, but the function expects a object<Composer\Composer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$composer is of type object<Composer\Composer>, but the function expects a object<Composer\IO\IOInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
44
        } else {
45
            
46
            $package_installer = new PackageInstaller();
47
            
48
            $installer = new Installer($io, $composer, $package_installer);
0 ignored issues
show
Documentation introduced by
$io is of type object<Composer\IO\IOInterface>, but the function expects a object<Composer\Composer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$composer is of type object<Composer\Composer>, but the function expects a object<Composer\IO\IOInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
            
50
            $this->getIO()->write('<comment>Comodojo configuration loaded, installer ready.</comment>');
0 ignored issues
show
Bug introduced by
The method getIO() does not seem to exist on object<Comodojo\Installer\Plugin>.

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
            
52
        }
53
54
        $composer->getInstallationManager()->addInstaller($installer);
55
56
    }
57
58
    private function loadInstallerConfig(Composer $composer) {
59
60
        $extra = $composer->getPackage()->getExtra();
61
62
        $installer_default_config = array(
63
            'app-assets' => 'public/apps',
64
            'framework-js' => 'public/js',
65
            'framework-templates' => 'public/templates',
66
            'local-cache' => 'cache',
67
            'static-config' => 'config',
68
            'local-logs' => 'logs',
69
            'local-database' => 'database'
70
        );
71
72
        if ( isset($extra['comodojo-installer-paths']) && is_array($extra['comodojo-installer-paths']) ) {
73
74
            $installer_config = array_replace($installer_default_config, $extra['comodojo-installer-paths']);
75
76
        } else {
77
78
            $installer_config = $installer_default_config;
79
80
        }
81
82
        define('COMODOJO_INSTALLER_APP_ASSETS', $installer_config['app-assets']);
83
84
        define('COMODOJO_INSTALLER_THEME_ASSETS', $installer_config['theme-assets']);
85
86
        define('COMODOJO_INSTALLER_LOCAL_CACHE', $installer_config['local-cache']);
87
88
        define('COMODOJO_INSTALLER_STATIC_CONFIG', $installer_config['static-config']);
89
90
        define('COMODOJO_INSTALLER_LOCAL_LOGS', $installer_config['local-logs']);
91
92
        define('COMODOJO_INSTALLER_LOCAL_DATABASE', $installer_config['local-database']);
93
94
        define('COMODOJO_INSTALLER_WORKING_DIRECTORY', getcwd());
95
96
    }
97
98
    private function loadStaticConfiguration() {
99
100
        $config_file = COMODOJO_INSTALLER_WORKING_DIRECTORY.COMODOJO_INSTALLER_STATIC_CONFIG.'/config.php';
101
102
        if ( is_file($config_file) && is_readable($config_file) ) {
103
104
            include_once($config_file);
105
106
            return true;
107
108
        }
109
110
        return false;
111
112
    }
113
114
}
115