Completed
Push — master ( 4dbc3b...796c61 )
by Aimeos
03:24
created

Standard::addData()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 16
nop 3
dl 0
loc 51
rs 8.4444
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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), 2015-2020
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Email\Payment;
13
14
15
/**
16
 * Default implementation of payment emails.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/email/payment/standard/subparts
26
	 * List of HTML sub-clients rendered within the email payment section
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * @param array List of sub-client names
55
	 * @since 2014.03
56
	 * @category Developer
57
	 */
58
	private $subPartPath = 'client/html/email/payment/standard/subparts';
59
60
	/** client/html/email/payment/text/name
61
	 * Name of the text part used by the email payment client implementation
62
	 *
63
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Payment\Text\Myname".
64
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
65
	 *
66
	 * @param string Last part of the client class name
67
	 * @since 2014.03
68
	 * @category Developer
69
	 */
70
71
	/** client/html/email/payment/html/name
72
	 * Name of the html part used by the email payment client implementation
73
	 *
74
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Payment\Html\Myname".
75
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
76
	 *
77
	 * @param string Last part of the client class name
78
	 * @since 2014.03
79
	 * @category Developer
80
	 */
81
	private $subPartNames = array( 'text', 'html' );
82
83
84
	/**
85
	 * Returns the HTML code for insertion into the body.
86
	 *
87
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
88
	 * @return string HTML code
89
	 */
90
	public function getBody( string $uid = '' ) : string
91
	{
92
		$view = $this->getObject()->addData( $this->getView() );
93
94
		$content = '';
95
		foreach( $this->getSubClients() as $subclient ) {
96
			$content .= $subclient->setView( $view )->getBody( $uid );
97
		}
98
		$view->paymentBody = $content;
99
100
101
		/** client/html/email/payment/attachments
102
		 * List of file paths whose content should be attached to all payment e-mails
103
		 *
104
		 * This configuration option allows you to add files to the e-mails that are
105
		 * sent to the customer when the payment status changes, e.g. for the order
106
		 * confirmation e-mail. These files can't be customer specific.
107
		 *
108
		 * @param array List of absolute file paths
109
		 * @since 2016.10
110
		 * @category Developer
111
		 * @category User
112
		 * @see client/html/email/delivery/attachments
113
		 */
114
		$files = $view->config( 'client/html/email/payment/attachments', [] );
115
116
		$this->addAttachments( $view->mail(), $files );
117
118
119
		/** client/html/email/payment/standard/template-body
120
		 * Relative path to the HTML body template of the email payment client.
121
		 *
122
		 * The template file contains the HTML code and processing instructions
123
		 * to generate the result shown in the body of the frontend. The
124
		 * configuration string is the path to the template file relative
125
		 * to the templates directory (usually in client/html/templates).
126
		 *
127
		 * You can overwrite the template file configuration in extensions and
128
		 * provide alternative templates. These alternative templates should be
129
		 * named like the default one but with the string "standard" replaced by
130
		 * an unique name. You may use the name of your project for this. If
131
		 * you've implemented an alternative client class as well, "standard"
132
		 * should be replaced by the name of the new class.
133
		 *
134
		 * The email payment HTML client allows to use a different template for
135
		 * each payment status value. You can create a template for each payment
136
		 * status and store it in the "email/payment/<status number>/" directory
137
		 * below the "templates" directory (usually in client/html/templates). If no
138
		 * specific layout template is found, the common template in the
139
		 * "email/payment/" directory is used.
140
		 *
141
		 * @param string Relative path to the template creating code for the HTML page body
142
		 * @since 2014.03
143
		 * @category Developer
144
		 * @see client/html/email/payment/standard/template-header
145
		 */
146
		$tplconf = 'client/html/email/payment/standard/template-body';
147
148
		$status = $view->extOrderItem->getPaymentStatus();
149
		$default = array( 'email/payment/' . $status . '/body-standard', 'email/payment/body-standard' );
150
151
		return $view->render( $view->config( $tplconf, $default ) );
152
	}
153
154
155
	/**
156
	 * Returns the HTML string for insertion into the header.
157
	 *
158
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
159
	 * @return string|null String including HTML tags for the header on error
160
	 */
161
	public function getHeader( string $uid = '' ) : ?string
162
	{
163
		$view = $this->getObject()->addData( $this->getView() );
164
165
		$content = '';
166
		foreach( $this->getSubClients() as $subclient ) {
167
			$content .= $subclient->setView( $view )->getHeader( $uid );
168
		}
169
		$view->paymentHeader = $content;
170
171
172
		$addr = $view->extAddressItem;
173
174
		$msg = $view->mail();
175
		$msg->addHeader( 'X-MailGenerator', 'Aimeos' );
176
		$msg->addTo( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() );
177
178
179
		/** client/html/email/from-name
180
		 * @see client/html/email/payment/from-email
181
		 */
182
		$fromName = $view->config( 'client/html/email/from-name' );
183
184
		/** client/html/email/payment/from-name
185
		 * Name used when sending payment e-mails
186
		 *
187
		 * The name of the person or e-mail account that is used for sending all
188
		 * shop related payment e-mails to customers. This configuration option
189
		 * overwrite the name set in "client/html/email/from-name".
190
		 *
191
		 * @param string Name shown in the e-mail
192
		 * @since 2014.03
193
		 * @category User
194
		 * @see client/html/email/from-name
195
		 * @see client/html/email/from-email
196
		 * @see client/html/email/reply-email
197
		 * @see client/html/email/bcc-email
198
		 */
199
		$fromNamePayment = $view->config( 'client/html/email/payment/from-name', $fromName );
200
201
		/** client/html/email/from-email
202
		 * @see client/html/email/payment/from-email
203
		 */
204
		$fromEmail = $view->config( 'client/html/email/from-email' );
205
206
		/** client/html/email/payment/from-email
207
		 * E-Mail address used when sending payment e-mails
208
		 *
209
		 * The e-mail address of the person or account that is used for sending
210
		 * all shop related payment emails to customers. This configuration option
211
		 * overwrites the e-mail address set via "client/html/email/from-email".
212
		 *
213
		 * @param string E-mail address
214
		 * @since 2014.03
215
		 * @category User
216
		 * @see client/html/email/payment/from-name
217
		 * @see client/html/email/from-email
218
		 * @see client/html/email/reply-email
219
		 * @see client/html/email/bcc-email
220
		 */
221
		if( ( $fromEmailPayment = $view->config( 'client/html/email/payment/from-email', $fromEmail ) ) != null ) {
222
			$msg->addFrom( $fromEmailPayment, $fromNamePayment );
223
		}
224
225
226
		/** client/html/email/reply-name
227
		 * @see client/html/email/payment/reply-email
228
		 */
229
		$replyName = $view->config( 'client/html/email/reply-name', $fromName );
230
231
		/** client/html/email/payment/reply-name
232
		 * Recipient name displayed when the customer replies to payment e-mails
233
		 *
234
		 * The name of the person or e-mail account the customer should
235
		 * reply to in case of payment related questions or problems. This
236
		 * configuration option overwrites the name set via
237
		 * "client/html/email/reply-name".
238
		 *
239
		 * @param string Name shown in the e-mail
240
		 * @since 2014.03
241
		 * @category User
242
		 * @see client/html/email/payment/reply-email
243
		 * @see client/html/email/reply-name
244
		 * @see client/html/email/reply-email
245
		 * @see client/html/email/from-email
246
		 * @see client/html/email/bcc-email
247
		 */
248
		$replyNamePayment = $view->config( 'client/html/email/payment/reply-name', $replyName );
249
250
		/** client/html/email/reply-email
251
		 * @see client/html/email/payment/reply-email
252
		 */
253
		$replyEmail = $view->config( 'client/html/email/reply-email', $fromEmail );
254
255
		/** client/html/email/payment/reply-email
256
		 * E-Mail address used by the customer when replying to payment e-mails
257
		 *
258
		 * The e-mail address of the person or e-mail account the customer
259
		 * should reply to in case of payment related questions or problems.
260
		 * This configuration option overwrites the e-mail address set via
261
		 * "client/html/email/reply-email".
262
		 *
263
		 * @param string E-mail address
264
		 * @since 2014.03
265
		 * @category User
266
		 * @see client/html/email/payment/reply-name
267
		 * @see client/html/email/reply-email
268
		 * @see client/html/email/from-email
269
		 * @see client/html/email/bcc-email
270
		 */
271
		if( ( $replyEmailPayment = $view->config( 'client/html/email/payment/reply-email', $replyEmail ) ) != null ) {
272
			$msg->addReplyTo( $replyEmailPayment, $replyNamePayment );
273
		}
274
275
276
		/** client/html/email/bcc-email
277
		 * @see client/html/email/payment/bcc-email
278
		 */
279
		$bccEmail = $view->config( 'client/html/email/bcc-email' );
280
281
		/** client/html/email/payment/bcc-email
282
		 * E-Mail address all payment e-mails should be also sent to
283
		 *
284
		 * Using this option you can send a copy of all payment related e-mails
285
		 * to a second e-mail account. This can be handy for testing and checking
286
		 * the e-mails sent to customers.
287
		 *
288
		 * It also allows shop owners with a very small volume of orders to be
289
		 * notified about payment changes. Be aware that this isn't useful if the
290
		 * order volumne is high or has peeks!
291
		 *
292
		 * This configuration option overwrites the e-mail address set via
293
		 * "client/html/email/bcc-email".
294
		 *
295
		 * @param string|array E-mail address or list of e-mail addresses
296
		 * @since 2014.03
297
		 * @category User
298
		 * @category Developer
299
		 * @see client/html/email/bcc-email
300
		 * @see client/html/email/reply-email
301
		 * @see client/html/email/from-email
302
		 */
303
		if( ( $bccEmailPayment = $view->config( 'client/html/email/payment/bcc-email', $bccEmail ) ) != null )
304
		{
305
			foreach( (array) $bccEmailPayment as $emailAddr ) {
306
				$msg->addBcc( $emailAddr );
307
			}
308
		}
309
310
311
		/** client/html/email/payment/standard/template-header
312
		 * Relative path to the HTML header template of the email payment client.
313
		 *
314
		 * The template file contains the HTML code and processing instructions
315
		 * to generate the HTML code that is inserted into the HTML page header
316
		 * of the rendered page in the frontend. The configuration string is the
317
		 * path to the template file relative to the templates directory (usually
318
		 * in client/html/templates).
319
		 *
320
		 * You can overwrite the template file configuration in extensions and
321
		 * provide alternative templates. These alternative templates should be
322
		 * named like the default one but with the string "standard" replaced by
323
		 * an unique name. You may use the name of your project for this. If
324
		 * you've implemented an alternative client class as well, "standard"
325
		 * should be replaced by the name of the new class.
326
		 *
327
		 * The email payment HTML client allows to use a different template for
328
		 * each payment status value. You can create a template for each payment
329
		 * status and store it in the "email/payment/<status number>/" directory
330
		 * below the "templates" directory (usually in client/html/templates). If no
331
		 * specific layout template is found, the common template in the
332
		 * "email/payment/" directory is used.
333
		 *
334
		 * @param string Relative path to the template creating code for the HTML page head
335
		 * @since 2014.03
336
		 * @category Developer
337
		 * @see client/html/email/payment/standard/template-body
338
		 */
339
		$tplconf = 'client/html/email/payment/standard/template-header';
340
341
		$status = $view->extOrderItem->getPaymentStatus();
342
		$default = array( 'email/payment/' . $status . '/header-standard', 'email/payment/header-standard' );
343
344
		return $view->render( $view->config( $tplconf, $default ) ); ;
345
	}
346
347
348
	/**
349
	 * Returns the sub-client given by its name.
350
	 *
351
	 * @param string $type Name of the client type
352
	 * @param string|null $name Name of the sub-client (Default if null)
353
	 * @return \Aimeos\Client\Html\Iface Sub-client object
354
	 */
355
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
356
	{
357
		/** client/html/email/payment/decorators/excludes
358
		 * Excludes decorators added by the "common" option from the email payment html client
359
		 *
360
		 * Decorators extend the functionality of a class by adding new aspects
361
		 * (e.g. log what is currently done), executing the methods of the underlying
362
		 * class only in certain conditions (e.g. only for logged in users) or
363
		 * modify what is returned to the caller.
364
		 *
365
		 * This option allows you to remove a decorator added via
366
		 * "client/html/common/decorators/default" before they are wrapped
367
		 * around the html client.
368
		 *
369
		 *  client/html/email/payment/decorators/excludes = array( 'decorator1' )
370
		 *
371
		 * This would remove the decorator named "decorator1" from the list of
372
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
373
		 * "client/html/common/decorators/default" to the html client.
374
		 *
375
		 * @param array List of decorator names
376
		 * @since 2014.05
377
		 * @category Developer
378
		 * @see client/html/common/decorators/default
379
		 * @see client/html/email/payment/decorators/global
380
		 * @see client/html/email/payment/decorators/local
381
		 */
382
383
		/** client/html/email/payment/decorators/global
384
		 * Adds a list of globally available decorators only to the email payment html client
385
		 *
386
		 * Decorators extend the functionality of a class by adding new aspects
387
		 * (e.g. log what is currently done), executing the methods of the underlying
388
		 * class only in certain conditions (e.g. only for logged in users) or
389
		 * modify what is returned to the caller.
390
		 *
391
		 * This option allows you to wrap global decorators
392
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
393
		 *
394
		 *  client/html/email/payment/decorators/global = array( 'decorator1' )
395
		 *
396
		 * This would add the decorator named "decorator1" defined by
397
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
398
		 *
399
		 * @param array List of decorator names
400
		 * @since 2014.05
401
		 * @category Developer
402
		 * @see client/html/common/decorators/default
403
		 * @see client/html/email/payment/decorators/excludes
404
		 * @see client/html/email/payment/decorators/local
405
		 */
406
407
		/** client/html/email/payment/decorators/local
408
		 * Adds a list of local decorators only to the email payment html client
409
		 *
410
		 * Decorators extend the functionality of a class by adding new aspects
411
		 * (e.g. log what is currently done), executing the methods of the underlying
412
		 * class only in certain conditions (e.g. only for logged in users) or
413
		 * modify what is returned to the caller.
414
		 *
415
		 * This option allows you to wrap local decorators
416
		 * ("\Aimeos\Client\Html\Email\Decorator\*") around the html client.
417
		 *
418
		 *  client/html/email/payment/decorators/local = array( 'decorator2' )
419
		 *
420
		 * This would add the decorator named "decorator2" defined by
421
		 * "\Aimeos\Client\Html\Email\Decorator\Decorator2" only to the html client.
422
		 *
423
		 * @param array List of decorator names
424
		 * @since 2014.05
425
		 * @category Developer
426
		 * @see client/html/common/decorators/default
427
		 * @see client/html/email/payment/decorators/excludes
428
		 * @see client/html/email/payment/decorators/global
429
		 */
430
431
		return $this->createSubClient( 'email/payment/' . $type, $name );
432
	}
433
434
435
	/**
436
	 * Adds the given list of files as attachments to the mail message object
437
	 *
438
	 * @param \Aimeos\MW\Mail\Message\Iface $msg Mail message
439
	 * @param array $files List of absolute file paths
440
	 */
441
	protected function addAttachments( \Aimeos\MW\Mail\Message\Iface $msg, array $files )
442
	{
443
		foreach( $files as $filename )
444
		{
445
			if( ( $content = @file_get_contents( $filename ) ) === false ) {
446
				throw new \Aimeos\Client\Html\Exception( sprintf( 'File "%1$s" doesn\'t exist', $filename ) );
447
			}
448
449
			if( class_exists( 'finfo' ) )
450
			{
451
				try
452
				{
453
					$finfo = new \finfo( FILEINFO_MIME_TYPE );
454
					$mimetype = $finfo->file( $filename );
455
				}
456
				catch( \Exception $e )
457
				{
458
					throw new \Aimeos\Client\Html\Exception( $e->getMessage() );
459
				}
460
			}
461
			else if( function_exists( 'mime_content_type' ) )
462
			{
463
				$mimetype = mime_content_type( $filename );
464
			}
465
			else
466
			{
467
				$mimetype = 'application/binary';
468
			}
469
470
			$msg->addAttachment( $content, $mimetype, basename( $filename ) );
471
		}
472
	}
473
474
475
	/**
476
	 * Returns the list of sub-client names configured for the client.
477
	 *
478
	 * @return array List of HTML client names
479
	 */
480
	protected function getSubClientNames() : array
481
	{
482
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
483
	}
484
485
486
	/**
487
	 * Sets the necessary parameter values in the view.
488
	 *
489
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
490
	 * @param array &$tags Result array for the list of tags that are associated to the output
491
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
492
	 * @return \Aimeos\MW\View\Iface Modified view object
493
	 */
494
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
495
	{
496
		$list = array(
497
			/// E-mail intro with first name (%1$s) and last name (%2$s)
498
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_UNKNOWN => $view->translate( 'client', 'Dear %1$s %2$s' ),
499
			/// E-mail intro with first name (%1$s) and last name (%2$s)
500
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR => $view->translate( 'client', 'Dear Mr %1$s %2$s' ),
501
			/// E-mail intro with first name (%1$s) and last name (%2$s)
502
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MRS => $view->translate( 'client', 'Dear Mrs %1$s %2$s' ),
503
			/// E-mail intro with first name (%1$s) and last name (%2$s)
504
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MISS => $view->translate( 'client', 'Dear Miss %1$s %2$s' ),
505
		);
506
507
		if( isset( $view->extAddressItem ) && ( $addr = $view->extAddressItem ) && isset( $list[$addr->getSalutation()] ) ) {
508
			$view->emailIntro = sprintf( $list[$addr->getSalutation()], $addr->getFirstName(), $addr->getLastName() );
509
		} else {
510
			$view->emailIntro = $view->translate( 'client', 'Dear Sir or Madam' );
511
		}
512
513
514
		$key = 'pay:' . $view->extOrderItem->getPaymentStatus();
515
		$status = $view->translate( 'mshop/code', $key );
516
517
		/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
518
		$msg = $view->translate( 'client', 'Thank you for your order %1$s from %2$s.' );
519
520
		switch( $view->extOrderItem->getPaymentStatus() )
521
		{
522
			case 3:
523
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
524
				$msg = $view->translate( 'client', 'The payment for your order %1$s from %2$s has been refunded.' );
525
				break;
526
			case 4:
527
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
528
				$msg .= "\n" . $view->translate( 'client', 'The order is pending until we receive the final payment. If you\'ve chosen to pay in advance, please transfer the money to our bank account with the order ID %1$s as reference.' );
529
				break;
530
			case 6:
531
				/// Payment e-mail intro with order ID (%1$s), order date (%2$s) and payment status (%3%s)
532
				$msg .= "\n" . $view->translate( 'client', 'We have received your payment, and will take care of your order immediately.' );
533
				break;
534
		}
535
536
		$date = date_create( $view->extOrderItem->getTimeCreated() )->format( $view->translate( 'client', 'Y-m-d' ) );
537
		$view->message = sprintf( $msg, $view->extOrderItem->getId(), $date, $status );
538
539
		$pricefmt = $view->translate( 'client/code', 'price:default' );
540
		/// Price format with price value (%1$s) and currency (%2$s)
541
		$view->priceFormat = $pricefmt !== 'price:default' ? $pricefmt : $view->translate( 'client', '%1$s %2$s' );
542
543
544
		return parent::addData( $view, $tags, $expire );
545
	}
546
}
547