Passed
Branch master (4f99e3)
by Aimeos
04:48
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 76
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 Aimeos (aimeos.org), 2016-2018
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Account\Download;
12
13
14
/**
15
 * Default implementation of account download 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/account/download/standard/subparts
25
	 * List of HTML sub-clients rendered within the account download 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 2016.02
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/account/download/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( $uid = '' )
68
	{
69
		return '';
70
	}
71
72
73
	/**
74
	 * Returns the HTML string for insertion into the header.
75
	 *
76
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
77
	 * @return string|null String including HTML tags for the header on error
78
	 */
79
	public function getHeader( $uid = '' )
80
	{
81
		return '';
82
	}
83
84
85
	/**
86
	 * Returns the sub-client given by its name.
87
	 *
88
	 * @param string $type Name of the client type
89
	 * @param string|null $name Name of the sub-client (Default if null)
90
	 * @return \Aimeos\Client\Html\Iface Sub-client object
91
	 */
92
	public function getSubClient( $type, $name = null )
93
	{
94
		/** client/html/account/download/decorators/excludes
95
		 * Excludes decorators added by the "common" option from the account download html client
96
		 *
97
		 * Decorators extend the functionality of a class by adding new aspects
98
		 * (e.g. log what is currently done), executing the methods of the underlying
99
		 * class only in certain conditions (e.g. only for logged in users) or
100
		 * modify what is returned to the caller.
101
		 *
102
		 * This option allows you to remove a decorator added via
103
		 * "client/html/common/decorators/default" before they are wrapped
104
		 * around the html client.
105
		 *
106
		 *  client/html/account/download/decorators/excludes = array( 'decorator1' )
107
		 *
108
		 * This would remove the decorator named "decorator1" from the list of
109
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
110
		 * "client/html/common/decorators/default" to the html client.
111
		 *
112
		 * @param array List of decorator names
113
		 * @since 2016.02
114
		 * @category Developer
115
		 * @see client/html/common/decorators/default
116
		 * @see client/html/account/download/decorators/global
117
		 * @see client/html/account/download/decorators/local
118
		 */
119
120
		/** client/html/account/download/decorators/global
121
		 * Adds a list of globally available decorators only to the account download html client
122
		 *
123
		 * Decorators extend the functionality of a class by adding new aspects
124
		 * (e.g. log what is currently done), executing the methods of the underlying
125
		 * class only in certain conditions (e.g. only for logged in users) or
126
		 * modify what is returned to the caller.
127
		 *
128
		 * This option allows you to wrap global decorators
129
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
130
		 *
131
		 *  client/html/account/download/decorators/global = array( 'decorator1' )
132
		 *
133
		 * This would add the decorator named "decorator1" defined by
134
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
135
		 *
136
		 * @param array List of decorator names
137
		 * @since 2016.02
138
		 * @category Developer
139
		 * @see client/html/common/decorators/default
140
		 * @see client/html/account/download/decorators/excludes
141
		 * @see client/html/account/download/decorators/local
142
		 */
143
144
		/** client/html/account/download/decorators/local
145
		 * Adds a list of local decorators only to the account download html client
146
		 *
147
		 * Decorators extend the functionality of a class by adding new aspects
148
		 * (e.g. log what is currently done), executing the methods of the underlying
149
		 * class only in certain conditions (e.g. only for logged in users) or
150
		 * modify what is returned to the caller.
151
		 *
152
		 * This option allows you to wrap local decorators
153
		 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
154
		 *
155
		 *  client/html/account/download/decorators/local = array( 'decorator2' )
156
		 *
157
		 * This would add the decorator named "decorator2" defined by
158
		 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
159
		 *
160
		 * @param array List of decorator names
161
		 * @since 2016.02
162
		 * @category Developer
163
		 * @see client/html/common/decorators/default
164
		 * @see client/html/account/download/decorators/excludes
165
		 * @see client/html/account/download/decorators/global
166
		 */
167
		return $this->createSubClient( 'account/download/' . $type, $name );
168
	}
169
170
171
	/**
172
	 * Processes the input, e.g. store given values.
173
	 * A view must be available and this method doesn't generate any output
174
	 * besides setting view variables.
175
	 */
176
	public function process()
177
	{
178
		$context = $this->getContext();
179
180
		try
181
		{
182
			$view = $this->getView();
183
			$id = $view->param( 'dl_id' );
184
			$customerId = $context->getUserId();
185
			$target = $context->getConfig()->get( 'client/html/account/download/error/url/target' );
186
187
			if( $this->checkAccess( $customerId, $id ) === false )
188
			{
189
				$view->response()->withStatus( 401 )->withHeader( 'Location', $view->url( $target ) );
190
				return;
191
			}
192
193
			$manager = \Aimeos\MShop::create( $context, 'order/base/product/attribute' );
194
			$item = $manager->getItem( $id );
195
196
			if( $this->checkDownload( $context->getUserId(), $id ) === true ) {
197
				$this->addDownload( $item );
198
			} else {
199
				$view->response()->withStatus( 403 )->withHeader( 'Location', $view->url( $target ) );
200
			}
201
202
			parent::process();
203
		}
204
		catch( \Exception $e )
205
		{
206
			$this->logException( $e );
207
		}
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()
217
	{
218
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
219
	}
220
221
222
	/**
223
	 * Adds the necessary headers and the download content to the reponse object
224
	 *
225
	 * @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface $item Order product attribute item with file reference
226
	 */
227
	protected function addDownload( \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface $item )
228
	{
229
		$fs = $this->getContext()->getFilesystemManager()->get( 'fs-secure' );
230
		$response = $this->getView()->response();
231
		$value = (string) $item->getValue();
232
233
		if( $fs->has( $value ) )
234
		{
235
			$name = $item->getName();
236
237
			if( pathinfo( $name, PATHINFO_EXTENSION ) == null
238
					&& ( $ext = pathinfo( $value, PATHINFO_EXTENSION ) ) != null
239
			) {
240
				$name .= '.' . $ext;
241
			}
242
243
			$response->withHeader( 'Content-Description', 'File Transfer' );
244
			$response->withHeader( 'Content-Type', 'application/octet-stream' );
245
			$response->withHeader( 'Content-Disposition', 'attachment; filename="' . $name . '"' );
246
			$response->withHeader( 'Content-Length', (string) $fs->size( $value ) );
247
			$response->withHeader( 'Cache-Control', 'must-revalidate' );
248
			$response->withHeader( 'Pragma', 'private' );
249
			$response->withHeader( 'Expires', '0' );
250
251
			$response->withBody( $response->createStream( $fs->reads( $value ) ) );
252
		}
253
		elseif( filter_var( $value, FILTER_VALIDATE_URL ) !== false )
254
		{
255
			$response->withHeader( 'Location', $value );
256
			$response->withStatus( 303 );
257
		}
258
		else
259
		{
260
			$response->withStatus( 404 );
261
		}
262
	}
263
264
265
	/**
266
	 * Checks if the customer is allowed to download the file
267
	 *
268
	 * @param string $customerId Unique customer ID
269
	 * @param string $id Unique order base product attribute ID referencing the download file
270
	 * @return boolean True if download is allowed, false if not
271
	 */
272
	protected function checkAccess( $customerId, $id )
273
	{
274
		if( $customerId !== null && $id !== null )
0 ignored issues
show
introduced by
The condition $id !== null is always true.
Loading history...
275
		{
276
			$manager = \Aimeos\MShop::create( $this->getContext(), 'order/base' );
277
278
			$search = $manager->createSearch();
279
			$expr = array(
280
				$search->compare( '==', 'order.base.customerid', $customerId ),
281
				$search->compare( '==', 'order.base.product.attribute.id', $id ),
282
			);
283
			$search->setConditions( $search->combine( '&&', $expr ) );
284
			$search->setSlice( 0, 1 );
285
286
			if( count( $manager->searchItems( $search ) ) > 0 ) {
287
				return true;
288
			}
289
		}
290
291
		return false;
292
	}
293
294
295
	/**
296
	 * Updates the download counter for the downloaded file
297
	 *
298
	 * @param string $customerId Unique customer ID
299
	 * @param string $id Unique order base product attribute ID referencing the download file
300
	 * @return boolean True if download is allowed, false if not
301
	 */
302
	protected function checkDownload( $customerId, $id )
303
	{
304
		$context = $this->getContext();
305
306
		/** client/html/account/download/maxcount
307
		 * Maximum number of file downloads allowed for an ordered product
308
		 *
309
		 * This configuration setting enables you to limit the number of downloads
310
		 * of a product download file. The count is the maximum number for each
311
		 * bought product and customer, i.e. setting the count to "3" allows
312
		 * a customer to download the bought product file up to three times.
313
		 *
314
		 * The default value of null enforces no limit.
315
		 *
316
		 * @param integer Maximum number of downloads
317
		 * @since 2016.02
318
		 * @category Developer
319
		 * @category User
320
		 */
321
		$maxcnt = $context->getConfig()->get( 'client/html/account/download/maxcount' );
322
323
		$listItem = $this->getListItem( $customerId, $id );
324
		$config = $listItem->getConfig();
325
326
		if( !isset( $config['count'] ) ) {
327
			$config['count'] = 0;
328
		}
329
330
		if( $maxcnt === null || ((int) $config['count']) < $maxcnt )
331
		{
332
			$config['count']++;
333
			$listItem->setConfig( $config );
334
335
			$manager = \Aimeos\MShop::create( $context, 'customer/lists' );
336
			$manager->saveItem( $listItem, false );
337
338
			return true;
339
		}
340
341
		return false;
342
	}
343
344
345
	/**
346
	 * Returns the list item storing the download counter
347
	 *
348
	 * @param string $customerId Unique customer ID
349
	 * @param string $refId Unique order base product attribute ID referencing the download file
350
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface List item object
351
	 */
352
	protected function getListItem( $customerId, $refId )
353
	{
354
		$context = $this->getContext();
355
		$manager = \Aimeos\MShop::create( $context, 'customer/lists' );
356
357
		$search = $manager->createSearch();
358
		$expr = array(
359
			$search->compare( '==', 'customer.lists.parentid', $customerId ),
360
			$search->compare( '==', 'customer.lists.refid', $refId ),
361
			$search->compare( '==', 'customer.lists.domain', 'order' ),
362
			$search->compare( '==', 'customer.lists.type', 'download' ),
363
		);
364
		$search->setConditions( $search->combine( '&&', $expr ) );
365
366
		$listItems = $manager->searchItems( $search );
367
368
		if( ( $listItem = reset( $listItems ) ) === false )
369
		{
370
			$listItem = $manager->createItem();
371
			$listItem->setParentId( $customerId );
0 ignored issues
show
Bug introduced by
The method setParentId() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

371
			$listItem->/** @scrutinizer ignore-call */ 
372
              setParentId( $customerId );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
372
			$listItem->setType( 'download' );
373
			$listItem->setDomain( 'order' );
374
			$listItem->setRefId( $refId );
0 ignored issues
show
Bug introduced by
The method setRefId() does not exist on Aimeos\MShop\Attribute\Item\Iface. Did you maybe mean setId()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

374
			$listItem->/** @scrutinizer ignore-call */ 
375
              setRefId( $refId );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
375
		}
376
377
		return $listItem;
378
	}
379
}
380