GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SilverstripePage::getVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Selim;
4
5
use Exception;
6
use Symfony\Component\Yaml\Exception\ParseException;
7
use Symfony\Component\Yaml\Yaml;
8
9
class SilverstripePage
10
{
11
    private $path_configphp;
12
    private $path_project;
13
    private $path_configyml;
14
    private $path_root;
15
    private $path_ssversion;
16
    private $path_composer;
17
    private $name;
18
    private $version;
19
    private $defadmin;
20
    private $maillog;
21
    private $envtype;
22
    private $modules;
23
24
    public function __construct(SiteConfig $sc)
25
    {
26
        $this->name = $sc->name;
27
28
        if ($this->setupPaths($sc->path)) {
29
            if (!$this->path_configphp) {
30
                throw new Exception("No _config.php found at: $sc->path ($sc->name)");
31
            }
32
        }
33
34
        $this->reload();
35
    }
36
37
    /**
38
     * gets the path of all required files and folders.
39
     *
40
     * @param string $path
41
     *
42
     * @return boolean
43
     */
44
    public function setupPaths($path)
45
    {
46
        $this->path_project = realpath($path);
47
        $this->path_configphp = realpath($this->path_project.'/_config.php');
48
        $this->path_configyml = realpath($this->path_project.'/_config/config.yml');
49
        $this->path_root = realpath($this->path_project.'/..');
50
        $this->path_ssversion = realpath($this->path_project.'/../framework/')
51
            ? realpath($this->path_project.'/../framework/silverstripe_version')
52
            : realpath($this->path_project.'/../sapphire/silverstripe_version');
53
        $this->path_composer = realpath($this->path_root.'/composer.lock');
54
    }
55
56
    public function reload()
57
    {
58
        $this->readVersion();
59
        $this->readDefaultAdmin();
60
        $this->readEmailLogging();
61
        $this->readEnvironmentType();
62
        $this->readModules();
63
    }
64
65
    private function readVersion()
66
    {
67
        $this->version = null;
68
        if ($this->path_ssversion) {
69
            $content_ssv = file_get_contents($this->path_ssversion);
70
            $v = array();
71
            preg_match_all("/\\d+\\.\\d+\\.\\d+/", $content_ssv, $v);
72
73
            if (!empty($v) && !empty($v[0]) && isset($v[0][0])) {
74
                $this->version = $v[0][0];
75
            }
76
        }
77
78
        if ($this->version === null) {
79
            if ($this->path_composer) {
80
                $content_c = file_get_contents($this->path_composer);
81
                $json = json_decode($content_c);
82
                if ($json->packages) {
83
                    foreach ($json->packages as $package) {
84
                        if ($package->name && $package->name == "silverstripe/cms") {
85
                            if (is_string($package->version)) {
86
                                $this->version = $package->version;
87
                            }
88
                        }
89
                    }
90
                }
91
            } else {
92
                $this->version = realpath($this->path_root.'/sapphire') ? "2" : "3";
93
            }
94
        }
95
    }
96
97
    private function readDefaultAdmin()
98
    {
99
        $d = $this->matchInConfigPhp("/^\\s*Security::setDefaultAdmin/m");
100
        $this->defadmin = $d && $d[0] && $d[0][0] ? true : false;
101
    }
102
103
    private function readEmailLogging()
104
    {
105
        $m = $this->matchInConfigPhp("/\\s*SS_Log::add_writer\\(\\s*new\\s*SS_LogEmailWriter/m");
106
        $this->maillog = $m && $m[0] && $m[0][0] ? true : false;
107
    }
108
109
    private function readEnvironmentType()
110
    {
111
        $et = $this->matchInConfigPhp("/\\s*Director::set_environment_type\\(\\s*['\"](?<env>dev|live|test*)['\"]\\s*\\);/m");
112
        if ($et && $et["env"]) {
113
            $this->envtype = $et["env"][0];
114
        } else if ($this->path_configyml) {
115
            $content = file_get_contents($this->path_configyml);
116
            foreach (preg_split("/^---/m", $content) as $block) {
117
                try {
118
                    //Seems like the Yaml parser doesnt like the commas in the first block of the _config.yml
119
                    if (preg_match("~'framework/\\*','cms/\\*'~", $block)) {
120
                        continue;
121
                    }
122
                    $yml = Yaml::parse($block);
123
                    if ($yml && array_key_exists("Director", $yml) && array_key_exists("environment_type", $yml["Director"])) {
124
                        $this->envtype = $yml["Director"]["environment_type"];
125
                    }
126
                } catch (ParseException $e) {
127
                    echo $e->getMessage().PHP_EOL;
128
                }
129
            }
130
        }else{
131
            $this->envtype = "live";
132
        }
133
    }
134
135
    private function readModules()
136
    {
137
        $modules = array();
138
        $proj = basename($this->path_project);
139
140
        if ($this->path_root) {
141
            foreach (scandir($this->path_root) as $f) {
142
                $abs = "$this->path_root/$f";
143
                if (is_dir($abs) && realpath("$abs/_config.php") && $f !== $proj) {
144
                    switch ($f) {
145
                        case "assets":
146
                        case "cms":
147
                        case "framework":
148
                        case "sapphire":
149
                            break;
150
                        default:
151
                            array_push($modules, $f);
152
                            break;
153
                    }
154
                }
155
            }
156
        }
157
        $this->modules = $modules;
158
    }
159
160
    /**
161
     * searches for a regex string in PROJECT/_config.php.
162
     *
163
     * @param string $regex
164
     *
165
     * @return boolean|array
166
     */
167
    private function matchInConfigPhp($regex)
168
    {
169
        if ($this->path_configphp) {
170
            $content = file_get_contents($this->path_configphp);
171
            $content = Util::stripPhpComments($content);
172
            $matches = array();
173
            preg_match_all($regex, $content, $matches);
174
175
            return $matches;
176
        }
177
178
        return null;
179
    }
180
181
    /**
182
     * @param string $regex
183
     * @return bool
184
     */
185
    public function hasModule($regex)
186
    {
187
        foreach ($this->modules as $m) {
188
            if (preg_match($regex, $m) === 1) {
189
                return true;
190
            }
191
        }
192
193
        return false;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getName()
200
    {
201
        return $this->name;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getVersion()
208
    {
209
        return $this->version;
210
    }
211
212
    /**
213
     * @return boolean indicates if a DefaultAdmin is specified
214
     */
215
    public function hasDefaultAdmin()
216
    {
217
        return $this->defadmin;
218
    }
219
220
    /**
221
     * @return boolean indicates if EmailLogging is activated
222
     */
223
    public function hasEmailLogging()
224
    {
225
        return $this->maillog;
226
    }
227
228
    /**
229
     * @return string with the EnvironmentType
230
     */
231
    public function getEnvironmentType()
232
    {
233
        return $this->envtype;
234
    }
235
236
    /**
237
     * @return array with the module folder names
238
     */
239
    public function getModules()
240
    {
241
        return $this->modules;
242
    }
243
244
    public function getConfigPhpPath()
245
    {
246
        return $this->path_configphp;
247
    }
248
249
    public function getConfigYmlPath()
250
    {
251
        return $this->path_configyml;
252
    }
253
254
    public function getRootPath()
255
    {
256
        return $this->path_root;
257
    }
258
}
259