Passed
Push — master ( 756b0e...0edf2f )
by Josh
02:56
created

MODXPackagesConfig::addPackageConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 8
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
4
namespace LCI\Blend\Transport;
5
6
7
use LCI\MODX\Console\Console;
8
9
class MODXPackagesConfig
10
{
11
    /** @var \modX */
0 ignored issues
show
Bug introduced by
The type modX was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
    public static $modx;
13
14
    /** @var bool|Console */
15
    protected static $console = false;
16
17
    /** @var string */
18
    protected static $package_file;
19
20
    /** @var bool|array ~ ['package_name' => ['signature' => 'ace-1.8.0-pl', 'provider' => 'modx.com', 'latest' => true], ... ] */
21
    protected static $packages = false;
22
23
    /**
24
     * @param string $signature - ex: ace-1.8.0-pl
25
     * @param bool $latest_version
26
     * @param string $provider_name
27
     */
28
    public static function addPackageConfig($signature, $latest_version=true, $provider_name='modx.com')
29
    {
30
        static::loadMODXTransportPackageInfo();
31
        $package_parts = MODXPackages::getVersionInfo($signature);
32
33
        static::$packages[$package_parts['base']] = [
34
            'signature' => $signature,
35
            'latest' => $latest_version,
36
            'provider' => $provider_name
37
        ];
38
39
        ksort(static::$packages);
0 ignored issues
show
Bug introduced by
It seems like static::packages can also be of type boolean; however, parameter $array of ksort() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        ksort(/** @scrutinizer ignore-type */ static::$packages);
Loading history...
40
41
        static::writeCacheFile(static::$package_file, static::$packages);
42
    }
43
44
    /**
45
     * @return array|bool
46
     */
47
    public static function getPackages()
48
    {
49
        static::loadMODXTransportPackageInfo();
50
51
        return static::$packages;
52
    }
53
54
    /**
55
     * @param string $signature
56
     */
57
    public static function removePackageConfig($signature)
58
    {
59
        static::loadMODXTransportPackageInfo();
60
        $package_parts = MODXPackages::getVersionInfo($signature);
61
62
        if (isset(static::$packages[$package_parts['base']])) {
63
            unset(static::$packages[$package_parts['base']]);
64
65
            static::writeCacheFile(static::$package_file, static::$packages);
0 ignored issues
show
Bug introduced by
It seems like static::packages can also be of type boolean; however, parameter $data of LCI\Blend\Transport\MODX...onfig::writeCacheFile() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            static::writeCacheFile(static::$package_file, /** @scrutinizer ignore-type */ static::$packages);
Loading history...
66
        }
67
    }
68
69
    /**
70
     * @return \LCI\MODX\Console\Console
71
     */
72
    protected static function getConsole()
73
    {
74
        if (!static::$console) {
75
            /** @var \LCI\MODX\Console\Console $console */
76
            static::$console = new Console();
77
        }
78
79
        return static::$console;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::console also could return the type true which is incompatible with the documented return type LCI\MODX\Console\Console.
Loading history...
80
    }
81
82
    /**
83
     *
84
     */
85
    protected static function loadMODXTransportPackageInfo()
86
    {
87
        /** @var \LCI\MODX\Console\Console $console */
88
        $console = static::getConsole();
89
90
        if (empty(static::$modx)) {
91
            static::$modx = $console->loadMODX();
92
        }
93
94
        if (!static::$packages) {
95
            static::$packages = [];
96
97
            static::$package_file = $console->getConfigFilePaths()['config_dir'] . 'lci_modx_transport_package.php';
98
99
            if (file_exists(static::$package_file)) {
100
                static::$packages = include static::$package_file;
101
            }
102
        }
103
    }
104
105
    /**
106
     * @param string $file
107
     * @param array $data
108
     */
109
    protected static function writeCacheFile($file, $data)
110
    {
111
        $content = '<?php ' . PHP_EOL .
112
            'return ' . var_export($data, true) . ';';
113
114
        file_put_contents($file, $content);
115
    }
116
}
117