LazyOpenStream::createStream()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thruster\Component\HttpMessage;
4
5
use Psr\Http\Message\StreamInterface;
6
7
/**
8
 * Class LazyOpenStream
9
 *
10
 * @package Thruster\Component\HttpMessage
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class LazyOpenStream implements StreamInterface
14
{
15
    use StreamDecoratorTrait;
16
17
    /**
18
     * @var string File to open
19
     */
20
    private $filename;
21
22
    /**
23
     * @var string $mode
24
     */
25
    private $mode;
26
27
    /**
28
     * @param string $filename File to lazily open
29
     * @param string $mode     fopen mode to use when opening the stream
30
     */
31 8
    public function __construct(string $filename, string $mode)
32
    {
33 8
        $this->filename = $filename;
34 8
        $this->mode = $mode;
35 8
    }
36
37
    /**
38
     * Creates the underlying stream lazily when required.
39
     *
40
     * @return StreamInterface
41
     */
42 7
    protected function createStream()
43
    {
44 7
        return stream_for(try_fopen($this->filename, $this->mode));
45
    }
46
}
47