Statistics::getHddDiskUsage()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace ExpressApi\V1\Rpc\Statistics;
4
5
use ExpressApi\Apis\CPanel;
6
7
class Statistics {
8
9
    private $config;
10
    private $cpanel;
11
12
    function __construct($config) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
        $this->config = $config;
14
        $this->cpanel = new CPanel($this->config); // Connect to cPanel - only do this once.
15
    }
16
17
    public function getHddDiskUsage() {
18
        $result = '';
19
        // Report the raw disk usage
20
        $reportDiskUsage = json_decode($this->cpanel->getHddDiskUsage());
21
        if($reportDiskUsage->cpanelresult && is_array($reportDiskUsage->cpanelresult->data)) {
22
            $data = $reportDiskUsage->cpanelresult->data[0];
23
            $result = $this->formatBytes((int) $data->user_contained_usage);
24
        }
25
        return $result;
26
    }
27
28
    public function getBandwidthUsage() {
29
        $result = '';
30
        // Report the raw disk usage
31
        $reportBandwidthUsage = json_decode($this->cpanel->getBandwidthUsage());
32
        if($reportBandwidthUsage->cpanelresult && is_array($reportBandwidthUsage->cpanelresult->data)) {
33
            $data = $reportBandwidthUsage->cpanelresult->data[0];
34
            $result = $this->formatBytes($data->bw);
35
        }
36
        return $result;
37
    }
38
39
    public function getEmailsAccountsOverQuota() {
40
        $result = array();
41
        // Report the raw disk usage
42
        $emailsAccountsReport = json_decode($this->cpanel->getEmailAccounts());
43
        if($emailsAccountsReport->cpanelresult && is_array($emailsAccountsReport->cpanelresult->data)) {
44
            $data = $emailsAccountsReport->cpanelresult->data;
45
            if(is_array($data) && count($data)) {
46
                foreach($data as $emailInfo) {
47
                    if((float) $emailInfo->diskusedpercent20 > 75) {
48
                        $result[] = array(
49
                            'email' => $emailInfo->email,
50
                            'disk_used' => $emailInfo->diskusedpercent20
51
                        );
52
                    }
53
                }
54
            }
55
        }
56
        return $result;
57
    }
58
59
    public function getEmailsAccountsDiskUsage() {
60
        $result = 0;
61
        // Report the raw disk usage
62
        $emailsAccountsReport = json_decode($this->cpanel->getEmailAccounts());
63
        if($emailsAccountsReport->cpanelresult && is_array($emailsAccountsReport->cpanelresult->data)) {
64
            $data = $emailsAccountsReport->cpanelresult->data;
65
            if(is_array($data) && count($data)) {
66
                foreach($data as $emailInfo) {
67
                    $result += (float) $emailInfo->diskused;
68
                }
69
            }
70
        }
71
        return $result;
72
    }
73
74
    public function getEmailsAccountsCount() {
75
        $result = 0;
76
        // Report the raw disk usage
77
        $emailsAccountsReport = json_decode($this->cpanel->getEmailAccounts());
78
        if($emailsAccountsReport->cpanelresult && is_array($emailsAccountsReport->cpanelresult->data)) {
79
            $result = count($emailsAccountsReport->cpanelresult->data);
80
        }
81
        return $result;
82
    }
83
84
    private function formatBytes($bytes, $precision = 2) {
85
        $base = log($bytes, 1024);
86
        $suffixes = array('', ' KB', ' MB', ' GB', ' TB');
87
88
        return round(pow(1024, $base - floor($base)), $precision).$suffixes[floor($base)];
89
    }
90
}
91