Standard   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 379
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 80
dl 0
loc 379
rs 9.84
c 0
b 0
f 0
wmc 32

11 Methods

Rating   Name   Duplication   Size   Complexity  
A storeFile() 0 18 3
A render() 0 24 1
A create() 0 10 1
A getSubClientNames() 0 35 1
A getSubClient() 0 73 1
A save() 0 8 1
A delete() 0 19 5
B fromArray() 0 41 11
A copy() 0 7 1
A toArray() 0 28 6
A get() 0 7 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
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
	 */
35
36
37
	/**
38
	 * Copies a resource
39
	 *
40
	 * @return string|null HTML output
41
	 */
42
	public function copy() : ?string
43
	{
44
		$view = $this->object()->data( $this->view() );
45
		$view->downloadData = $this->toArray( $view->item, true );
46
		$view->downloadBody = parent::copy();
47
48
		return $this->render( $view );
49
	}
50
51
52
	/**
53
	 * Creates a new resource
54
	 *
55
	 * @return string|null HTML output
56
	 */
57
	public function create() : ?string
58
	{
59
		$view = $this->object()->data( $this->view() );
60
		$data['product.lists.siteid'] = $this->context()->locale()->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...
61
		$data = array_replace_recursive( $this->toArray( $view->item ), $view->param( 'download', [] ) );
62
63
		$view->downloadData = $data;
64
		$view->downloadBody = parent::create();
65
66
		return $this->render( $view );
67
	}
68
69
70
	/**
71
	 * Deletes a resource
72
	 *
73
	 * @return string|null HTML output
74
	 */
75
	public function delete() : ?string
76
	{
77
		parent::delete();
78
79
		$item = $this->view()->item;
80
		$fs = $this->context()->fs( 'fs-secure' );
81
82
		foreach( $item->getListItems( 'attribute', 'hidden', 'download', false ) as $listItem )
83
		{
84
			$refItem = $listItem->getRefItem();
85
86
			if( $refItem !== null && $refItem->getCode() != '' && $fs->has( $refItem->getCode() ) ) {
87
				$fs->rm( $refItem->getCode() );
88
			}
89
90
			$item->deleteListItem( 'attribute', $listItem, $refItem );
91
		}
92
93
		return null;
94
	}
95
96
97
	/**
98
	 * Returns a single resource
99
	 *
100
	 * @return string|null HTML output
101
	 */
102
	public function get() : ?string
103
	{
104
		$view = $this->object()->data( $this->view() );
105
		$view->downloadData = $this->toArray( $view->item );
106
		$view->downloadBody = parent::get();
107
108
		return $this->render( $view );
109
	}
110
111
112
	/**
113
	 * Saves the data
114
	 *
115
	 * @return string|null HTML output
116
	 */
117
	public function save() : ?string
118
	{
119
		$view = $this->view();
120
121
		$this->fromArray( $view->item, $view->param( 'download', [] ) );
122
		$view->downloadBody = parent::save();
123
124
		return null;
125
	}
126
127
128
	/**
129
	 * Returns the sub-client given by its name.
130
	 *
131
	 * @param string $type Name of the client type
132
	 * @param string|null $name Name of the sub-client (Default if null)
133
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
134
	 */
135
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
136
	{
137
		/** admin/jqadm/product/download/decorators/excludes
138
		 * Excludes decorators added by the "common" option from the product JQAdm client
139
		 *
140
		 * Decorators extend the functionality of a class by adding new aspects
141
		 * (e.g. log what is currently done), executing the methods of the underlying
142
		 * class only in certain conditions (e.g. only for logged in users) or
143
		 * modify what is returned to the caller.
144
		 *
145
		 * This option allows you to remove a decorator added via
146
		 * "admin/jqadm/common/decorators/default" before they are wrapped
147
		 * around the JQAdm client.
148
		 *
149
		 *  admin/jqadm/product/download/decorators/excludes = array( 'decorator1' )
150
		 *
151
		 * This would remove the decorator named "decorator1" from the list of
152
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
153
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2016.03
157
		 * @see admin/jqadm/common/decorators/default
158
		 * @see admin/jqadm/product/download/decorators/global
159
		 * @see admin/jqadm/product/download/decorators/local
160
		 */
161
162
		/** admin/jqadm/product/download/decorators/global
163
		 * Adds a list of globally available decorators only to the product JQAdm client
164
		 *
165
		 * Decorators extend the functionality of a class by adding new aspects
166
		 * (e.g. log what is currently done), executing the methods of the underlying
167
		 * class only in certain conditions (e.g. only for logged in users) or
168
		 * modify what is returned to the caller.
169
		 *
170
		 * This option allows you to wrap global decorators
171
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
172
		 *
173
		 *  admin/jqadm/product/download/decorators/global = array( 'decorator1' )
174
		 *
175
		 * This would add the decorator named "decorator1" defined by
176
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
177
		 *
178
		 * @param array List of decorator names
179
		 * @since 2016.03
180
		 * @see admin/jqadm/common/decorators/default
181
		 * @see admin/jqadm/product/download/decorators/excludes
182
		 * @see admin/jqadm/product/download/decorators/local
183
		 */
184
185
		/** admin/jqadm/product/download/decorators/local
186
		 * Adds a list of local decorators only to the product JQAdm client
187
		 *
188
		 * Decorators extend the functionality of a class by adding new aspects
189
		 * (e.g. log what is currently done), executing the methods of the underlying
190
		 * class only in certain conditions (e.g. only for logged in users) or
191
		 * modify what is returned to the caller.
192
		 *
193
		 * This option allows you to wrap local decorators
194
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
195
		 *
196
		 *  admin/jqadm/product/download/decorators/local = array( 'decorator2' )
197
		 *
198
		 * This would add the decorator named "decorator2" defined by
199
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
200
		 *
201
		 * @param array List of decorator names
202
		 * @since 2016.03
203
		 * @see admin/jqadm/common/decorators/default
204
		 * @see admin/jqadm/product/download/decorators/excludes
205
		 * @see admin/jqadm/product/download/decorators/global
206
		 */
207
		return $this->createSubClient( 'product/download/' . $type, $name );
208
	}
209
210
211
	/**
212
	 * Returns the list of sub-client names configured for the client.
213
	 *
214
	 * @return array List of JQAdm client names
215
	 */
216
	protected function getSubClientNames() : array
217
	{
218
		/** admin/jqadm/product/download/subparts
219
		 * List of JQAdm sub-clients rendered within the product download section
220
		 *
221
		 * The output of the frontend is composed of the code generated by the JQAdm
222
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
223
		 * that are responsible for rendering certain sub-parts of the output. The
224
		 * sub-clients can contain JQAdm clients themselves and therefore a
225
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
226
		 * the output that is placed inside the container of its parent.
227
		 *
228
		 * At first, always the JQAdm code generated by the parent is printed, then
229
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
230
		 * determines the order of the output of these sub-clients inside the parent
231
		 * container. If the configured list of clients is
232
		 *
233
		 *  array( "subclient1", "subclient2" )
234
		 *
235
		 * you can easily change the order of the output by reordering the subparts:
236
		 *
237
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
238
		 *
239
		 * You can also remove one or more parts if they shouldn't be rendered:
240
		 *
241
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
242
		 *
243
		 * As the clients only generates structural JQAdm, the layout defined via CSS
244
		 * should support adding, removing or reordering content by a fluid like
245
		 * design.
246
		 *
247
		 * @param array List of sub-client names
248
		 * @since 2016.03
249
		 */
250
		return $this->context()->config()->get( 'admin/jqadm/product/download/subparts', [] );
251
	}
252
253
254
	/**
255
	 * Stores the uploaded file in the "fs-secure" file system
256
	 *
257
	 * @param \Psr\Http\Message\UploadedFileInterface $file
258
	 * @param string|null $path Path the file should be stored at
259
	 * @return string Path to the uploaded file
260
	 */
261
	protected function storeFile( \Psr\Http\Message\UploadedFileInterface $file, ?string $path = null ) : string
262
	{
263
		$fs = $this->context()->fs( 'fs-secure' );
264
265
		if( $path === null )
266
		{
267
			$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

267
			$ext = pathinfo( /** @scrutinizer ignore-type */ $file->getClientFilename(), PATHINFO_EXTENSION );
Loading history...
268
			$hash = md5( $file->getClientFilename() . microtime( true ) );
269
			$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

269
			$path = sprintf( '%s/%s/%s.%s', $hash[0], $hash[1], $hash, /** @scrutinizer ignore-type */ $ext );
Loading history...
270
271
			if( !$fs->isdir( $hash[0] . '/' . $hash[1] ) ) {
272
				$fs->mkdir( $hash[0] . '/' . $hash[1] );
273
			}
274
		}
275
276
		$fs->writes( $path, $file->getStream()->detach() );
277
278
		return $path;
279
	}
280
281
282
	/**
283
	 * Creates new and updates existing items using the data array
284
	 *
285
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
286
	 * @param array $data Data array
287
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
288
	 */
289
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
290
	{
291
		$context = $this->context();
292
		$fs = $context->fs( 'fs-secure' );
293
294
		$prodManager = \Aimeos\MShop::create( $context, 'product' );
295
		$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
296
		$listItems = $item->getListItems( 'attribute', 'hidden', 'download', false );
297
298
		if( $this->val( $data, 'attribute.label' ) != '' )
299
		{
300
			$listId = $this->val( $data, 'product.lists.id' );
301
302
			$litem = $listItems->pull( $listId ) ?: $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

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