Test Failed
Pull Request — master (#171)
by Zaahid
04:55
created

MessagePart::hasContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message;
8
9
use GuzzleHttp\Psr7;
10
use GuzzleHttp\Psr7\StreamWrapper;
11
use Psr\Http\Message\StreamInterface;
12
use ZBateson\MailMimeParser\MailMimeParser;
13
use SplObjectStorage;
14
use SplObserver;
15
16
/**
17
 * Implements IMessagePart with a few concrete methods that can have reasonable
18
 * defaults.
19
 *
20
 * @author Zaahid Bateson
21
 */
22
abstract class MessagePart implements IMessagePart
23
{
24
    /**
25
     * @var IMimePart parent part
26
     */
27
    protected $parent;
28
29
    /**
30
     * @var PartStreamContainer holds 'stream' and 'contentStream'.
31
     */
32
    protected $partStreamContainer;
33
34
    /**
35
     * @var string can be used to set an override for content's charset in cases
36
     *      where a user knows the charset on the content is not what it claims
37
     *      to be.
38
     */
39
    protected $charsetOverride;
40
41
    /**
42
     * @var bool set to true when a user attaches a stream manually, it's
43
     *      assumed to already be decoded or to have relevant transfer encoding
44
     *      decorators attached already.
45
     */
46
    protected $ignoreTransferEncoding;
47
48
    /**
49
     * SplObjectStorage attached observers
50
     */
51
    protected $observers;
52
53
    public function __construct(PartStreamContainer $streamContainer, IMimePart $parent = null)
54
    {
55
        $this->partStreamContainer = $streamContainer;
56
        $this->parent = $parent;
57
        $this->observers = new SplObjectStorage();
58
    }
59
60
    public function attach(SplObserver $observer)
61
    {
62
        $this->observers->attach($observer);
63
    }
64
65
    public function detach(SplObserver $observer)
66
    {
67
        $this->observers->detach($observer);
68
    }
69
70
    public function notify()
71
    {
72
        foreach ($this->observers as $observer) {
73
            $observer->update($this);
74
        }
75
        if ($this->parent !== null) {
76
            $this->parent->notify();
77
        }
78
    }
79
80
    public function hasContent()
81
    {
82
        return $this->partStreamContainer->hasContent();
83
    }
84
85
    public function getFilename()
86
    {
87
        return null;
88
    }
89
90
    public function getResourceHandle()
91
    {
92
        return StreamWrapper::getResource($this->getStream());
93
    }
94
95
    public function getStream()
96
    {
97
        return $this->partStreamContainer->getStream();
98
    }
99
100
    public function setCharsetOverride($charsetOverride, $onlyIfNoCharset = false)
101
    {
102
        if (!$onlyIfNoCharset || $this->getCharset() === null) {
103
            $this->charsetOverride = $charsetOverride;
104
        }
105
    }
106
107
    public function getContentStream($charset = MailMimeParser::DEFAULT_CHARSET)
108
    {
109
        if ($this->hasContent()) {
110
            $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding();
111
            $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset();
112
            return $this->partStreamContainer->getContentStream(
113
                $tr,
114
                $ch,
115
                $charset
116
            );
117
        }
118
        return null;
119
    }
120
121
    public function getBinaryContentStream()
122
    {
123
        if ($this->hasContent()) {
124
            $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding();
125
            return $this->partStreamContainer->getBinaryContentStream($tr);
126
        }
127
        return null;
128
    }
129
130
    public function getBinaryContentResourceHandle()
131
    {
132
        $stream = $this->getBinaryContentStream();
133
        if ($stream !== null) {
134
            return StreamWrapper::getResource($stream);
135
        }
136
        return null;
137
    }
138
139
    public function saveContent($filenameResourceOrStream)
140
    {
141
        $resourceOrStream = $filenameResourceOrStream;
142
        if (is_string($filenameResourceOrStream)) {
143
            $resourceOrStream = fopen($filenameResourceOrStream, 'w+');
144
        }
145
146
        $stream = Psr7\stream_for($resourceOrStream);
0 ignored issues
show
Bug introduced by
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for($resourceOrStream);
Loading history...
147
        Psr7\copy_to_stream($this->getBinaryContentStream(), $stream);
0 ignored issues
show
Bug introduced by
The function copy_to_stream was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        /** @scrutinizer ignore-call */ 
148
        Psr7\copy_to_stream($this->getBinaryContentStream(), $stream);
Loading history...
148
149
        if (!is_string($filenameResourceOrStream)
150
            && !($filenameResourceOrStream instanceof StreamInterface)) {
151
            // only detach if it wasn't a string or StreamInterface, so the
152
            // fopen call can be properly closed if it was
153
            $stream->detach();
154
        }
155
    }
156
157
    public function getContent($charset = MailMimeParser::DEFAULT_CHARSET)
158
    {
159
        $stream = $this->getContentStream($charset);
160
        if ($stream !== null) {
161
            return $stream->getContents();
162
        }
163
        return null;
164
    }
165
166
    public function getParent()
167
    {
168
        return $this->parent;
169
    }
170
171
    public function attachContentStream(StreamInterface $stream, $streamCharset = MailMimeParser::DEFAULT_CHARSET)
172
    {
173
        $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset();
174
        if ($ch !== null && $streamCharset !== $ch) {
175
            $this->charsetOverride = $streamCharset;
176
        }
177
        $this->ignoreTransferEncoding = true;
178
        $this->partStreamContainer->setContentStream($stream);
179
        $this->notify();
180
    }
181
182
    public function detachContentStream()
183
    {
184
        $this->partStreamContainer->setContentStream(null);
185
        $this->notify();
186
    }
187
188
    public function setContent($resource, $charset = MailMimeParser::DEFAULT_CHARSET)
189
    {
190
        $stream = Psr7\stream_for($resource);
0 ignored issues
show
Bug introduced by
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for($resource);
Loading history...
191
        $this->attachContentStream($stream, $charset);
192
        // this->notify() called in attachContentStream
193
    }
194
195
    public function save($filenameResourceOrStream)
196
    {
197
        $resourceOrStream = $filenameResourceOrStream;
198
        if (is_string($filenameResourceOrStream)) {
199
            $resourceOrStream = fopen($filenameResourceOrStream, 'w+');
200
        }
201
202
        $partStream = $this->getStream();
203
        $partStream->rewind();
204
        $stream = Psr7\stream_for($resourceOrStream);
0 ignored issues
show
Bug introduced by
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
        $stream = /** @scrutinizer ignore-call */ Psr7\stream_for($resourceOrStream);
Loading history...
205
        Psr7\copy_to_stream($partStream, $stream);
0 ignored issues
show
Bug introduced by
The function copy_to_stream was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
        /** @scrutinizer ignore-call */ 
206
        Psr7\copy_to_stream($partStream, $stream);
Loading history...
206
207
        if (!is_string($filenameResourceOrStream)
208
            && !($filenameResourceOrStream instanceof StreamInterface)) {
209
            // only detach if it wasn't a string or StreamInterface, so the
210
            // fopen call can be properly closed if it was
211
            $stream->detach();
212
        }
213
    }
214
215
    public function __toString()
216
    {
217
        return $this->getStream()->getContents();
218
    }
219
}
220