Completed
Pull Request — master (#18)
by Frederik
01:29
created

MessageStream   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 53.7%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 172
c 0
b 0
f 0
ccs 29
cts 54
cp 0.537
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A close() 0 4 1
A detach() 0 4 1
A getSize() 0 4 1
A tell() 0 4 1
A eof() 0 4 1
A isSeekable() 0 4 1
A seek() 0 4 1
A rewind() 0 4 1
A isWritable() 0 4 1
A write() 0 4 1
A isReadable() 0 4 1
A read() 0 4 1
A getContents() 0 4 1
A getMetadata() 0 4 1
B __construct() 0 34 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Genkgo\Mail\Stream;
5
6
use Genkgo\Mail\Header\HeaderLine;
7
use Genkgo\Mail\HeaderInterface;
8
use Genkgo\Mail\MessageInterface;
9
use Genkgo\Mail\StreamInterface;
10
11
final class MessageStream implements StreamInterface
12
{
13
    /**
14
     * @var StreamInterface
15
     */
16
    private $decoratedStream;
17
18
    /**
19
     * MessageStream constructor.
20
     * @param MessageInterface $message
21
     */
22
    public function __construct(MessageInterface $message)
23
    {
24 22
        $this->decoratedStream = new LazyStream(function () use ($message) {
25 22
            $headerString = implode(
26 22
                "\r\n",
27 22
                array_values(
28 22
                    array_filter(
29 22
                        array_map(
30 22
                            function (array $headers) {
31 22
                                return implode(
32 22
                                    "\r\n",
33 22
                                    array_map(
34 22
                                        function (HeaderInterface $header) {
35 22
                                            return (string) (new HeaderLine($header));
36 22
                                        },
37 22
                                        $headers
38
                                    )
39
                                );
40 22
                            },
41 22
                            $message->getHeaders()
42
                        )
43
                    )
44
                )
45
            );
46
47 22
            return new ConcatenatedStream(
48 22
                new \ArrayObject([
0 ignored issues
show
Documentation introduced by
new \ArrayObject(array(n..., $message->getBody())) is of type object<ArrayObject>, but the function expects a object<Genkgo\Mail\Stream\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49 22
                    new StringStream($headerString),
50 22
                    new StringStream("\r\n\r\n"),
51 22
                    $message->getBody()
52
                ])
53
            );
54 22
        });
55 22
    }
56
57
    /**
58
     * @return string
59
     */
60 21
    public function __toString(): string
61
    {
62 21
        return $this->decoratedStream->__toString();
63
    }
64
65
    /**
66
     *
67
     */
68
    public function close(): void
69
    {
70
        $this->decoratedStream->close();
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function detach()
77
    {
78
        return $this->decoratedStream->detach();
79
    }
80
81
    /**
82
     * @return int|null
83
     */
84
    public function getSize(): ?int
85
    {
86
        return $this->decoratedStream->getSize();
87
    }
88
89
    /**
90
     * @return int
91
     * @throws \RuntimeException
92
     */
93
    public function tell(): int
94
    {
95
        return $this->decoratedStream->tell();
96
    }
97
98
    /**
99
     * @return bool
100
     */
101 1
    public function eof(): bool
102
    {
103 1
        return $this->decoratedStream->eof();
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isSeekable(): bool
110
    {
111
        return $this->decoratedStream->isSeekable();
112
    }
113
114
    /**
115
     * @param int $offset
116
     * @param int $whence
117
     * @return int
118
     */
119
    public function seek(int $offset, int $whence = SEEK_SET): int
120
    {
121
        return $this->decoratedStream->seek($offset, $whence);
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function rewind(): bool
128
    {
129
        return $this->decoratedStream->rewind();
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isWritable(): bool
136
    {
137
        return false;
138
    }
139
140
    /**
141
     * @param $string
142
     * @return int
143
     */
144
    public function write($string): int
145
    {
146
        throw new \RuntimeException('Cannot write to stream');
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function isReadable(): bool
153
    {
154
        return true;
155
    }
156
157
    /**
158
     * @param int $length
159
     * @return string
160
     */
161 1
    public function read(int $length): string
162
    {
163 1
        return $this->decoratedStream->read($length);
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getContents(): string
170
    {
171
        return $this->decoratedStream->getContents();
172
    }
173
174
    /**
175
     * @param array $keys
176
     * @return array
177
     */
178
    public function getMetadata(array $keys = []): array
179
    {
180
        return $this->decoratedStream->getMetadata();
181
    }
182
}