LazyOpenStream   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 1
cbo 1
dl 0
loc 34
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createStream() 0 4 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