FileStream   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 32
ccs 11
cts 13
cp 0.8462
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A openFile() 0 17 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
use Sunrise\Http\Message\Exception\InvalidArgumentException;
15
use Sunrise\Http\Message\Stream;
16
use Throwable;
17
18
use function fopen;
19
use function is_resource;
20
use function sprintf;
21
22
final class FileStream extends Stream
23
{
24
    /**
25
     * @throws InvalidArgumentException
26
     */
27 5
    public function __construct(string $filename, string $mode)
28
    {
29 5
        parent::__construct(self::openFile($filename, $mode));
30
    }
31
32
    /**
33
     * @return resource
34
     *
35
     * @throws InvalidArgumentException
36
     */
37 5
    private static function openFile(string $filename, string $mode)
38
    {
39
        try {
40 5
            $resource = @fopen($filename, $mode);
41
        } catch (Throwable $e) {
42
            $resource = false;
43
        }
44
45 5
        if (!is_resource($resource)) {
46 1
            throw new InvalidArgumentException(sprintf(
47 1
                'Unable to open the file "%s" in the mode "%s"',
48 1
                $filename,
49 1
                $mode
50 1
            ));
51
        }
52
53 4
        return $resource;
54
    }
55
}
56