Passed
Pull Request — master (#31)
by Anatoly
39:30
created

FileStream::tempFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
use Throwable;
20
21
/**
22
 * Import functions
23
 */
24
use function fopen;
25
use function is_resource;
26
use function sprintf;
27
28
/**
29
 * FileStream
30
 */
31
final class FileStream extends Stream
32
{
33
34
    /**
35
     * Constructor of the class
36
     *
37
     * @param string $filename
38
     * @param string $mode
39
     *
40
     * @throws RuntimeException
41
     */
42 13
    public function __construct(string $filename, string $mode)
43
    {
44
        try {
45 13
            $resource = fopen($filename, $mode);
46 1
        } catch (Throwable $e) {
47 1
            $resource = false;
48
        }
49
50 13
        if (!is_resource($resource)) {
51 1
            throw new RuntimeException(sprintf(
52 1
                'Unable to open the file "%s" in the mode "%s"',
53 1
                $filename,
54 1
                $mode
55 1
            ));
56
        }
57
58 12
        parent::__construct($resource);
59
    }
60
}
61