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