Passed
Pull Request — master (#171)
by Zaahid
03:23
created

MessagePart::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 ZBateson\MailMimeParser\MailMimeParser;
10
use GuzzleHttp\Psr7\StreamWrapper;
11
use GuzzleHttp\Psr7\Utils;
12
use Psr\Http\Message\StreamInterface;
13
use SplObjectStorage;
14
use SplObserver;
15
16
/**
17
 * Most basic representation of a single part of an email.
18
 *
19
 * @author Zaahid Bateson
20
 */
21
abstract class MessagePart implements IMessagePart
22
{
23
    /**
24
     * @var IMimePart parent part
25
     */
26
    protected $parent;
27
28
    /**
29
     * @var PartStreamContainer holds 'stream' and 'content stream'.
30
     */
31
    protected $partStreamContainer;
32
33
    /**
34
     * @var string can be used to set an override for content's charset in cases
35
     *      where a user knows the charset on the content is not what it claims
36
     *      to be.
37
     */
38
    protected $charsetOverride;
39
40
    /**
41
     * @var bool set to true when a user attaches a stream manually, it's
42
     *      assumed to already be decoded or to have relevant transfer encoding
43
     *      decorators attached already.
44
     */
45
    protected $ignoreTransferEncoding;
46
47
    /**
48
     * @var SplObjectStorage attached observers that need to be notified of
49
     *      modifications to this part.
50
     */
51
    protected $observers;
52
53 153
    public function __construct(PartStreamContainer $streamContainer, IMimePart $parent = null)
54
    {
55 153
        $this->partStreamContainer = $streamContainer;
56 153
        $this->parent = $parent;
57 153
        $this->observers = new SplObjectStorage();
58 153
    }
59
60 117
    public function attach(SplObserver $observer)
61
    {
62 117
        $this->observers->attach($observer);
63 117
    }
64
65 3
    public function detach(SplObserver $observer)
66
    {
67 3
        $this->observers->detach($observer);
68 3
    }
69
70 109
    public function notify()
71
    {
72 109
        foreach ($this->observers as $observer) {
73 107
            $observer->update($this);
74
        }
75 109
        if ($this->parent !== null) {
76 69
            $this->parent->notify();
77
        }
78 109
    }
79
80 24
    public function getParent()
81
    {
82 24
        return $this->parent;
83
    }
84
85 110
    public function hasContent()
86
    {
87 110
        return $this->partStreamContainer->hasContent();
88
    }
89
90 1
    public function getFilename()
91
    {
92 1
        return null;
93
    }
94
95 2
    public function setCharsetOverride($charsetOverride, $onlyIfNoCharset = false)
96
    {
97 2
        if (!$onlyIfNoCharset || $this->getCharset() === null) {
98 2
            $this->charsetOverride = $charsetOverride;
99
        }
100 2
    }
101
102 105
    public function getContentStream($charset = MailMimeParser::DEFAULT_CHARSET)
103
    {
104 105
        if ($this->hasContent()) {
105 104
            $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding();
106 104
            $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset();
107 104
            return $this->partStreamContainer->getContentStream(
108 104
                $tr,
109 104
                $ch,
110 104
                $charset
111
            );
112
        }
113 47
        return null;
114
    }
115
116 49
    public function getBinaryContentStream()
117
    {
118 49
        if ($this->hasContent()) {
119 49
            $tr = ($this->ignoreTransferEncoding) ? '' : $this->getContentTransferEncoding();
120 49
            return $this->partStreamContainer->getBinaryContentStream($tr);
121
        }
122
        return null;
123
    }
124
125 46
    public function getBinaryContentResourceHandle()
126
    {
127 46
        $stream = $this->getBinaryContentStream();
128 46
        if ($stream !== null) {
129 46
            return StreamWrapper::getResource($stream);
130
        }
131
        return null;
132
    }
133
134 3
    public function saveContent($filenameResourceOrStream)
135
    {
136 3
        $resourceOrStream = $filenameResourceOrStream;
137 3
        if (is_string($filenameResourceOrStream)) {
138 1
            $resourceOrStream = fopen($filenameResourceOrStream, 'w+');
139
        }
140
141 3
        $stream = Utils::streamFor($resourceOrStream);
142 3
        Utils::copyToStream($this->getBinaryContentStream(), $stream);
0 ignored issues
show
Bug introduced by
It seems like $this->getBinaryContentStream() can also be of type null; however, parameter $source of GuzzleHttp\Psr7\Utils::copyToStream() does only seem to accept Psr\Http\Message\StreamInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

142
        Utils::copyToStream(/** @scrutinizer ignore-type */ $this->getBinaryContentStream(), $stream);
Loading history...
143
144 3
        if (!is_string($filenameResourceOrStream)
145 3
            && !($filenameResourceOrStream instanceof StreamInterface)) {
146
            // only detach if it wasn't a string or StreamInterface, so the
147
            // fopen call can be properly closed if it was
148 1
            $stream->detach();
149
        }
150 3
    }
151
152 25
    public function getContent($charset = MailMimeParser::DEFAULT_CHARSET)
153
    {
154 25
        $stream = $this->getContentStream($charset);
155 25
        if ($stream !== null) {
156 24
            return $stream->getContents();
157
        }
158 1
        return null;
159
    }
160
161 22
    public function attachContentStream(StreamInterface $stream, $streamCharset = MailMimeParser::DEFAULT_CHARSET)
162
    {
163 22
        $ch = ($this->charsetOverride !== null) ? $this->charsetOverride : $this->getCharset();
164 22
        if ($ch !== null && $streamCharset !== $ch) {
165 9
            $this->charsetOverride = $streamCharset;
166
        }
167 22
        $this->ignoreTransferEncoding = true;
168 22
        $this->partStreamContainer->setContentStream($stream);
169 22
        $this->notify();
170 22
    }
171
172 19
    public function detachContentStream()
173
    {
174 19
        $this->partStreamContainer->setContentStream(null);
175 19
        $this->notify();
176 19
    }
177
178 17
    public function setContent($resource, $charset = MailMimeParser::DEFAULT_CHARSET)
179
    {
180 17
        $stream = Utils::streamFor($resource);
181 17
        $this->attachContentStream($stream, $charset);
182
        // this->notify() called in attachContentStream
183 17
    }
184
185 1
    public function getResourceHandle()
186
    {
187 1
        return StreamWrapper::getResource($this->getStream());
188
    }
189
190 102
    public function getStream()
191
    {
192 102
        return $this->partStreamContainer->getStream();
193
    }
194
195 95
    public function save($filenameResourceOrStream, $filemode = 'w+')
196
    {
197 95
        $resourceOrStream = $filenameResourceOrStream;
198 95
        if (is_string($filenameResourceOrStream)) {
199 1
            $resourceOrStream = fopen($filenameResourceOrStream, $filemode);
200
        }
201
202 95
        $partStream = $this->getStream();
203 95
        $partStream->rewind();
204 95
        $stream = Utils::streamFor($resourceOrStream);
205 95
        Utils::copyToStream($partStream, $stream);
206
207 95
        if (!is_string($filenameResourceOrStream)
208 95
            && !($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 94
            $stream->detach();
212
        }
213 95
    }
214
215 1
    public function __toString()
216
    {
217 1
        return $this->getStream()->getContents();
218
    }
219
}
220