Completed
Push — master ( 4be27a...24b753 )
by Aimeos
03:01 queued 03:01
created

Laravel::subject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022
6
 * @package Base
7
 * @subpackage Mail
8
 */
9
10
11
namespace Aimeos\Base\Mail\Message;
12
13
14
/**
15
 * Laravel implementation for creating e-mails.
16
 *
17
 * @package Base
18
 * @subpackage Mail
19
 */
20
class Laravel implements \Aimeos\Base\Mail\Message\Iface
21
{
22
	private $charset;
23
	private $message;
24
	private $mailer;
25
26
27
	/**
28
	 * Initializes the message instance.
29
	 *
30
	 * @param \Illuminate\Mail\Mailer $object Laravel mailer object
31
	 * @param \Symfony\Component\Mime\Email $message Message object
32
	 * @param string $charset Default charset of the message
33
	 */
34
	public function __construct( \Illuminate\Mail\Mailer $mailer, \Symfony\Component\Mime\Email $message, string $charset )
35
	{
36
		$this->charset = $charset;
37
		$this->message = $message;
38
		$this->mailer = $mailer;
39
	}
40
41
42
	/**
43
	 * Adds a source e-mail address of the message.
44
	 *
45
	 * @param string $email Source e-mail address
46
	 * @param string|null $name Name of the user sending the e-mail or null for no name
47
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
48
	 */
49
	public function from( string $email, string $name = null ) : Iface
50
	{
51
		if( $email ) {
52
			$this->message->from( $email, $name );
53
		}
54
55
		return $this;
56
	}
57
58
59
	/**
60
	 * Adds a destination e-mail address of the target user mailbox.
61
	 *
62
	 * @param string $email Destination address of the target mailbox
63
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
64
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
65
	 */
66
	public function to( string $email, string $name = null ) : Iface
67
	{
68
		if( $email ) {
69
			$this->message->to( $email, $name );
70
		}
71
72
		return $this;
73
	}
74
75
76
	/**
77
	 * Adds a destination e-mail address for a copy of the message.
78
	 *
79
	 * @param string $email Destination address for a copy
80
	 * @param string|null $name Name of the user owning the target mailbox or null for no name
81
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
82
	 */
83
	public function cc( string $email, string $name = null ) : Iface
84
	{
85
		if( $email ) {
86
			$this->message->cc( $email, $name );
87
		}
88
89
		return $this;
90
	}
91
92
93
	/**
94
	 * Adds a destination e-mail address for a hidden copy of the message.
95
	 *
96
	 * @param array|string $email Destination address for a hidden copy
97
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
98
	 */
99
	public function bcc( $email ) : Iface
100
	{
101
		if( !empty( $email ) ) {
102
				$this->message->bcc( $email );
0 ignored issues
show
Bug introduced by
It seems like $email can also be of type array; however, parameter $addresses of Symfony\Component\Mime\Email::bcc() does only seem to accept Symfony\Component\Mime\Address|string, 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

102
				$this->message->bcc( /** @scrutinizer ignore-type */ $email );
Loading history...
103
		}
104
105
		return $this;
106
	}
107
108
109
	/**
110
	 * Adds the return e-mail address for the message.
111
	 *
112
	 * @param string $email E-mail address which should receive all replies
113
	 * @param string|null $name Name of the user which should receive all replies or null for no name
114
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
115
	 */
116
	public function replyTo( string $email, string $name = null ) : Iface
117
	{
118
		if( $email ) {
119
			$this->message->replyTo( $email, $name );
120
		}
121
122
		return $this;
123
	}
124
125
126
	/**
127
	 * Adds a custom header to the message.
128
	 *
129
	 * @param string $name Name of the custom e-mail header
130
	 * @param string $value Text content of the custom e-mail header
131
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
132
	 */
133
	public function header( string $name, string $value ) : Iface
134
	{
135
		if( $name ) {
136
			$this->message->getHeaders()->add( new \Symfony\Component\Mime\Header\UnstructuredHeader( $name, $value ) );
137
		}
138
139
		return $this;
140
	}
141
142
143
	/**
144
	 * Sends the e-mail message to the mail server.
145
	 *
146
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
147
	 */
148
	public function send() : Iface
149
	{
150
		$this->mailer->getSymfonyTransport()
151
			->send( $this->message, \Symfony\Component\Mailer\Envelope::create( $this->message ) );
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Mailer\Envelope 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...
152
153
		return $this;
154
	}
155
156
157
	/**
158
	 * Sets the e-mail address and name of the sender of the message (higher precedence than "From").
159
	 *
160
	 * @param string $email Source e-mail address
161
	 * @param string|null $name Name of the user who sent the message or null for no name
162
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
163
	 */
164
	public function sender( string $email, string $name = null ) : Iface
165
	{
166
		if( $email ) {
167
			$this->message->sender( $email, $name );
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Mime\Email::sender() has too many arguments starting with $name. ( Ignorable by Annotation )

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

167
			$this->message->/** @scrutinizer ignore-call */ 
168
                   sender( $email, $name );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
168
		}
169
170
		return $this;
171
	}
172
173
174
	/**
175
	 * Sets the subject of the message.
176
	 *
177
	 * @param string $subject Subject of the message
178
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
179
	 */
180
	public function subject( string $subject ) : Iface
181
	{
182
		if( $subject ) {
183
			$this->message->subject( $subject );
184
		}
185
186
		return $this;
187
	}
188
189
190
	/**
191
	 * Sets the text body of the message.
192
	 *
193
	 * @param string $message Text body of the message
194
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
195
	 */
196
	public function text( string $message ) : Iface
197
	{
198
		if( $message ) {
199
			$this->message->text( $message, $this->charset );
200
		}
201
202
		return $this;
203
	}
204
205
206
	/**
207
	 * Sets the HTML body of the message.
208
	 *
209
	 * @param string $message HTML body of the message
210
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
211
	 */
212
	public function html( string $message ) : Iface
213
	{
214
		if( $message ) {
215
			$this->message->html( $message, $this->charset );
216
		}
217
218
		return $this;
219
	}
220
221
222
	/**
223
	 * Adds an attachment to the message.
224
	 *
225
	 * @param string|null $data Binary or string @author nose
226
	 * @param string|null $filename Name of the attached file (or null if inline disposition is used)
227
	 * @param string|null $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
228
	 * @param string $disposition Type of the disposition ("attachment" or "inline")
229
	 * @return \Aimeos\Base\Mail\Message\Iface Message object
230
	 */
231
	public function attach( ?string $data, string $filename = null, string $mimetype = null, string $disposition = 'attachment' ) : Iface
232
	{
233
		if( $data )
234
		{
235
			$mimetype = $mimetype ?: (new \finfo( FILEINFO_MIME_TYPE ))->buffer( $data );
236
			$filename = $filename ?: md5( $data );
237
238
			$this->message->attach( $data, $filename, $mimetype );
239
		}
240
241
		return $this;
242
	}
243
244
245
	/**
246
	 * Embeds an attachment into the message and returns its reference.
247
	 *
248
	 * @param string|null $data Binary or string
249
	 * @param string|null $filename Name of the attached file
250
	 * @param string|null $mimetype Mime type of the attachment (e.g. "text/plain", "application/octet-stream", etc.)
251
	 * @return string Content ID for referencing the attachment in the HTML body
252
	 */
253
	public function embed( ?string $data, string $filename = null, string $mimetype = null ) : string
254
	{
255
		if( $data )
256
		{
257
			$mimetype = $mimetype ?: (new \finfo( FILEINFO_MIME_TYPE ))->buffer( $data );
258
			$filename = $filename ?: md5( $data );
259
260
			$this->message->embed( $data, $filename, $mimetype );
261
			return 'cid:' . $filename;
262
		}
263
264
		return '';
265
	}
266
267
268
	/**
269
	 * Clones the internal objects.
270
	 */
271
	public function __clone()
272
	{
273
		$this->message = clone $this->message;
274
	}
275
}
276