DiskUsageMonitor::runMonitor()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace EricMakesStuff\ServerMonitor\Monitors;
4
5
use Carbon\Carbon;
6
use EricMakesStuff\ServerMonitor\Events\DiskUsageAlarm;
7
use EricMakesStuff\ServerMonitor\Events\DiskUsageHealthy;
8
use EricMakesStuff\ServerMonitor\Exceptions\InvalidPath;
9
use EricMakesStuff\ServerMonitor\Helpers\Format;
10
11
class DiskUsageMonitor extends BaseMonitor
12
{
13
    /**  @var int */
14
    protected $totalSpace;
15
16
    /**  @var int */
17
    protected $freeSpace;
18
19
    /**  @var int */
20
    protected $usedSpace;
21
22
    /**  @var float */
23
    protected $percentageUsed;
24
25
    /** @var string */
26
    protected $path;
27
28
    /** @var int */
29
    protected $alarmPercentage = 75;
30
31
    /**
32
     * @param array $config
33
     */
34
    public function __construct(array $config)
35
    {
36
        $this->path = __DIR__;
37
38
        if (!empty($config['path'])) {
39
            $this->path = $config['path'];
40
        }
41
42
        if (!empty($config['alarmPercentage'])) {
43
            $this->alarmPercentage = intval($config['alarmPercentage']);
44
        }
45
    }
46
47
    /**
48
     * @throws InvalidPath
49
     */
50
    public function runMonitor()
51
    {
52
        if (! file_exists($this->path)) {
53
            throw InvalidPath::pathDoesNotExist($this->path);
54
        }
55
56
        $this->totalSpace = disk_total_space($this->path);
0 ignored issues
show
Documentation Bug introduced by
The property $totalSpace was declared of type integer, but disk_total_space($this->path) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
57
58
        $this->freeSpace = disk_free_space($this->path);
0 ignored issues
show
Documentation Bug introduced by
The property $freeSpace was declared of type integer, but disk_free_space($this->path) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
59
        
60
        $this->usedSpace = $this->totalSpace - $this->freeSpace;
0 ignored issues
show
Documentation Bug introduced by
The property $usedSpace was declared of type integer, but $this->totalSpace - $this->freeSpace is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
61
62
        $this->percentageUsed = sprintf('%.2f',($this->usedSpace / $this->totalSpace) * 100);
0 ignored issues
show
Documentation Bug introduced by
The property $percentageUsed was declared of type double, but sprintf('%.2f', $this->u...this->totalSpace * 100) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
63
        
64
        if ($this->percentageUsed >= $this->alarmPercentage) {
65
            event(new DiskUsageAlarm($this));
66
        } else {
67
            event(new DiskUsageHealthy($this));
68
        }
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getTotalSpace()
75
    {
76
        return Format::getHumanReadableSize($this->totalSpace);
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getFreeSpace()
83
    {
84
        return Format::getHumanReadableSize($this->freeSpace);
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getUsedSpace()
91
    {
92
        return Format::getHumanReadableSize($this->usedSpace);
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getPercentageUsed()
99
    {
100
        return $this->percentageUsed . '%';
101
    }
102
103
    /**
104
     * @return mixed|string
105
     */
106
    public function getPath()
107
    {
108
        return $this->path;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getAlarmPercentage()
115
    {
116
        return $this->alarmPercentage . '%';
117
    }
118
}
119