FileStream   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of slick/http
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slick\Http\Message\Stream;
13
14
use Psr\Http\Message\StreamInterface;
15
use Slick\Http\Message\Exception\InvalidArgumentException;
16
17
/**
18
 * File Stream
19
 *
20
 * @package Slick\Http\Message\Stream
21
 */
22
class FileStream extends AbstractStream implements StreamInterface
23
{
24
    /**
25
     * Creates a File Stream
26
     *
27
     * @param string $file The file FQ name to create the stream from
28
     *
29
     * @throws InvalidArgumentException If provided file does not exists
30
     */
31
    public function __construct($file)
32
    {
33
        if (!filter_var($file, FILTER_VALIDATE_URL) && !is_file($file)) {
34
            throw new InvalidArgumentException(
35
                "Cannot create stream: given file is not found."
36
            );
37
        }
38
39
        $string = fopen($file, 'r');
40
        if ($string === false) {
41
            throw new InvalidArgumentException(
42
                "Cannot create stream: could not open file."
43
            );
44
        }
45
46
        $this->stream = $string;
47
    }
48
}
49