Completed
Pull Request — master (#12)
by Frederik
12:11
created

LazyStream::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Stream;
5
6
use Genkgo\Mail\StreamInterface;
7
8
final class LazyStream implements StreamInterface
9
{
10
    /**
11
     * @var \Closure
12
     */
13
    private $callback;
14
    /**
15
     * @var StreamInterface
16
     */
17
    private $decoratedStream;
18
19
    /**
20
     * LazyStream constructor.
21
     * @param \Closure $callback
22
     */
23 22
    public function __construct(\Closure $callback)
24
    {
25 22
        $this->callback = $callback;
26 22
    }
27
28
    /**
29
     * @return string
30
     */
31 21
    public function __toString(): string
32
    {
33 21
        return $this->getStream()->__toString();
34
    }
35
36
    /**
37
     *
38
     */
39
    public function close(): void
40
    {
41
        $this->getStream()->close();
42
    }
43
44
    /**
45
     * @return mixed
46
     */
47
    public function detach()
48
    {
49
        return $this->getStream()->detach();
50
    }
51
52
    /**
53
     * @return int|null
54
     */
55
    public function getSize(): ?int
56
    {
57
        return $this->getStream()->getSize();
58
    }
59
60
    /**
61
     * @return int
62
     * @throws \RuntimeException
63
     */
64
    public function tell(): int
65
    {
66
        return $this->getStream()->tell();
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 1
    public function eof(): bool
73
    {
74 1
        return $this->getStream()->eof();
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isSeekable(): bool
81
    {
82
        return $this->getStream()->isSeekable();
83
    }
84
85
    /**
86
     * @param int $offset
87
     * @param int $whence
88
     * @return int
89
     */
90
    public function seek(int $offset, int $whence = SEEK_SET): int
91
    {
92
        return $this->getStream()->seek($offset, $whence);
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function rewind(): bool
99
    {
100
        return $this->getStream()->rewind();
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function isWritable(): bool
107
    {
108
        return false;
109
    }
110
111
    /**
112
     * @param $string
113
     * @return int
114
     */
115
    public function write($string): int
116
    {
117
        throw new \RuntimeException('Cannot write to stream');
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function isReadable(): bool
124
    {
125
        return true;
126
    }
127
128
    /**
129
     * @param int $length
130
     * @return string
131
     */
132 1
    public function read(int $length): string
133
    {
134 1
        return $this->getStream()->read($length);
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getContents(): string
141
    {
142
        return $this->getStream()->getContents();
143
    }
144
145
    /**
146
     * @param array $keys
147
     * @return array
148
     */
149
    public function getMetadata(array $keys = []): array
150
    {
151
        return $this->getStream()->getMetadata();
152
    }
153
154
    /**
155
     * @return StreamInterface
156
     */
157 22
    private function getStream(): StreamInterface
158
    {
159 22
        if ($this->decoratedStream === null) {
160 22
            $callback = $this->callback;
161 22
            $this->decoratedStream = $callback();
162
        }
163
164 22
        return $this->decoratedStream;
165
    }
166
}