Passed
Push — master ( e7737f...0b7334 )
by Aimeos
03:42
created

Standard::header()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 189
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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