Completed
Push — master ( a921a6...4bc1f0 )
by Mihail
06:01
created

Environment::loadAverage()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 29
rs 6.7272
cc 7
eloc 16
nc 10
nop 0
1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
6
/**
7
 * Class Environment. Special function to get information about working environment
8
 * @package Ffcms\Core\Helper
9
 */
10
class Environment
11
{
12
    /**
13
     * Get current operation system full name
14
     * @return string
15
     */
16
    public static function osName()
17
    {
18
        return php_uname('a');
19
    }
20
21
    /**
22
     * Get working API type (apache2handler, cgi, fcgi, etc)
23
     * @return string
24
     */
25
    public static function phpSAPI()
26
    {
27
        return php_sapi_name();
28
    }
29
30
    /**
31
     * Get current php version and small build info
32
     * @return string
33
     */
34
    public static function phpVersion()
35
    {
36
        return phpversion();
37
    }
38
39
    /**
40
     * Get load average in percents.
41
     * @return string
42
     */
43
    public static function loadAverage()
44
    {
45
        $load = 0;
46
        if (stristr(PHP_OS, 'win')) {
47
            // its not a better solution, but no other way to do this
48
            $cmd = "wmic cpu get loadpercentage /all";
49
            @exec($cmd, $output);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
50
51
            // if output is exist
52
            if ($output) {
53
                // try to find line with numeric data
54
                foreach ($output as $line) {
55
                    if ($line && preg_match("/^[0-9]+\$/", $line)) {
56
                        $load = $line;
57
                        break;
58
                    }
59
                }
60
            }
61
        } else {
62
            $sys_load = sys_getloadavg(); // get linux load average (1 = 100% of 1 CPU)
63
            $load = $sys_load[0] * 100; // to percentage
64
        }
65
66
        if ((int)$load <= 0) {
67
            return 'error';
68
        }
69
70
        return (int)$load . '%';
71
    }
72
}