Passed
Push — master ( fd781a...b1eaa4 )
by Aimeos
08:26
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 77
rs 10
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, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Email\Watch;
13
14
15
/**
16
 * Default implementation of product notification e-mails.
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/watch/subparts
26
	 * List of HTML sub-clients rendered within the product notification e-mail
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/watch/subparts';
59
60
	/** client/html/email/watch/text/name
61
	 * Name of the text part used by the product notification e-mail client implementation
62
	 *
63
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Watch\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/watch/html/name
72
	 * Name of the html part used by the product notification e-mail client implementation
73
	 *
74
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Email\Watch\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 body( string $uid = '' ) : string
91
	{
92
		$view = $this->object()->data( $this->view() );
93
94
		$content = '';
95
		foreach( $this->getSubClients() as $subclient ) {
96
			$content .= $subclient->setView( $view )->body( $uid );
97
		}
98
		$view->watchBody = $content;
99
100
		/** client/html/email/watch/template-body
101
		 * Relative path to the HTML body template of the product notification e-mail client.
102
		 *
103
		 * The template file contains the HTML code and processing instructions
104
		 * to generate the result shown in the body of the frontend. The
105
		 * configuration string is the path to the template file relative
106
		 * to the templates directory (usually in client/html/templates).
107
		 *
108
		 * You can overwrite the template file configuration in extensions and
109
		 * provide alternative templates. These alternative templates should be
110
		 * named like the default one but suffixed by
111
		 * an unique name. You may use the name of your project for this. If
112
		 * you've implemented an alternative client class as well, it
113
		 * should be suffixed by the name of the new class.
114
		 *
115
		 * The product notification e-mail HTML client allows to use a different template for
116
		 * each watch status value. You can create a template for each watch
117
		 * status and store it in the "email/watch/<status number>/" directory
118
		 * below the "templates" directory (usually in client/html/templates). If no
119
		 * specific layout template is found, the common template in the
120
		 * "email/watch/" directory is used.
121
		 *
122
		 * @param string Relative path to the template creating code for the HTML page body
123
		 * @since 2014.03
124
		 * @category Developer
125
		 * @see client/html/email/watch/template-header
126
		 */
127
		$tplconf = 'client/html/email/watch/template-body';
128
129
		return $view->render( $view->config( $tplconf, 'email/watch/body' ) );
130
	}
131
132
133
	/**
134
	 * Returns the HTML string for insertion into the header.
135
	 *
136
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
137
	 * @return string|null String including HTML tags for the header on error
138
	 */
139
	public function header( string $uid = '' ) : ?string
140
	{
141
		$config = $this->context()->config();
142
		$view = $this->object()->data( $this->view() );
143
144
145
		$addr = $view->extAddressItem;
146
147
		$msg = $view->mail();
148
		$msg->header( 'X-MailGenerator', 'Aimeos' );
149
		$msg->to( $addr->getEMail(), $addr->getFirstName() . ' ' . $addr->getLastName() );
150
151
152
		$fromName = $config->get( 'resource/email/from-name' );
153
154
		/** client/html/email/from-name
155
		 * @see client/html/email/watch/from-email
156
		 */
157
		$fromName = $config->get( 'client/html/email/from-name', $fromName );
158
159
		/** client/html/email/watch/from-name
160
		 * Name used when sending product notification e-mails
161
		 *
162
		 * The name of the person or e-mail account that is used for sending all
163
		 * shop related watch e-mails to customers. This configuration option
164
		 * overwrite the name set in "client/html/email/from-name".
165
		 *
166
		 * @param string Name shown in the e-mail
167
		 * @since 2014.03
168
		 * @category User
169
		 * @see client/html/email/from-name
170
		 * @see client/html/email/from-email
171
		 * @see client/html/email/reply-email
172
		 * @see client/html/email/bcc-email
173
		 */
174
		$fromNameWatch = $config->get( 'client/html/email/watch/from-name', $fromName );
175
176
		$fromEmail = $config->get( 'resource/email/from-email' );
177
178
		/** client/html/email/from-email
179
		 * @see client/html/email/watch/from-email
180
		 */
181
		$fromEmail = $config->get( 'client/html/email/from-email', $fromEmail );
182
183
		/** client/html/email/watch/from-email
184
		 * E-Mail address used when sending product notification e-mails
185
		 *
186
		 * The e-mail address of the person or account that is used for sending
187
		 * all shop related product notification e-mails to customers. This configuration option
188
		 * overwrites the e-mail address set via "client/html/email/from-email".
189
		 *
190
		 * @param string E-mail address
191
		 * @since 2014.03
192
		 * @category User
193
		 * @see client/html/email/watch/from-name
194
		 * @see client/html/email/from-email
195
		 * @see client/html/email/reply-email
196
		 * @see client/html/email/bcc-email
197
		 */
198
		if( ( $fromEmailWatch = $config->get( 'client/html/email/watch/from-email', $fromEmail ) ) != null ) {
199
			$msg->from( $fromEmailWatch, $fromNameWatch );
200
		}
201
202
203
		/** client/html/email/reply-name
204
		 * @see client/html/email/watch/reply-email
205
		 */
206
		$replyName = $config->get( 'client/html/email/reply-name', $fromName );
207
208
		/** client/html/email/watch/reply-name
209
		 * Recipient name displayed when the customer replies to product notification e-mails
210
		 *
211
		 * The name of the person or e-mail account the customer should
212
		 * reply to in case of watch related questions or problems. This
213
		 * configuration option overwrites the name set via
214
		 * "client/html/email/reply-name".
215
		 *
216
		 * @param string Name shown in the e-mail
217
		 * @since 2014.03
218
		 * @category User
219
		 * @see client/html/email/watch/reply-email
220
		 * @see client/html/email/reply-name
221
		 * @see client/html/email/reply-email
222
		 * @see client/html/email/from-email
223
		 * @see client/html/email/bcc-email
224
		 */
225
		$replyNameWatch = $config->get( 'client/html/email/watch/reply-name', $replyName );
226
227
		/** client/html/email/reply-email
228
		 * @see client/html/email/watch/reply-email
229
		 */
230
		$replyEmail = $config->get( 'client/html/email/reply-email', $fromEmail );
231
232
		/** client/html/email/watch/reply-email
233
		 * E-Mail address used by the customer when replying to product notification e-mails
234
		 *
235
		 * The e-mail address of the person or e-mail account the customer
236
		 * should reply to in case of watch related questions or problems.
237
		 * This configuration option overwrites the e-mail address set via
238
		 * "client/html/email/reply-email".
239
		 *
240
		 * @param string E-mail address
241
		 * @since 2014.03
242
		 * @category User
243
		 * @see client/html/email/watch/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
		if( ( $replyEmailWatch = $config->get( 'client/html/email/watch/reply-email', $replyEmail ) ) != null ) {
249
			$msg->replyTo( $replyEmailWatch, $replyNameWatch );
250
		}
251
252
253
		/** client/html/email/bcc-email
254
		 * @see client/html/email/watch/bcc-email
255
		 */
256
		$bccEmail = $config->get( 'client/html/email/bcc-email' );
257
258
		/** client/html/email/watch/bcc-email
259
		 * E-Mail address all product notification e-mails should be also sent to
260
		 *
261
		 * Using this option you can send a copy of all watch related e-mails
262
		 * to a second e-mail account. This can be handy for testing and checking
263
		 * the e-mails sent to customers.
264
		 *
265
		 * It also allows shop owners with a very small volume of orders to be
266
		 * notified about watch changes. Be aware that this isn't useful if the
267
		 * order volumne is high or has peeks!
268
		 *
269
		 * This configuration option overwrites the e-mail address set via
270
		 * "client/html/email/bcc-email".
271
		 *
272
		 * @param string|array E-mail address or list of e-mail addresses
273
		 * @since 2014.03
274
		 * @category User
275
		 * @category Developer
276
		 * @see client/html/email/bcc-email
277
		 * @see client/html/email/reply-email
278
		 * @see client/html/email/from-email
279
		 */
280
		if( ( $bccEmailWatch = $config->get( 'client/html/email/watch/bcc-email', $bccEmail ) ) != null )
281
		{
282
			foreach( (array) $bccEmailWatch as $emailAddr ) {
283
				$msg->Bcc( $emailAddr );
284
			}
285
		}
286
287
288
		/** client/html/email/watch/template-header
289
		 * Relative path to the HTML header template of the product notification e-mail client.
290
		 *
291
		 * The template file contains the HTML code and processing instructions
292
		 * to generate the HTML code that is inserted into the HTML page header
293
		 * of the rendered page in the frontend. The configuration string is the
294
		 * path to the template file relative to the templates directory (usually
295
		 * in client/html/templates).
296
		 *
297
		 * You can overwrite the template file configuration in extensions and
298
		 * provide alternative templates. These alternative templates should be
299
		 * named like the default one but suffixed by
300
		 * an unique name. You may use the name of your project for this. If
301
		 * you've implemented an alternative client class as well, it
302
		 * should be suffixed by the name of the new class.
303
		 *
304
		 * The product notification e-mail HTML client allows to use a different template for
305
		 * each watch status value. You can create a template for each watch
306
		 * status and store it in the "email/watch/<status number>/" directory
307
		 * below the "templates" directory (usually in client/html/templates). If no
308
		 * specific layout template is found, the common template in the
309
		 * "email/watch/" directory is used.
310
		 *
311
		 * @param string Relative path to the template creating code for the HTML page head
312
		 * @since 2014.03
313
		 * @category Developer
314
		 * @see client/html/email/watch/template-body
315
		 */
316
		$tplconf = 'client/html/email/watch/template-header';
317
318
		return $view->render( $view->config( $tplconf, 'email/watch/header' ) ); ;
319
	}
320
321
322
	/**
323
	 * Returns the sub-client given by its name.
324
	 *
325
	 * @param string $type Name of the client type
326
	 * @param string|null $name Name of the sub-client (Default if null)
327
	 * @return \Aimeos\Client\Html\Iface Sub-client object
328
	 */
329
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
330
	{
331
		/** client/html/email/watch/decorators/excludes
332
		 * Excludes decorators added by the "common" option from the email watch html client
333
		 *
334
		 * Decorators extend the functionality of a class by adding new aspects
335
		 * (e.g. log what is currently done), executing the methods of the underlying
336
		 * class only in certain conditions (e.g. only for logged in users) or
337
		 * modify what is returned to the caller.
338
		 *
339
		 * This option allows you to remove a decorator added via
340
		 * "client/html/common/decorators/default" before they are wrapped
341
		 * around the html client.
342
		 *
343
		 *  client/html/email/watch/decorators/excludes = array( 'decorator1' )
344
		 *
345
		 * This would remove the decorator named "decorator1" from the list of
346
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
347
		 * "client/html/common/decorators/default" to the html client.
348
		 *
349
		 * @param array List of decorator names
350
		 * @since 2014.05
351
		 * @category Developer
352
		 * @see client/html/common/decorators/default
353
		 * @see client/html/email/watch/decorators/global
354
		 * @see client/html/email/watch/decorators/local
355
		 */
356
357
		/** client/html/email/watch/decorators/global
358
		 * Adds a list of globally available decorators only to the email watch 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 wrap global decorators
366
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
367
		 *
368
		 *  client/html/email/watch/decorators/global = array( 'decorator1' )
369
		 *
370
		 * This would add the decorator named "decorator1" defined by
371
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
372
		 *
373
		 * @param array List of decorator names
374
		 * @since 2014.05
375
		 * @category Developer
376
		 * @see client/html/common/decorators/default
377
		 * @see client/html/email/watch/decorators/excludes
378
		 * @see client/html/email/watch/decorators/local
379
		 */
380
381
		/** client/html/email/watch/decorators/local
382
		 * Adds a list of local decorators only to the email watch html client
383
		 *
384
		 * Decorators extend the functionality of a class by adding new aspects
385
		 * (e.g. log what is currently done), executing the methods of the underlying
386
		 * class only in certain conditions (e.g. only for logged in users) or
387
		 * modify what is returned to the caller.
388
		 *
389
		 * This option allows you to wrap local decorators
390
		 * ("\Aimeos\Client\Html\Email\Decorator\*") around the html client.
391
		 *
392
		 *  client/html/email/watch/decorators/local = array( 'decorator2' )
393
		 *
394
		 * This would add the decorator named "decorator2" defined by
395
		 * "\Aimeos\Client\Html\Email\Decorator\Decorator2" only to the html client.
396
		 *
397
		 * @param array List of decorator names
398
		 * @since 2014.05
399
		 * @category Developer
400
		 * @see client/html/common/decorators/default
401
		 * @see client/html/email/watch/decorators/excludes
402
		 * @see client/html/email/watch/decorators/global
403
		 */
404
405
		return $this->createSubClient( 'email/watch/' . $type, $name );
406
	}
407
408
409
	/**
410
	 * Returns the list of sub-client names configured for the client.
411
	 *
412
	 * @return array List of HTML client names
413
	 */
414
	protected function getSubClientNames() : array
415
	{
416
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
417
	}
418
419
420
	/**
421
	 * Sets the necessary parameter values in the view.
422
	 *
423
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
424
	 * @param array &$tags Result array for the list of tags that are associated to the output
425
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
426
	 * @return \Aimeos\MW\View\Iface Modified view object
427
	 */
428
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
429
	{
430
		$addr = $view->get( 'extAddressItem' );
431
		$list = [
432
			/// E-mail intro with first name (%1$s) and last name (%2$s)
433
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_UNKNOWN => $view->translate( 'client', 'Dear %1$s %2$s' ),
434
			/// E-mail intro with first name (%1$s) and last name (%2$s)
435
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MR => $view->translate( 'client', 'Dear Mr %1$s %2$s' ),
436
			/// E-mail intro with first name (%1$s) and last name (%2$s)
437
			\Aimeos\MShop\Common\Item\Address\Base::SALUTATION_MS => $view->translate( 'client', 'Dear Ms %1$s %2$s' ),
438
		];
439
440
		if( $addr && isset( $list[$addr->getSalutation()] ) ) {
441
			$view->emailIntro = sprintf( $list[$addr->getSalutation()], $addr->getFirstName(), $addr->getLastName() );
442
		} else {
443
			$view->emailIntro = $view->translate( 'client', 'Dear customer' );
444
		}
445
446
		return parent::data( $view, $tags, $expire );
447
	}
448
}
449