LibTmp::tmpFilePut()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
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
     *
19
     * @return string path of temporary file
20
     */
21 1
    public static function tmpFilePut($buffer)
22
    {
23 1
        list(, $path) = self::tmpFile();
24 1
        file_put_contents($path, $buffer);
25
26 1
        return $path;
27
    }
28
29
    /**
30
     * Create handle and path of a temporary file (which gets cleaned up)
31
     *
32
     * @return array [resource stream-handle, string path]
33
     */
34 2
    public static function tmpFile()
35
    {
36 2
        $handle = tmpfile();
37 2
        if (false === $handle) {
38
            // @codeCoverageIgnoreStart
39
            throw new UnexpectedValueException('Unable to create temporary file');
40
            // @codeCoverageIgnoreEnd
41
        }
42 2
        $meta = stream_get_meta_data($handle);
43
44 2
        return array($handle, $meta['uri']);
45
    }
46
47
    /**
48
     * Create temporary directory (which does not get cleaned up)
49
     *
50
     * @param string $prefix [optional]
51
     *
52
     * @return string path
53
     */
54 1
    public static function tmpDir($prefix = '')
55
    {
56 1
        $path = tempnam(sys_get_temp_dir(), $prefix);
57 1
        LibFs::rm($path);
58 1
        LibFs::mkDir($path, 0700);
59
60 1
        return $path;
61
    }
62
}
63