Completed
Push — master ( dd6d00...52e4ba )
by Aimeos
03:58
created

Standard::getBody()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 54
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
nc 3
nop 1
dl 0
loc 54
c 2
b 0
f 0
cc 3
rs 9.7333

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), 2020
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Email\Payment\Pdf;
12
13
14
/**
15
 * Default implementation of email PDF client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Summary\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/email/payment/pdf/standard/subparts
25
	 * List of HTML sub-clients rendered within the email payment PDF 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 2020.07
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/email/payment/pdf/standard/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
		if( $view->extOrderItem->getPaymentStatus() < \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED ) {
72
			return '';
73
		}
74
75
		$content = '';
76
		foreach( $this->getSubClients() as $subclient ) {
77
			$content .= $subclient->setView( $view )->getBody( $uid );
78
		}
79
		$view->pdfBody = $content;
80
81
		/** client/html/email/payment/pdf/standard/template-body
82
		 * Relative path to the HTML body template of the email payment PDF client.
83
		 *
84
		 * The template file contains the HTML code and processing instructions
85
		 * to generate the result shown in the body of the e-mail. The
86
		 * configuration string is the path to the template file relative
87
		 * to the templates directory (usually in client/html/templates).
88
		 *
89
		 * You can overwrite the template file configuration in extensions and
90
		 * provide alternative templates. These alternative templates should be
91
		 * named like the default one but with the string "standard" replaced by
92
		 * an unique name. You may use the name of your project for this. If
93
		 * you've implemented an alternative client class as well, "standard"
94
		 * should be replaced by the name of the new class.
95
		 *
96
		 * The email payment PDF client allows to use a different template for
97
		 * each payment status value. You can create a template for each payment
98
		 * status and store it in the "email/payment/<status number>/" directory
99
		 * below the "templates" directory (usually in client/html/templates). If no
100
		 * specific layout template is found, the common template in the
101
		 * "email/payment/" directory is used.
102
		 *
103
		 * @param string Relative path to the template creating code for the HTML e-mail body
104
		 * @since 2020.07
105
		 * @category Developer
106
		 * @see client/html/email/payment/pdf/standard/template-header
107
		 */
108
		$tplconf = 'client/html/email/payment/pdf/standard/template-body';
109
110
		$view->pdf = new Tcpdf( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false );
111
		$view->pdf->setCreator( PDF_CREATOR );
112
		$view->pdf->setAuthor( 'Aimeos' );
113
114
		$view->pdf->addPage();
115
		$view->pdf->writeHtml( $view->render( $view->config( $tplconf, 'email/payment/pdf-body-standard' ) ) );
116
		$view->pdf->lastPage();
117
118
		$view->mail()->addAttachment( $view->pdf->output( '', 'S' ), 'application/pdf', 'order_' . $view->extOrderItem->getId() . '.pdf' );
119
120
		return '';
121
	}
122
123
124
	/**
125
	 * Returns the sub-client given by its name.
126
	 *
127
	 * @param string $type Name of the client type
128
	 * @param string|null $name Name of the sub-client (Default if null)
129
	 * @return \Aimeos\Client\Html\Iface Sub-client object
130
	 */
131
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
132
	{
133
		/** client/html/email/payment/pdf/decorators/excludes
134
		 * Excludes decorators added by the "common" option from the "email payment pdf" html client
135
		 *
136
		 * Decorators extend the functionality of a class by adding new aspects
137
		 * (e.g. log what is currently done), executing the methods of the underlying
138
		 * class only in certain conditions (e.g. only for logged in users) or
139
		 * modify what is returned to the caller.
140
		 *
141
		 * This option allows you to remove a decorator added via
142
		 * "client/html/common/decorators/default" before they are wrapped
143
		 * around the html client.
144
		 *
145
		 *  client/html/email/payment/pdf/decorators/excludes = array( 'decorator1' )
146
		 *
147
		 * This would remove the decorator named "decorator1" from the list of
148
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
149
		 * "client/html/common/decorators/default" to the html client.
150
		 *
151
		 * @param array List of decorator names
152
		 * @since 2020.07
153
		 * @category Developer
154
		 * @see client/html/common/decorators/default
155
		 * @see client/html/email/payment/pdf/decorators/global
156
		 * @see client/html/email/payment/pdf/decorators/local
157
		 */
158
159
		/** client/html/email/payment/pdf/decorators/global
160
		 * Adds a list of globally available decorators only to the "email payment html" html client
161
		 *
162
		 * Decorators extend the functionality of a class by adding new aspects
163
		 * (e.g. log what is currently done), executing the methods of the underlying
164
		 * class only in certain conditions (e.g. only for logged in users) or
165
		 * modify what is returned to the caller.
166
		 *
167
		 * This option allows you to wrap global decorators
168
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
169
		 *
170
		 *  client/html/email/payment/pdf/decorators/global = array( 'decorator1' )
171
		 *
172
		 * This would add the decorator named "decorator1" defined by
173
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
174
		 *
175
		 * @param array List of decorator names
176
		 * @since 2020.07
177
		 * @category Developer
178
		 * @see client/html/common/decorators/default
179
		 * @see client/html/email/payment/pdf/decorators/excludes
180
		 * @see client/html/email/payment/pdf/decorators/local
181
		 */
182
183
		/** client/html/email/payment/pdf/decorators/local
184
		 * Adds a list of local decorators only to the "email payment html" html client
185
		 *
186
		 * Decorators extend the functionality of a class by adding new aspects
187
		 * (e.g. log what is currently done), executing the methods of the underlying
188
		 * class only in certain conditions (e.g. only for logged in users) or
189
		 * modify what is returned to the caller.
190
		 *
191
		 * This option allows you to wrap local decorators
192
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
193
		 *
194
		 *  client/html/email/payment/pdf/decorators/local = array( 'decorator2' )
195
		 *
196
		 * This would add the decorator named "decorator2" defined by
197
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
198
		 *
199
		 * @param array List of decorator names
200
		 * @since 2020.07
201
		 * @category Developer
202
		 * @see client/html/common/decorators/default
203
		 * @see client/html/email/payment/pdf/decorators/excludes
204
		 * @see client/html/email/payment/pdf/decorators/global
205
		 */
206
207
		return $this->createSubClient( 'email/payment/pdf/' . $type, $name );
208
	}
209
210
211
	/**
212
	 * Returns the list of sub-client names configured for the client.
213
	 *
214
	 * @return array List of HTML client names
215
	 */
216
	protected function getSubClientNames() : array
217
	{
218
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
219
	}
220
221
222
	/**
223
	 * Sets the necessary parameter values in the view.
224
	 *
225
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
226
	 * @param array &$tags Result array for the list of tags that are associated to the output
227
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
228
	 * @return \Aimeos\MW\View\Iface Modified view object
229
	 */
230
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
231
	{
232
		$basket = $view->extOrderBaseItem;
233
234
		// we can't cache the calculation because the same client object is used for all e-mails
235
		$view->summaryCostsDelivery = $this->getCostsDelivery( $basket );
236
		$view->summaryCostsPayment = $this->getCostsPayment( $basket );
237
		$view->summaryNamedTaxes = $this->getNamedTaxes( $basket );
238
		$view->summaryTaxRates = $this->getTaxRates( $basket );
239
		$view->summaryBasket = $basket;
240
241
		if( $view->extOrderItem->getPaymentStatus() >= $this->getDownloadPaymentStatus() ) {
242
			$view->summaryShowDownloadAttributes = true;
243
		}
244
245
		return parent::addData( $view, $tags, $expire );
246
	}
247
}
248