CimLogicalDisk::getFreeSpace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Cim;
4
5
/**
6
 * @link https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/cim-logicaldisk
7
 */
8
class CimLogicalDisk extends CimStorageExtent
9
{
10
    protected $byte_suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
11
12
    protected $uuid = '{8502C539-5FBB-11D2-AAC1-006008C78BC7}';
13
14
    protected $freeSpace;
15
16
    protected $size;
17
18
    protected $hidden_attributes = ['byte_suffixes'];
19
20
    protected $attribute_casting = [
21
        'freeSpace' => 'int',
22
        'size'      => 'int',
23
    ];
24
25
    public function getFreeSpace(int $decimal_places = 2, array $byte_suffixes = null)
26
    {
27
        return $this->convertBytes($this->getAttribute('freeSpace'), $decimal_places, $byte_suffixes);
28
    }
29
30
    public function getSize(int $decimal_places = 2, array $byte_suffixes = null)
31
    {
32
        return $this->convertBytes($this->getAttribute('size'), $decimal_places, $byte_suffixes);
33
    }
34
35
    protected function convertBytes($bytes, int $decimal_places = 2, array $byte_suffixes = null)
36
    {
37
        $byte_suffixes = $byte_suffixes ?? $this->byte_suffixes;
38
        $factor = floor((strlen($bytes) - 1) / 3);
39
40
        return sprintf("%.{$decimal_places}f", $bytes / pow(1024, $factor)) . ' ' . $byte_suffixes[(int) $factor] ?? '';
41
    }
42
}
43