Passed
Push — test ( 32f496...c27118 )
by Tom
02:18
created

LibTmp::tmpFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines;
6
7
use UnexpectedValueException;
8
9
/**
10
 * Library for temporary files and directories related functions
11
 */
12
class LibTmp
13
{
14
    /**
15
     * Create temporary file w/ contents
16
     *
17
     * @param string $buffer
18
     * @return string path of temporary file
19
     */
20 1
    public static function tmpFilePut($buffer)
21
    {
22 1
        list(, $path) = self::tmpFile();
23 1
        file_put_contents($path, $buffer);
24
25 1
        return $path;
26
    }
27
28
    /**
29
     * Create handle and path of a temporary file (which gets cleaned up)
30
     *
31
     * @return array(handle, string)
32
     */
33 2
    public static function tmpFile()
34
    {
35 2
        $handle = tmpfile();
36 2
        if (false === $handle) {
37
            // @codeCoverageIgnoreStart
38
            throw new UnexpectedValueException('Unable to create temporary file');
39
            // @codeCoverageIgnoreEnd
40
        }
41 2
        $meta = stream_get_meta_data($handle);
42
43 2
        return array($handle, $meta['uri']);
44
    }
45
46
    /**
47
     * Create temporary directory (which does not get cleaned up)
48
     *
49
     * @param string $prefix [optional]
50
     * @return string path
51
     */
52 1
    public static function tmpDir($prefix = '')
53
    {
54 1
        $path = tempnam(sys_get_temp_dir(), $prefix);
55 1
        LibFs::rm($path);
56 1
        LibFs::mkDir($path, 0700);
57
58 1
        return $path;
59
    }
60
}
61