Swift_Message::__wakeup()   A
last analyzed

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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
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 145
    public function __construct($subject = null, $body = null, $contentType = null, $charset = null)
44
    {
45 145
        call_user_func_array(
46 145
            array($this, 'Swift_Mime_SimpleMessage::__construct'),
47 145
            Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message')
48 145
        );
49
50 145
        if (!isset($charset)) {
51 145
            $charset = Swift_DependencyContainer::getInstance()->lookup('properties.charset');
52 145
        }
53
54 145
        $this->setSubject($subject);
55 145
        $this->setBody($body);
56 145
        $this->setCharset($charset);
57 145
        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 145
    }
61
62
    /**
63
     * Create a new Message.
64
     *
65
     * @param string $subject
66
     * @param string $body
67
     * @param string $contentType
68
     * @param string $charset
69
     *
70
     * @return $this
71
     */
72 4
    public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null)
73
    {
74 4
        return new self($subject, $body, $contentType, $charset);
75
    }
76
77
    /**
78
     * Add a MimePart to this Message.
79
     *
80
     * @param string|Swift_OutputByteStream $body
81
     * @param string                        $contentType
82
     * @param string                        $charset
83
     *
84
     * @return $this
85
     */
86 3
    public function addPart($body, $contentType = null, $charset = null)
87
    {
88 3
        return $this->attach(
89 3
            Swift_MimePart::newInstance(
90 3
                $body,
0 ignored issues
show
Bug introduced by
It seems like $body defined by parameter $body on line 86 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...
91 3
                $contentType,
92
                $charset
93 3
            )->setEncoder($this->getEncoder())
94 3
        );
95
    }
96
97
    /**
98
     * Detach a signature handler from a message.
99
     *
100
     * @param Swift_Signer $signer
101
     *
102
     * @return $this
103
     */
104 9
    public function attachSigner(Swift_Signer $signer)
105
    {
106 9
        if ($signer instanceof Swift_Signers_HeaderSigner) {
107 1
            $this->headerSigners[] = $signer;
108 9
        } elseif ($signer instanceof Swift_Signers_BodySigner) {
109 8
            $this->bodySigners[] = $signer;
110 8
        }
111
112 9
        return $this;
113
    }
114
115
    /**
116
     * Attach a new signature handler to the message.
117
     *
118
     * @param Swift_Signer $signer
119
     *
120
     * @return $this
121
     */
122
    public function detachSigner(Swift_Signer $signer)
123
    {
124
        if ($signer instanceof Swift_Signers_HeaderSigner) {
125
            foreach ($this->headerSigners as $k => $headerSigner) {
126
                if ($headerSigner === $signer) {
127
                    unset($this->headerSigners[$k]);
128
129
                    return $this;
130
                }
131
            }
132
        } elseif ($signer instanceof Swift_Signers_BodySigner) {
133
            foreach ($this->bodySigners as $k => $bodySigner) {
134
                if ($bodySigner === $signer) {
135
                    unset($this->bodySigners[$k]);
136
137
                    return $this;
138
                }
139
            }
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * Get this message as a complete string.
147
     *
148
     * @return string
149
     */
150 118 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...
151
    {
152 118
        if (empty($this->headerSigners) && empty($this->bodySigners)) {
153 118
            return parent::toString();
154
        }
155
156
        $this->saveMessage();
157
158
        $this->doSign();
159
160
        $string = parent::toString();
161
162
        $this->restoreMessage();
163
164
        return $string;
165
    }
166
167
    /**
168
     * Write this message to a {@link Swift_InputByteStream}.
169
     *
170
     * @param Swift_InputByteStream $is
171
     */
172 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...
173
    {
174 17
        if (empty($this->headerSigners) && empty($this->bodySigners)) {
175 17
            parent::toByteStream($is);
176
177 17
            return;
178
        }
179
180 8
        $this->saveMessage();
181
182 8
        $this->doSign();
183
184 8
        parent::toByteStream($is);
185
186 8
        $this->restoreMessage();
187 8
    }
188
189
    public function __wakeup()
190
    {
191
        Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.message');
192
    }
193
194
    /**
195
     * loops through signers and apply the signatures.
196
     */
197 8
    protected function doSign()
198
    {
199 8
        foreach ($this->bodySigners as $signer) {
200 8
            $altered = $signer->getAlteredHeaders();
201 8
            $this->saveHeaders($altered);
202 8
            $signer->signMessage($this);
203 8
        }
204
205 8
        foreach ($this->headerSigners as $signer) {
206
            $altered = $signer->getAlteredHeaders();
207
            $this->saveHeaders($altered);
208
            $signer->reset();
209
210
            $signer->setHeaders($this->getHeaders());
211
212
            $signer->startBody();
213
            $this->_bodyToByteStream($signer);
214
            $signer->endBody();
215
216
            $signer->addSignature($this->getHeaders());
217 8
        }
218 8
    }
219
220
    /**
221
     * save the message before any signature is applied.
222
     */
223 13
    protected function saveMessage()
224 5
    {
225 13
        $this->savedMessage = array('headers' => array());
226 8
        $this->savedMessage['body'] = $this->getBody();
227 8
        $this->savedMessage['children'] = $this->getChildren();
228
229
        if (
230 8
            $this->getBody() != ''
231 8
            &&
232 8
            count($this->savedMessage['children']) > 0
233 8
        ) {
234 1
            $this->setChildren(array_merge(array($this->_becomeMimePart()), $this->savedMessage['children']));
235 1
            $this->setBody('');
236 1
        }
237 8
    }
238
239
    /**
240
     * save the original headers.
241
     *
242
     * @param array $altered
243
     */
244 8
    protected function saveHeaders(array $altered)
245
    {
246 8
        foreach ($altered as $head) {
247 8
            $lc = Swift::strtolowerWithStaticCache($head);
248
249 8
            if (!isset($this->savedMessage['headers'][$lc])) {
250 8
                $this->savedMessage['headers'][$lc] = $this->getHeaders()->getAll($head);
251 8
            }
252 8
        }
253 8
    }
254
255
    /**
256
     * Remove or restore altered headers.
257
     */
258 8
    protected function restoreHeaders()
259
    {
260 8
        foreach ($this->savedMessage['headers'] as $name => $savedValue) {
261 8
            $headers = $this->getHeaders()->getAll($name);
262
263 8
            foreach ($headers as $key => $value) {
264 8
                if (!isset($savedValue[$key])) {
265 7
                    $this->getHeaders()->remove($name, $key);
266 7
                }
267 8
            }
268 8
        }
269 8
    }
270
271
    /**
272
     * Restore message body.
273
     */
274 8
    protected function restoreMessage()
275
    {
276 8
        $this->setBody($this->savedMessage['body']);
277 8
        $this->setChildren($this->savedMessage['children']);
278
279 8
        $this->restoreHeaders();
280 8
        $this->savedMessage = array();
281 8
    }
282
283
    /**
284
     * Clone Message Signers.
285
     *
286
     * @see Swift_Mime_SimpleMimeEntity::__clone()
287
     */
288 5
    public function __clone()
289
    {
290 5
        parent::__clone();
291
292 5
        foreach ($this->bodySigners as $key => $bodySigner) {
293
            $this->bodySigners[$key] = clone $bodySigner;
294 5
        }
295
296 5
        foreach ($this->headerSigners as $key => $headerSigner) {
297 1
            $this->headerSigners[$key] = clone $headerSigner;
298 5
        }
299 5
    }
300
}
301