Passed
Push — master ( 596853...253ba3 )
by Aimeos
16:40
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-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Download;
12
13
sprintf( 'download' ); // for translation
14
15
16
/**
17
 * Default implementation of product download JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/product/download/name
27
	 * Name of the download subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Download\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2016.04
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string|null HTML output
42
	 */
43
	public function copy() : ?string
44
	{
45
		$view = $this->getObject()->addData( $this->getView() );
46
		$view->downloadData = $this->toArray( $view->item, true );
47
		$view->downloadBody = parent::copy();
48
49
		return $this->render( $view );
50
	}
51
52
53
	/**
54
	 * Creates a new resource
55
	 *
56
	 * @return string|null HTML output
57
	 */
58
	public function create() : ?string
59
	{
60
		$view = $this->getObject()->addData( $this->getView() );
61
		$data['product.lists.siteid'] = $this->getContext()->getLocale()->getSiteId();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
62
		$data = array_replace_recursive( $this->toArray( $view->item ), $view->param( 'download', [] ) );
63
64
		$view->downloadData = $data;
65
		$view->downloadBody = parent::create();
66
67
		return $this->render( $view );
68
	}
69
70
71
	/**
72
	 * Deletes a resource
73
	 *
74
	 * @return string|null HTML output
75
	 */
76
	public function delete() : ?string
77
	{
78
		parent::delete();
79
80
		$item = $this->getView()->item;
81
		$fs = $this->getContext()->getFilesystemManager()->get( 'fs-secure' );
82
83
		foreach( $item->getListItems( 'attribute', 'hidden', 'download', false ) as $listItem )
84
		{
85
			$refItem = $listItem->getRefItem();
86
87
			if( $refItem !== null && $refItem->getCode() != '' && $fs->has( $refItem->getCode() ) ) {
88
				$fs->rm( $refItem->getCode() );
89
			}
90
91
			$item->deleteListItem( 'attribute', $listItem, $refItem );
92
		}
93
94
		return null;
95
	}
96
97
98
	/**
99
	 * Returns a single resource
100
	 *
101
	 * @return string|null HTML output
102
	 */
103
	public function get() : ?string
104
	{
105
		$view = $this->getObject()->addData( $this->getView() );
106
		$view->downloadData = $this->toArray( $view->item );
107
		$view->downloadBody = parent::get();
108
109
		return $this->render( $view );
110
	}
111
112
113
	/**
114
	 * Saves the data
115
	 *
116
	 * @return string|null HTML output
117
	 */
118
	public function save() : ?string
119
	{
120
		$view = $this->getView();
121
122
		$this->fromArray( $view->item, $view->param( 'download', [] ) );
123
		$view->downloadBody = parent::save();
124
125
		return null;
126
	}
127
128
129
	/**
130
	 * Returns the sub-client given by its name.
131
	 *
132
	 * @param string $type Name of the client type
133
	 * @param string|null $name Name of the sub-client (Default if null)
134
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
135
	 */
136
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
137
	{
138
		/** admin/jqadm/product/download/decorators/excludes
139
		 * Excludes decorators added by the "common" option from the product JQAdm client
140
		 *
141
		 * Decorators extend the functionality of a class by adding new aspects
142
		 * (e.g. log what is currently done), executing the methods of the underlying
143
		 * class only in certain conditions (e.g. only for logged in users) or
144
		 * modify what is returned to the caller.
145
		 *
146
		 * This option allows you to remove a decorator added via
147
		 * "admin/jqadm/common/decorators/default" before they are wrapped
148
		 * around the JQAdm client.
149
		 *
150
		 *  admin/jqadm/product/download/decorators/excludes = array( 'decorator1' )
151
		 *
152
		 * This would remove the decorator named "decorator1" from the list of
153
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
154
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
155
		 *
156
		 * @param array List of decorator names
157
		 * @since 2016.03
158
		 * @category Developer
159
		 * @see admin/jqadm/common/decorators/default
160
		 * @see admin/jqadm/product/download/decorators/global
161
		 * @see admin/jqadm/product/download/decorators/local
162
		 */
163
164
		/** admin/jqadm/product/download/decorators/global
165
		 * Adds a list of globally available decorators only to the product JQAdm client
166
		 *
167
		 * Decorators extend the functionality of a class by adding new aspects
168
		 * (e.g. log what is currently done), executing the methods of the underlying
169
		 * class only in certain conditions (e.g. only for logged in users) or
170
		 * modify what is returned to the caller.
171
		 *
172
		 * This option allows you to wrap global decorators
173
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
174
		 *
175
		 *  admin/jqadm/product/download/decorators/global = array( 'decorator1' )
176
		 *
177
		 * This would add the decorator named "decorator1" defined by
178
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
179
		 *
180
		 * @param array List of decorator names
181
		 * @since 2016.03
182
		 * @category Developer
183
		 * @see admin/jqadm/common/decorators/default
184
		 * @see admin/jqadm/product/download/decorators/excludes
185
		 * @see admin/jqadm/product/download/decorators/local
186
		 */
187
188
		/** admin/jqadm/product/download/decorators/local
189
		 * Adds a list of local decorators only to the product JQAdm client
190
		 *
191
		 * Decorators extend the functionality of a class by adding new aspects
192
		 * (e.g. log what is currently done), executing the methods of the underlying
193
		 * class only in certain conditions (e.g. only for logged in users) or
194
		 * modify what is returned to the caller.
195
		 *
196
		 * This option allows you to wrap local decorators
197
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
198
		 *
199
		 *  admin/jqadm/product/download/decorators/local = array( 'decorator2' )
200
		 *
201
		 * This would add the decorator named "decorator2" defined by
202
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
203
		 *
204
		 * @param array List of decorator names
205
		 * @since 2016.03
206
		 * @category Developer
207
		 * @see admin/jqadm/common/decorators/default
208
		 * @see admin/jqadm/product/download/decorators/excludes
209
		 * @see admin/jqadm/product/download/decorators/global
210
		 */
211
		return $this->createSubClient( 'product/download/' . $type, $name );
212
	}
213
214
215
	/**
216
	 * Returns the list of sub-client names configured for the client.
217
	 *
218
	 * @return array List of JQAdm client names
219
	 */
220
	protected function getSubClientNames() : array
221
	{
222
		/** admin/jqadm/product/download/subparts
223
		 * List of JQAdm sub-clients rendered within the product download section
224
		 *
225
		 * The output of the frontend is composed of the code generated by the JQAdm
226
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
227
		 * that are responsible for rendering certain sub-parts of the output. The
228
		 * sub-clients can contain JQAdm clients themselves and therefore a
229
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
230
		 * the output that is placed inside the container of its parent.
231
		 *
232
		 * At first, always the JQAdm code generated by the parent is printed, then
233
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
234
		 * determines the order of the output of these sub-clients inside the parent
235
		 * container. If the configured list of clients is
236
		 *
237
		 *  array( "subclient1", "subclient2" )
238
		 *
239
		 * you can easily change the order of the output by reordering the subparts:
240
		 *
241
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
242
		 *
243
		 * You can also remove one or more parts if they shouldn't be rendered:
244
		 *
245
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
246
		 *
247
		 * As the clients only generates structural JQAdm, the layout defined via CSS
248
		 * should support adding, removing or reordering content by a fluid like
249
		 * design.
250
		 *
251
		 * @param array List of sub-client names
252
		 * @since 2016.03
253
		 * @category Developer
254
		 */
255
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/download/subparts', [] );
256
	}
257
258
259
	/**
260
	 * Stores the uploaded file in the "fs-secure" file system
261
	 *
262
	 * @param \Psr\Http\Message\UploadedFileInterface $file
263
	 * @param string|null $path Path the file should be stored at
264
	 * @return string Path to the uploaded file
265
	 */
266
	protected function storeFile( \Psr\Http\Message\UploadedFileInterface $file, string $path = null ) : string
267
	{
268
		$fs = $this->getContext()->getFilesystemManager()->get( 'fs-secure' );
269
270
		if( $path === null )
271
		{
272
			$ext = pathinfo( $file->getClientFilename(), PATHINFO_EXTENSION );
0 ignored issues
show
Bug introduced by
It seems like $file->getClientFilename() can also be of type null; however, parameter $path of pathinfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

272
			$ext = pathinfo( /** @scrutinizer ignore-type */ $file->getClientFilename(), PATHINFO_EXTENSION );
Loading history...
273
			$hash = md5( $file->getClientFilename() . microtime( true ) );
274
			$path = sprintf( '%s/%s/%s.%s', $hash[0], $hash[1], $hash, $ext );
0 ignored issues
show
Bug introduced by
It seems like $ext can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

274
			$path = sprintf( '%s/%s/%s.%s', $hash[0], $hash[1], $hash, /** @scrutinizer ignore-type */ $ext );
Loading history...
275
276
			if( !$fs->isdir( $hash[0] . '/' . $hash[1] ) ) {
277
				$fs->mkdir( $hash[0] . '/' . $hash[1] );
278
			}
279
		}
280
281
		$fs->writes( $path, $file->getStream()->detach() );
282
283
		return $path;
284
	}
285
286
287
	/**
288
	 * Creates new and updates existing items using the data array
289
	 *
290
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
291
	 * @param array $data Data array
292
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
293
	 */
294
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
295
	{
296
		$context = $this->getContext();
297
		$fs = $context->getFilesystemManager()->get( 'fs-secure' );
298
299
		$prodManager = \Aimeos\MShop::create( $context, 'product' );
300
		$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
301
		$listItems = $item->getListItems( 'attribute', 'hidden', 'download', false );
302
303
		if( $this->getValue( $data, 'attribute.label' ) != '' )
304
		{
305
			$listId = $this->getValue( $data, 'product.lists.id' );
306
307
			if( isset( $listItems[$listId] ) ) {
308
				$litem = $listItems[$listId]; unset( $listItems[$listId] );
309
			} else {
310
				$litem = $prodManager->createListItem()->setType( 'hidden' );
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

310
				$litem = $prodManager->/** @scrutinizer ignore-call */ createListItem()->setType( 'hidden' );

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...
311
			}
312
313
			if( ( $refItem = $litem->getRefItem() ) === null ) {
314
				$refItem = $attrManager->create()->setType( 'download' );
315
			}
316
317
			$litem->fromArray( $data, true );
318
			$refItem->fromArray( $data, true );
319
320
			if( ( $file = $this->getValue( (array) $this->getView()->request()->getUploadedFiles(), 'download/file' ) ) !== null
321
				&& $file->getError() === UPLOAD_ERR_OK
322
			) {
323
				$path = ( $this->getValue( $data, 'overwrite' ) == 1 ? $refItem->getCode() : null );
324
				$refItem->setCode( $this->storeFile( $file, $path ) );
325
			}
326
327
			$item->addListItem( 'attribute', $litem, $refItem );
328
		}
329
330
		foreach( $listItems as $listItem )
331
		{
332
			$refItem = $listItem->getRefItem();
333
334
			if( $refItem !== null && ( $path = $refItem->getCode() ) != '' && $fs->has( $path ) ) {
335
				$fs->rm( $path );
336
			}
337
338
			$item->deleteListItem( 'attribute', $listItem, $refItem );
339
		}
340
341
		return $item;
342
	}
343
344
345
	/**
346
	 * Constructs the data array for the view from the given item
347
	 *
348
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
349
	 * @param bool $copy True if items should be copied, false if not
350
	 * @return string[] Multi-dimensional associative list of item data
351
	 */
352
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
353
	{
354
		$siteId = $this->getContext()->getLocale()->getSiteId();
355
		$data = ['product.lists.siteid' => $siteId];
356
357
		foreach( $item->getListItems( 'attribute', 'hidden', 'download', false ) as $listItem )
358
		{
359
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
360
				continue;
361
			}
362
363
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
364
365
			if( $copy === true ) {
366
				$list['product.lists.id'] = '';
367
			}
368
369
			foreach( $list as $key => $value ) {
370
				$data[$key] = $value;
371
			}
372
373
			try
374
			{
375
				$fs = $this->getContext()->getFilesystemManager()->get( 'fs-secure' );
376
377
				$data['time'] = $fs->time( $data['attribute.code'] );
378
				$data['size'] = $fs->size( $data['attribute.code'] );
379
			}
380
			catch( \Exception $e ) { ; } // Show product even if file isn't available any more
381
		}
382
383
		return $data;
384
	}
385
386
387
	/**
388
	 * Returns the rendered template including the view data
389
	 *
390
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
391
	 * @return string HTML output
392
	 */
393
	protected function render( \Aimeos\MW\View\Iface $view ) : string
394
	{
395
		/** admin/jqadm/product/download/template-item
396
		 * Relative path to the HTML body template of the download subpart for products.
397
		 *
398
		 * The template file contains the HTML code and processing instructions
399
		 * to generate the result shown in the body of the frontend. The
400
		 * configuration string is the path to the template file relative
401
		 * to the templates directory (usually in admin/jqadm/templates).
402
		 *
403
		 * You can overwrite the template file configuration in extensions and
404
		 * provide alternative templates. These alternative templates should be
405
		 * named like the default one but with the string "default" replaced by
406
		 * an unique name. You may use the name of your project for this. If
407
		 * you've implemented an alternative client class as well, "default"
408
		 * should be replaced by the name of the new class.
409
		 *
410
		 * @param string Relative path to the template creating the HTML code
411
		 * @since 2016.04
412
		 * @category Developer
413
		 */
414
		$tplconf = 'admin/jqadm/product/download/template-item';
415
		$default = 'product/item-download-standard';
416
417
		return $view->render( $view->config( $tplconf, $default ) );
418
	}
419
}
420