Completed
Branch master (7a77f2)
by Nikola
03:15 queued 01:04
created

Filesize   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 4
c 4
b 0
f 2
lcom 1
cbo 0
dl 0
loc 45
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getBytes() 0 17 3
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Utils;
14
15
/**
16
 * Class Filesize
17
 *
18
 * File size utilities
19
 *
20
 * @package RunOpenCode\Backup\Utils
21
 */
22
final class Filesize
23
{
24
    private function __construct() {}
25
26
    private static $units = array(
27
        't' => 8796093022208,
28
        'tb' => 8796093022208,
29
        'g' => 8589934592,
30
        'gb' => 8589934592,
31
        'm' => 8388608,
32
        'mb' => 8388608
33
    );
34
35
    /**
36
     * Get bytes from formatted string size.
37
     *
38
     * E.g:
39
     *
40
     * 1m, 1mb => megabytes
41
     * 1g, 1gb => gigabytes
42
     * 1t, 1tb => terabytes
43
     *
44
     * Note: size format is case insensitive.
45
     *
46
     * @param $size
47
     * @return int
48
     */
49 12
    public static function getBytes($size)
50
    {
51 12
        if (is_numeric($size)) {
52 10
            return intval($size);
53
        }
54
55 4
        $size = strtolower(trim($size));
56
57 4
        $numeric = preg_replace('/[^0-9]/', '', $size);
58 4
        $unit = str_replace($numeric, '', $size);
59
60 4
        if (isset(self::$units[$unit])) {
61 2
            return $numeric * self::$units[$unit];
62
        }
63
64 2
        throw new \InvalidArgumentException(sprintf('Unknown size format: "%s"', $size));
65
    }
66
}
67