Passed
Push — master ( 8fd9a3...01022c )
by Aimeos
17:44 queued 10:06
created

Typo3::addAttachment()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 23
rs 9.6111
cc 5
nc 4
nop 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2014-2021
7
 * @package MW
8
 * @subpackage Mail
9
 */
10
11
12
namespace Aimeos\MW\Mail\Message;
13
14
15
/**
16
 * Zend implementation for creating e-mails.
17
 *
18
 * @package MW
19
 * @subpackage Mail
20
 */
21
class Typo3 implements \Aimeos\MW\Mail\Message\Iface
22
{
23
	private $charset;
24
	private $object;
25
26
27
	/**
28
	 * Initializes the message instance.
29
	 *
30
	 * @param \TYPO3\CMS\Core\Mail\MailMessage $object TYPO3 mail object
31
	 * @param string $charset Default charset of the message
32
	 */
33
	public function __construct( \TYPO3\CMS\Core\Mail\MailMessage $object, string $charset )
34
	{
35
		$this->charset = $charset;
36
		$this->object = $object;
37
	}
38
39
40
	/**
41
	 * Adds a source e-mail address of the message.
42
	 *
43
	 * @param string $email Source e-mail address
44
	 * @param string|null $name Name of the user sending the e-mail or null for no name
45
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
46
	 */
47
	public function addFrom( string $email, string $name = null ) : Iface
48
	{
49
		if( $email )
50
		{
51
			$class = '\Symfony\Component\Mime\Email';
52
53
			if( class_exists( $class ) && $this->object instanceof $class ) {
54
				$this->object->addFrom( new \Symfony\Component\Mime\Address( $email, (string) $name ) );
55
			} else {
56
				$this->object->addFrom( $email, $name );
57
			}
58
		}
59
60
		return $this;
61
	}
62
63
64
	/**
65
	 * Adds a destination e-mail address of the target user mailbox.
66
	 *
67
	 * @param string $email Destination address of the target mailbox
68
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
69
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
70
	 */
71
	public function addTo( string $email, string $name = null ) : Iface
72
	{
73
		if( $email )
74
		{
75
			$class = '\Symfony\Component\Mime\Email';
76
77
			if( class_exists( $class ) && $this->object instanceof $class ) {
78
				$this->object->addTo( new \Symfony\Component\Mime\Address( $email, (string) $name ) );
79
			} else {
80
				$this->object->addTo( $email, $name );
81
			}
82
		}
83
84
		return $this;
85
	}
86
87
88
	/**
89
	 * Adds a destination e-mail address for a copy of the message.
90
	 *
91
	 * @param string $email Destination address for a copy
92
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
93
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
94
	 */
95
	public function addCc( string $email, string $name = null ) : Iface
96
	{
97
		if( $email )
98
		{
99
			$class = '\Symfony\Component\Mime\Email';
100
101
			if( class_exists( $class ) && $this->object instanceof $class ) {
102
				$this->object->addCc( new \Symfony\Component\Mime\Address( $email, (string) $name ) );
103
			} else {
104
				$this->object->addCc( $email, $name );
105
			}
106
		}
107
108
		return $this;
109
	}
110
111
112
	/**
113
	 * Adds a destination e-mail address for a hidden copy of the message.
114
	 *
115
	 * @param string $email Destination address for a hidden copy
116
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
117
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
118
	 */
119
	public function addBcc( string $email, string $name = null ) : Iface
120
	{
121
		if( $email )
122
		{
123
			$class = '\Symfony\Component\Mime\Email';
124
125
			if( class_exists( $class ) && $this->object instanceof $class ) {
126
				$this->object->addBcc( new \Symfony\Component\Mime\Address( $email, (string) $name ) );
127
			} else {
128
				$this->object->addBcc( $email, $name );
129
			}
130
		}
131
132
		return $this;
133
	}
134
135
136
	/**
137
	 * Adds the return e-mail address for the message.
138
	 *
139
	 * @param string $email E-mail address which should receive all replies
140
	 * @param string|null $name Name of the user which should receive all replies or null for no name
141
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
142
	 */
143
	public function addReplyTo( string $email, string $name = null ) : Iface
144
	{
145
		if( $email )
146
		{
147
			$class = '\Symfony\Component\Mime\Email';
148
149
			if( class_exists( $class ) && $this->object instanceof $class ) {
150
				$this->object->addReplyTo( new \Symfony\Component\Mime\Address( $email, (string) $name ) );
151
			} else {
152
				$this->object->addReplyTo( $email, $name );
153
			}
154
		}
155
156
		return $this;
157
	}
158
159
160
	/**
161
	 * Adds a custom header to the message.
162
	 *
163
	 * @param string $name Name of the custom e-mail header
164
	 * @param string $value Text content of the custom e-mail header
165
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
166
	 */
167
	public function addHeader( string $name, string $value ) : Iface
168
	{
169
		if( $name )
170
		{
171
			$class = '\Symfony\Component\Mime\Email';
172
173
			if( class_exists( $class ) && $this->object instanceof $class ) {
174
				$this->object->getHeaders()->add( new \Symfony\Component\Mime\Header\UnstructuredHeader( $name, $value ) );
175
			} else {
176
				$this->object->getHeaders()->addTextHeader( $name, $value );
177
			}
178
		}
179
180
		return $this;
181
	}
182
183
184
	/**
185
	 * Sends the e-mail message to the mail server.
186
	 *
187
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
188
	 */
189
	public function send() : Iface
190
	{
191
		$this->object->send();
192
		return $this;
193
	}
194
195
196
	/**
197
	 * Sets the e-mail address and name of the sender of the message (higher precedence than "From").
198
	 *
199
	 * @param string $email Source e-mail address
200
	 * @param string|null $name Name of the user who sent the message or null for no name
201
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
202
	 */
203
	public function setSender( string $email, string $name = null ) : Iface
204
	{
205
		if( $email )
206
		{
207
			$class = '\Symfony\Component\Mime\Email';
208
209
			if( class_exists( $class ) && $this->object instanceof $class ) {
210
				$this->object->setSender( new \Symfony\Component\Mime\Address( $email, (string) $name ) );
0 ignored issues
show
Bug introduced by
new Symfony\Component\Mi...($email, (string)$name) of type Symfony\Component\Mime\Address is incompatible with the type string expected by parameter $address of TYPO3\CMS\Core\Mail\MailMessage::setSender(). ( Ignorable by Annotation )

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

210
				$this->object->setSender( /** @scrutinizer ignore-type */ new \Symfony\Component\Mime\Address( $email, (string) $name ) );
Loading history...
211
			} else {
212
				$this->object->setSender( $email, $name );
213
			}
214
		}
215
216
		return $this;
217
	}
218
219
220
	/**
221
	 * Sets the subject of the message.
222
	 *
223
	 * @param string $subject Subject of the message
224
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
225
	 */
226
	public function setSubject( string $subject ) : Iface
227
	{
228
		if( $subject )
229
		{
230
			$class = '\Symfony\Component\Mime\Email';
231
232
			if( class_exists( $class ) && $this->object instanceof $class ) {
233
				$this->object->setSubject( $subject );
234
			} else {
235
				$this->object->setSubject( $subject );
236
			}
237
		}
238
239
		return $this;
240
	}
241
242
243
	/**
244
	 * Sets the text body of the message.
245
	 *
246
	 * @param string $message Text body of the message
247
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
248
	 */
249
	public function setBody( string $message ) : Iface
250
	{
251
		if( $message )
252
		{
253
			$class = '\Symfony\Component\Mime\Email';
254
255
			if( class_exists( $class ) && $this->object instanceof $class ) {
256
				$this->object->text( $message, $this->charset );
257
			} elseif( class_exists( '\Swift_Mailer' ) ) {
258
				$this->object->setBody( $message );
0 ignored issues
show
Bug introduced by
$message of type string is incompatible with the type Symfony\Component\Mime\Part\AbstractPart|null expected by parameter $body of Symfony\Component\Mime\Message::setBody(). ( Ignorable by Annotation )

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

258
				$this->object->setBody( /** @scrutinizer ignore-type */ $message );
Loading history...
259
			}
260
		}
261
262
		return $this;
263
	}
264
265
266
	/**
267
	 * Sets the HTML body of the message.
268
	 *
269
	 * @param string $message HTML body of the message
270
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
271
	 */
272
	public function setBodyHtml( string $message ) : Iface
273
	{
274
		if( $message )
275
		{
276
			$class = '\Symfony\Component\Mime\Email';
277
278
			if( class_exists( $class ) && $this->object instanceof $class ) {
279
				$this->object->html( $message, $this->charset );
280
			} elseif( class_exists( '\Swift_Mailer' ) ) {
281
				$this->object->addPart( $message, 'text/html' );
0 ignored issues
show
Bug introduced by
The method addPart() does not exist on TYPO3\CMS\Core\Mail\MailMessage. ( Ignorable by Annotation )

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

281
				$this->object->/** @scrutinizer ignore-call */ 
282
                   addPart( $message, 'text/html' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
282
			}
283
		}
284
285
		return $this;
286
	}
287
288
289
	/**
290
	 * Adds an attachment to the message.
291
	 *
292
	 * @param string $data Binary or string
293
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
294
	 * @param string|null $filename Name of the attached file (or null if inline disposition is used)
295
	 * @param string $disposition Type of the disposition ("attachment" or "inline")
296
	 * @return \Aimeos\MW\Mail\Message\Iface Message object
297
	 */
298
	public function addAttachment( string $data, string $mimetype, string $filename, string $disposition = 'attachment' ) : Iface
299
	{
300
		if( $data )
301
		{
302
			$class = '\Symfony\Component\Mime\Email';
303
304
			if( class_exists( $class ) && $this->object instanceof $class )
305
			{
306
				$this->object->attach( $data, $filename, $mimetype );
307
			}
308
			elseif( class_exists( '\Swift_Attachment' ) )
309
			{
310
				$part = \Swift_Attachment::newInstance( $data, $filename, $mimetype );
0 ignored issues
show
Bug introduced by
The type Swift_Attachment was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
311
				$part->setDisposition( $disposition );
312
				$this->object->attach( $part );
313
			}
314
			else
315
			{
316
				throw new \RuntimeException( 'Symfony mailer or Swiftmailer package missing' );
317
			}
318
		}
319
320
		return $this;
321
	}
322
323
324
	/**
325
	 * Embeds an attachment into the message and returns its reference.
326
	 *
327
	 * @param string $data Binary or string
328
	 * @param string $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
329
	 * @param string|null $filename Name of the attached file
330
	 * @return string Content ID for referencing the attachment in the HTML body
331
	 */
332
	public function embedAttachment( string $data, string $mimetype, string $filename ) : string
333
	{
334
		$class = '\Symfony\Component\Mime\Email';
335
336
		if( !$filename ) {
337
			$filename = md5( $data );
338
		}
339
340
		if( class_exists( $class ) && $this->object instanceof $class )
341
		{
342
			$this->object->embed( $data, $filename, $mimetype );
343
			return 'cid:' . $filename;
344
		}
345
		elseif( class_exists( '\Swift_EmbeddedFile' ) )
346
		{
347
			$part = \Swift_EmbeddedFile::newInstance( $data, $filename, $mimetype );
0 ignored issues
show
Bug introduced by
The type Swift_EmbeddedFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
348
			return $this->object->embed( $part );
349
		}
350
351
		throw new \RuntimeException( 'Symfony mailer or Swiftmailer package missing' );
352
	}
353
354
355
	/**
356
	 * Returns the internal TYPO3 mail message object.
357
	 *
358
	 * @return \TYPO3\CMS\Core\Mail\MailMessage TYPO3 mail message object
359
	 */
360
	public function getObject() : \TYPO3\CMS\Core\Mail\MailMessage
361
	{
362
		return $this->object;
363
	}
364
365
366
	/**
367
	 * Clones the internal objects.
368
	 */
369
	public function __clone()
370
	{
371
		$this->object = clone $this->object;
372
	}
373
}
374