Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Standard::getSubClient()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.48
c 0
b 0
f 0
cc 1
nc 1
nop 2

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-2021
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Email\Watch\Html;
13
14
15
/**
16
 * Default implementation of product notification e-mail html HTML client.
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/html/subparts
26
	 * List of HTML sub-clients rendered within the product notification e-mail html 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.09
56
	 * @category Developer
57
	 */
58
	private $subPartPath = 'client/html/email/watch/html/subparts';
59
	private $subPartNames = [];
60
61
62
	/**
63
	 * Returns the HTML code for insertion into the body.
64
	 *
65
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
66
	 * @return string HTML code
67
	 */
68
	public function getBody( string $uid = '' ) : string
69
	{
70
		$view = $this->getView();
71
72
		$content = '';
73
		foreach( $this->getSubClients() as $subclient ) {
74
			$content .= $subclient->setView( $view )->getBody( $uid );
75
		}
76
		$view->htmlBody = $content;
77
78
		/** client/html/email/watch/html/template-body
79
		 * Relative path to the HTML body template of the product notification e-mail html client.
80
		 *
81
		 * The template file contains the HTML code and processing instructions
82
		 * to generate the result shown in the body of the e-mail. The
83
		 * configuration string is the path to the template file relative
84
		 * to the templates directory (usually in client/html/templates).
85
		 *
86
		 * You can overwrite the template file configuration in extensions and
87
		 * provide alternative templates. These alternative templates should be
88
		 * named like the default one but with the string "standard" replaced by
89
		 * an unique name. You may use the name of your project for this. If
90
		 * you've implemented an alternative client class as well, "standard"
91
		 * should be replaced by the name of the new class.
92
		 *
93
		 * The product notification e-mail html client allows to use a different template for
94
		 * each watch status value. You can create a template for each watch
95
		 * status and store it in the "email/watch/<status number>/" directory
96
		 * below the "templates" directory (usually in client/html/templates). If no
97
		 * specific layout template is found, the common template in the
98
		 * "email/watch/" directory is used.
99
		 *
100
		 * @param string Relative path to the template creating code for the HTML e-mail body
101
		 * @since 2014.09
102
		 * @category Developer
103
		 * @see client/html/email/watch/html/template-header
104
		 */
105
		$tplconf = 'client/html/email/watch/html/template-body';
106
107
		$html = $view->render( $view->config( $tplconf, 'email/watch/html-body-standard' ) );
108
		$view->mail()->setBodyHtml( $html );
109
		return $html;
110
	}
111
112
113
	/**
114
	 * Returns the sub-client given by its name.
115
	 *
116
	 * @param string $type Name of the client type
117
	 * @param string|null $name Name of the sub-client (Default if null)
118
	 * @return \Aimeos\Client\Html\Iface Sub-client object
119
	 */
120
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
121
	{
122
		/** client/html/email/watch/html/decorators/excludes
123
		 * Excludes decorators added by the "common" option from the "email watch html" html client
124
		 *
125
		 * Decorators extend the functionality of a class by adding new aspects
126
		 * (e.g. log what is currently done), executing the methods of the underlying
127
		 * class only in certain conditions (e.g. only for logged in users) or
128
		 * modify what is returned to the caller.
129
		 *
130
		 * This option allows you to remove a decorator added via
131
		 * "client/html/common/decorators/default" before they are wrapped
132
		 * around the html client.
133
		 *
134
		 *  client/html/email/watch/html/decorators/excludes = array( 'decorator1' )
135
		 *
136
		 * This would remove the decorator named "decorator1" from the list of
137
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
138
		 * "client/html/common/decorators/default" to the html client.
139
		 *
140
		 * @param array List of decorator names
141
		 * @since 2015.08
142
		 * @category Developer
143
		 * @see client/html/common/decorators/default
144
		 * @see client/html/email/watch/html/decorators/global
145
		 * @see client/html/email/watch/html/decorators/local
146
		 */
147
148
		/** client/html/email/watch/html/decorators/global
149
		 * Adds a list of globally available decorators only to the "email watch html" html client
150
		 *
151
		 * Decorators extend the functionality of a class by adding new aspects
152
		 * (e.g. log what is currently done), executing the methods of the underlying
153
		 * class only in certain conditions (e.g. only for logged in users) or
154
		 * modify what is returned to the caller.
155
		 *
156
		 * This option allows you to wrap global decorators
157
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
158
		 *
159
		 *  client/html/email/watch/html/decorators/global = array( 'decorator1' )
160
		 *
161
		 * This would add the decorator named "decorator1" defined by
162
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
163
		 *
164
		 * @param array List of decorator names
165
		 * @since 2015.08
166
		 * @category Developer
167
		 * @see client/html/common/decorators/default
168
		 * @see client/html/email/watch/html/decorators/excludes
169
		 * @see client/html/email/watch/html/decorators/local
170
		 */
171
172
		/** client/html/email/watch/html/decorators/local
173
		 * Adds a list of local decorators only to the "email watch html" html client
174
		 *
175
		 * Decorators extend the functionality of a class by adding new aspects
176
		 * (e.g. log what is currently done), executing the methods of the underlying
177
		 * class only in certain conditions (e.g. only for logged in users) or
178
		 * modify what is returned to the caller.
179
		 *
180
		 * This option allows you to wrap local decorators
181
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
182
		 *
183
		 *  client/html/email/watch/html/decorators/local = array( 'decorator2' )
184
		 *
185
		 * This would add the decorator named "decorator2" defined by
186
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
187
		 *
188
		 * @param array List of decorator names
189
		 * @since 2015.08
190
		 * @category Developer
191
		 * @see client/html/common/decorators/default
192
		 * @see client/html/email/watch/html/decorators/excludes
193
		 * @see client/html/email/watch/html/decorators/global
194
		 */
195
196
		return $this->createSubClient( 'email/watch/html/' . $type, $name );
197
	}
198
199
200
	/**
201
	 * Returns the list of sub-client names configured for the client.
202
	 *
203
	 * @return array List of HTML client names
204
	 */
205
	protected function getSubClientNames() : array
206
	{
207
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
208
	}
209
210
211
	/**
212
	 * Sets the necessary parameter values in the view.
213
	 *
214
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
215
	 * @param array &$tags Result array for the list of tags that are associated to the output
216
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
217
	 * @return \Aimeos\MW\View\Iface Modified view object
218
	 */
219
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
220
	{
221
		/** client/html/email/logo
222
		 * Path to the logo image displayed in HTML e-mails
223
		 *
224
		 * The path can either be an absolute local path or an URL to a file on a
225
		 * remote server. If the file is stored on a remote server, "allow_url_fopen"
226
		 * must be enabled. See {@link http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen php.ini allow_url_fopen}
227
		 * documentation for details.
228
		 *
229
		 * @param string Absolute file system path or remote URL to the logo image
230
		 * @since 2014.03
231
		 * @category User
232
		 * @see client/html/email/from-email
233
		 */
234
		$file = $view->config( 'client/html/email/logo', 'client/html/themes/elegance/media/aimeos.png' );
235
236
		if( file_exists( $file ) && ( $content = file_get_contents( $file ) ) !== false )
237
		{
238
			$finfo = new \finfo( FILEINFO_MIME_TYPE );
239
			$mimetype = $finfo->file( $file );
240
241
			$view->htmlLogo = $view->mail()->embedAttachment( $content, $mimetype, basename( $file ) );
242
		}
243
244
245
		$path = $view->config( 'client/html/common/template/baseurl', 'client/html/themes/elegance' );
246
		$filepath = $path . DIRECTORY_SEPARATOR . 'email.css';
247
248
		if( file_exists( $filepath ) && ( $css = file_get_contents( $filepath ) ) !== false ) {
249
			$view->htmlCss = $css;
250
		}
251
252
		return parent::addData( $view, $tags, $expire );
253
	}
254
}
255