Completed
Push — 5.x ( b7e0f5...831ff0 )
by Lars
16:50 queued 10:32
created

Swift_Message::setUseMemorySpool()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * The Message class for building emails.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Message extends Swift_Mime_SimpleMessage
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * @var Swift_Signers_HeaderSigner[]
20
     */
21
    private $headerSigners = array();
22
23
    /**
24
     * @var Swift_Signers_BodySigner[]
25
     */
26
    private $bodySigners = array();
27
28
    /**
29
     * @var array
30
     */
31
    private $savedMessage = array();
32
33
    /**
34
     * Create a new Message.
35
     *
36
     * Details may be optionally passed into the constructor.
37
     *
38
     * @param string $subject
39
     * @param string $body
40
     * @param string $contentType
41
     * @param string $charset
42
     */
43 109
    public function __construct($subject = null, $body = null, $contentType = null, $charset = null)
44
    {
45 109
        call_user_func_array(
46 109
            array($this, 'Swift_Mime_SimpleMessage::__construct'),
47 109
            Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message')
48 109
        );
49
50 109
        if (!isset($charset)) {
51 109
            $charset = Swift_DependencyContainer::getInstance()->lookup('properties.charset');
52 109
        }
53
54 109
        $this->setSubject($subject);
55 109
        $this->setBody($body);
56 109
        $this->setCharset($charset);
57 109
        if ($contentType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $contentType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58 10
            $this->setContentType($contentType);
59 10
        }
60 109
    }
61
62
    /**
63
     * use this only if you use the memory-spool and
64
     * you need to generate a message, before you define the transport
65
     *
66
     * @param bool $bool
67
     */
68 1
    public function setUseMemorySpool($bool)
69
    {
70 1
        if (is_bool($bool)) {
71 1
            Swift::$useMemorySpool = $bool;
72 1
        }
73 1
    }
74
75
    /**
76
     * Create a new Message.
77
     *
78
     * @param string $subject
79
     * @param string $body
80
     * @param string $contentType
81
     * @param string $charset
82
     *
83
     * @return Swift_Message
84
     */
85 59
    public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null)
86
    {
87 59
        return new self($subject, $body, $contentType, $charset);
88
    }
89
90
    /**
91
     * Add a MimePart to this Message.
92
     *
93
     * @param string|Swift_OutputByteStream $body
94
     * @param string                        $contentType
95
     * @param string                        $charset
96
     *
97
     * @return Swift_Mime_SimpleMessage
98
     */
99 3
    public function addPart($body, $contentType = null, $charset = null)
100
    {
101 3
        return $this->attach(
102 3
            Swift_MimePart::newInstance(
103 3
                $body,
0 ignored issues
show
Bug introduced by
It seems like $body defined by parameter $body on line 99 can also be of type object<Swift_OutputByteStream>; however, Swift_MimePart::newInstance() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104 3
                $contentType,
105
                $charset
106 3
            )
107 3
        );
108
    }
109
110
    /**
111
     * Attach a new signature handler to the message.
112
     *
113
     * @param Swift_Signer $signer
114
     *
115
     * @return Swift_Message
116
     */
117 9
    public function attachSigner(Swift_Signer $signer)
118
    {
119 9
        if ($signer instanceof Swift_Signers_HeaderSigner) {
120 1
            $this->headerSigners[] = $signer;
121 9
        } elseif ($signer instanceof Swift_Signers_BodySigner) {
122 8
            $this->bodySigners[] = $signer;
123 8
        }
124
125 9
        return $this;
126
    }
127
128
    /**
129
     * Attach a new signature handler to the message.
130
     *
131
     * @param Swift_Signer $signer
132
     *
133
     * @return Swift_Message
134
     */
135
    public function detachSigner(Swift_Signer $signer)
136
    {
137
        if ($signer instanceof Swift_Signers_HeaderSigner) {
138
            foreach ($this->headerSigners as $k => $headerSigner) {
139
                if ($headerSigner === $signer) {
140
                    unset($this->headerSigners[$k]);
141
142
                    return $this;
143
                }
144
            }
145
        } elseif ($signer instanceof Swift_Signers_BodySigner) {
146
            foreach ($this->bodySigners as $k => $bodySigner) {
147
                if ($bodySigner === $signer) {
148
                    unset($this->bodySigners[$k]);
149
150
                    return $this;
151
                }
152
            }
153
        }
154
155
        return $this;
156
    }
157
158
    /**
159
     * Get this message as a complete string.
160
     *
161
     * @return string
162
     */
163 82 View Code Duplication
    public function toString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165 82
        if (empty($this->headerSigners) && empty($this->bodySigners)) {
166 82
            return parent::toString();
167
        }
168
169
        $this->saveMessage();
170
171
        $this->doSign();
172
173
        $string = parent::toString();
174
175
        $this->restoreMessage();
176
177
        return $string;
178
    }
179
180
    /**
181
     * Write this message to a {@link Swift_InputByteStream}.
182
     *
183
     * @param Swift_InputByteStream $is
184
     */
185 17 View Code Duplication
    public function toByteStream(Swift_InputByteStream $is)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187 17
        if (empty($this->headerSigners) && empty($this->bodySigners)) {
188 17
            parent::toByteStream($is);
189
190 17
            return;
191
        }
192
193 8
        $this->saveMessage();
194
195 8
        $this->doSign();
196
197 8
        parent::toByteStream($is);
198
199 8
        $this->restoreMessage();
200 8
    }
201
202
    public function __wakeup()
203
    {
204
        Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
205
    }
206
207
    /**
208
     * loops through signers and apply the signatures.
209
     */
210 8
    protected function doSign()
211
    {
212 8
        foreach ($this->bodySigners as $signer) {
213 8
            $altered = $signer->getAlteredHeaders();
214 8
            $this->saveHeaders($altered);
215 8
            $signer->signMessage($this);
216 8
        }
217
218 8
        foreach ($this->headerSigners as $signer) {
219
            $altered = $signer->getAlteredHeaders();
220
            $this->saveHeaders($altered);
221
            $signer->reset();
222
223
            $signer->setHeaders($this->getHeaders());
224
225
            $signer->startBody();
226
            $this->_bodyToByteStream($signer);
227
            $signer->endBody();
228
229
            $signer->addSignature($this->getHeaders());
230 8
        }
231 8
    }
232
233
    /**
234
     * save the message before any signature is applied.
235
     */
236 8
    protected function saveMessage()
237
    {
238 8
        $this->savedMessage = array('headers' => array());
239 8
        $this->savedMessage['body'] = $this->getBody();
240 8
        $this->savedMessage['children'] = $this->getChildren();
241
242
        if (
243 8
            count($this->savedMessage['children']) > 0
244 8
            &&
245 1
            $this->getBody() != ''
246 8
        ) {
247 1
            $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children']));
248 1
            $this->setBody('');
249 1
        }
250 8
    }
251
252
    /**
253
     * save the original headers.
254
     *
255
     * @param array $altered
256
     */
257 8
    protected function saveHeaders(array $altered)
258
    {
259 8
        foreach ($altered as $head) {
260 8
            $lc = Swift::strtolowerWithStaticCache($head);
261
262 8
            if (!isset($this->savedMessage['headers'][$lc])) {
263 8
                $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
264 8
            }
265 8
        }
266 8
    }
267
268
    /**
269
     * Remove or restore altered headers.
270
     */
271 8
    protected function restoreHeaders()
272
    {
273 8
        foreach ($this->savedMessage['headers'] as $name => $savedValue) {
274 8
            $headers = $this->getHeaders()->getAll($name);
275
276 8
            foreach ($headers as $key => $value) {
277 8
                if (!isset($savedValue[$key])) {
278 4
                    $this->getHeaders()->remove($name, $key);
279 4
                }
280 8
            }
281 8
        }
282 8
    }
283
284
    /**
285
     * Restore message body.
286
     */
287 8
    protected function restoreMessage()
288
    {
289 8
        $this->setBody($this->savedMessage['body']);
290 8
        $this->setChildren($this->savedMessage['children']);
291
292 8
        $this->restoreHeaders();
293 8
        $this->savedMessage = array();
294 8
    }
295
296
    /**
297
     * Clone Message Signers.
298
     *
299
     * @see Swift_Mime_SimpleMimeEntity::__clone()
300
     */
301 5
    public function __clone()
302
    {
303 5
        if (true === Swift::$useMemorySpool) {
304 4
            parent::__clone();
305
306 4
            foreach ($this->bodySigners as $key => $bodySigner) {
307
                $this->bodySigners[$key] = clone($bodySigner);
308 4
            }
309
310 4
            foreach ($this->headerSigners as $key => $headerSigner) {
311 1
                $this->headerSigners[$key] = clone($headerSigner);
312 4
            }
313 4
        }
314 5
    }
315
}
316