Passed
Pull Request — master (#27)
by Anatoly
39:10
created

TmpfileStream   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 21
ccs 6
cts 8
cp 0.75
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
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 is_resource;
24
use function is_writable;
25
use function sys_get_temp_dir;
26
use function tmpfile;
27
28
/**
29
 * The tmpfile() function opens a unique temporary file in binary
30
 * read/write (w+b) mode. The file will be automatically deleted
31
 * when it is closed or the program terminates.
32
 *
33
 * @link https://www.php.net/tmpfile
34
 */
35
class TmpfileStream extends Stream
36
{
37
38
    /**
39
     * Constructor of the class
40
     *
41
     * @throws RuntimeException
42
     */
43 15
    public function __construct()
44
    {
45 15
        $tmpdir = sys_get_temp_dir();
46 15
        if (!is_writable($tmpdir)) {
47
            throw new RuntimeException('Temporary files directory is not writable');
48
        }
49
50 15
        $tmpfile = tmpfile();
51 15
        if (!is_resource($tmpfile)) {
52
            throw new RuntimeException('Temporary file cannot be created or opened');
53
        }
54
55 15
        parent::__construct($tmpfile);
56
    }
57
}
58