PluginRequirements   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 120
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removePluginRequirement() 0 12 3
A addPluginRequirement() 0 19 3
A checkRequiredPlugins() 0 17 5
A checkPluginInstalled() 0 8 2
1
<?php
2
3
namespace TwinDigital\WPTools;
4
5
/**
6
 * Class PluginRequirements
7
 */
8
class PluginRequirements
9
{
10
11
    /**
12
     * The minimum required PHP-version.
13
     * @var string $phpMinVersion
14
     */
15
    public static $phpMinVersion;
16
17
    /**
18
     * The maximum required PHP-version.
19
     * @var string $phpMaxVersion
20
     */
21
    public static $phpMaxVersion;
22
23
    /**
24
     * Internal list of the required plugins that are added by `addPluginRequirement()`
25
     * @var array $requiredPlugins
26
     */
27
    protected static $requiredPlugins = [];
28
29
    /**
30
     * The minimum required WordPress-version.
31
     * @var string $wpMinVersion
32
     */
33
    public static $wpMinVersion;
34
35
    /**
36
     * The maximum required WordPress-version.
37
     * @var string $wpMaxVersion
38
     */
39
    public static $wpMaxVersion;
40
41
    /**
42
     * Add requirement for a plugin.
43
     * @param string $pluginName       The name of the plugin.
44
     * @param string $pluginMinVersion The minimum version (SemVer) of the plugin.
45
     * @param string $pluginMaxVersion The maximum version (SemVer) of the plugin.
46
     * @param bool   $activated        Wether the plugin should be activated or not.
47
     *
48
     * @return bool Wether the plugin was added to the requirements or not.
49
     */
50
    public static function addPluginRequirement(
51
        string $pluginName,
52
        string $pluginMinVersion,
53
        string $pluginMaxVersion,
54
        bool $activated = true
55
    ) {
56
        foreach (self::$requiredPlugins as $plugin) {
57
            if ($plugin['Name'] === $pluginName) {
58
                return false;
59
            }
60
        }
61
        self::$requiredPlugins[] = [
62
            'Name'       => $pluginName,
63
            'MinVersion' => $pluginMinVersion,
64
            'MaxVersion' => $pluginMaxVersion,
65
            'Active'     => $activated,
66
        ];
67
68
        return true;
69
    }
70
71
    /**
72
     * Removes a plugin from the requirements-list.
73
     * @param string $pluginName The name of the plugin.
74
     *
75
     * @return bool
76
     */
77
    public static function removePluginRequirement(string $pluginName): bool
78
    {
79
        $return = false;
80
        foreach (self::$requiredPlugins as $key => $plugin) {
81
            if ($plugin['Name'] === $pluginName) {
82
                unset(self::$requiredPlugins[$key]);
83
                $return = true;
84
            }
85
        }
86
        self::$requiredPlugins = array_values(self::$requiredPlugins);
87
88
        return $return;
89
    }
90
91
    /**
92
     * Checks the required plugins.
93
     * @return bool Wether all checks are successful.
94
     */
95
    public static function checkRequiredPlugins(): bool
96
    {
97
        $return = true;
98
        foreach (self::$requiredPlugins as $plugin) {
99
            $installed = self::checkPluginInstalled($plugin['Name']);
100
            if ($installed === false) {
101
                $return = false;
102
            } elseif ($installed === true) {
103
                if ($plugin['Active'] === true) {
104
                    $return = (bool)(
105
                        $plugin['Active'] === PluginTools::getPluginByTitleCaseInsensitive($plugin['Name'])['Active']
106
                    );
107
                }
108
            }
109
        }
110
111
        return $return;
112
    }
113
114
    /**
115
     * Checks wether a plugin is installed.
116
     * @param string $pluginName The name of the plugin.
117
     *
118
     * @return bool
119
     */
120
    public static function checkPluginInstalled(string $pluginName): bool
121
    {
122
        $return = false;
123
        if (is_array(PluginTools::getPluginByTitleCaseInsensitive($pluginName)) === true) {
124
            $return = true;
125
        }
126
127
        return $return;
128
    }
129
}
130