Completed
Push — master ( be15bc...e01bc3 )
by Albin
03:32 queued 36s
created

Size::fromResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Gaufrette\Util;
4
5
/**
6
 * Utility class for file sizes.
7
 *
8
 * @author Antoine Hérault <[email protected]>
9
 */
10
class Size
11
{
12
    /**
13
     * Returns the size in bytes from the given content.
14
     *
15
     * @param string $content
16
     *
17
     * @return int
18
     *
19
     * @todo handle the case the mbstring is not loaded
20
     */
21
    public static function fromContent($content)
22
    {
23
        // Make sure to get the real length in byte and not
24
        // accidentally mistake some bytes as a UTF BOM.
25
        return mb_strlen($content, '8bit');
26
    }
27
28
    /**
29
     * Returns the size in bytes from the given file.
30
     *
31
     * @param string $filename
32
     *
33
     * @return string
34
     */
35
    public static function fromFile($filename)
36
    {
37
        return filesize($filename);
38
    }
39
40
    /**
41
     * Returns the size in bytes from the given resource.
42
     *
43
     * @param resource $handle
44
     *
45
     * @return string
46
     */
47
    public static function fromResource($handle)
48
    {
49
        $cStat = fstat($handle);
50
        // if the resource is a remote file, $cStat will be false
51
        return $cStat ? $cStat['size'] : 0;
52
    }
53
}
54