Passed
Push — master ( 24ee82...991457 )
by Anatoly
02:42 queued 12s
created

TempFileStream   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 28
ccs 8
cts 11
cp 0.7272
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Stream;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Message\Exception\RuntimeException;
18
use Sunrise\Http\Message\Stream;
19
20
/**
21
 * Import functions
22
 */
23
use function fopen;
24
use function is_resource;
25
use function is_writable;
26
use function sys_get_temp_dir;
27
use function tempnam;
28
29
/**
30
 * TempFileStream
31
 */
32
final class TempFileStream extends Stream
33
{
34
35
    /**
36
     * Constructor of the class
37
     *
38
     * @param string $prefix
39
     *
40
     * @throws RuntimeException
41
     */
42 1
    public function __construct(string $prefix = '')
43
    {
44 1
        $dirname = sys_get_temp_dir();
45 1
        if (!is_writable($dirname)) {
46
            throw new RuntimeException('Temporary files directory is not writable');
47
        }
48
49 1
        $filename = tempnam($dirname, $prefix);
50 1
        if ($filename === false) {
51
            throw new RuntimeException('Temporary file name cannot be generated');
52
        }
53
54 1
        $resource = fopen($filename, 'w+b');
55 1
        if (!is_resource($resource)) {
56
            throw new RuntimeException('Temporary file cannot be created or opened');
57
        }
58
59 1
        parent::__construct($resource);
60
    }
61
}
62