Passed
Branch master (d032f7)
by Aimeos
05:30
created

Standard   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 416
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 416
rs 9.84
c 0
b 0
f 0
wmc 32

12 Methods

Rating   Name   Duplication   Size   Complexity  
B checkFileUpload() 0 21 9
A getSubClient() 0 76 1
A getCodeItems() 0 15 1
A render() 0 25 1
A toArray() 0 12 3
A get() 0 32 4
A save() 0 38 4
A getSubClientNames() 0 36 1
A storeFile() 0 19 4
A copy() 0 3 1
A fromArray() 0 16 2
A create() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Coupon\Code;
12
13
sprintf( 'code' ); // for translation
14
15
16
/**
17
 * Default implementation of coupon code 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/coupon/code/name
27
	 * Name of the code subpart used by the JQAdm coupon implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Coupon\Code\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 2017.07
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string HTML output
42
	 */
43
	public function copy()
44
	{
45
		return $this->render( $this->getView() );
46
	}
47
48
49
	/**
50
	 * Creates a new resource
51
	 *
52
	 * @return string HTML output
53
	 */
54
	public function create()
55
	{
56
		return $this->render( $this->getView() );
57
	}
58
59
60
	/**
61
	 * Returns a single resource
62
	 *
63
	 * @return string HTML output
64
	 */
65
	public function get()
66
	{
67
		$view = $this->getView();
68
69
		try
70
		{
71
			$total = 0;
72
			$params = $this->storeSearchParams( $view->param( 'vc', [] ), 'couponcode' );
73
			$codeItems = $this->getCodeItems( $view->item, $params, $total );
74
75
			$view->codeData = $this->toArray( $codeItems );
76
			$view->codeTotal = $total;
77
			$view->codeBody = '';
78
79
			foreach( $this->getSubClients() as $client ) {
80
				$view->codeBody .= $client->search();
81
			}
82
		}
83
		catch( \Aimeos\MShop\Exception $e )
84
		{
85
			$error = array( 'coupon-item-code' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
86
			$view->errors = $view->get( 'errors', [] ) + $error;
87
			$this->logException( $e );
88
		}
89
		catch( \Exception $e )
90
		{
91
			$error = array( 'coupon-item-code' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
92
			$view->errors = $view->get( 'errors', [] ) + $error;
93
			$this->logException( $e );
94
		}
95
96
		return $this->render( $view );
97
	}
98
99
100
	/**
101
	 * Saves the data
102
	 */
103
	public function save()
104
	{
105
		$view = $this->getView();
106
		$context = $this->getContext();
107
108
		$manager = \Aimeos\MShop::create( $context, 'coupon/code' );
109
		$manager->begin();
110
111
		try
112
		{
113
			$this->storeSearchParams( $view->param( 'vc', [] ), 'couponcode' );
114
			$this->storeFile( $view->item, (array) $view->request()->getUploadedFiles() );
115
			$this->fromArray( $view->item, $view->param( 'code', [] ) );
116
			$view->couponBody = '';
117
118
			foreach( $this->getSubClients() as $client ) {
119
				$view->couponBody .= $client->save();
120
			}
121
122
			$manager->commit();
123
			return;
124
		}
125
		catch( \Aimeos\MShop\Exception $e )
126
		{
127
			$error = array( 'coupon-item-code' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
128
			$view->errors = $view->get( 'errors', [] ) + $error;
129
			$this->logException( $e );
130
		}
131
		catch( \Exception $e )
132
		{
133
			$error = array( 'coupon-item-code' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
134
			$view->errors = $view->get( 'errors', [] ) + $error;
135
			$this->logException( $e );
136
		}
137
138
		$manager->rollback();
139
140
		throw new \Aimeos\Admin\JQAdm\Exception();
141
	}
142
143
144
	/**
145
	 * Returns the sub-client given by its name.
146
	 *
147
	 * @param string $type Name of the client type
148
	 * @param string|null $name Name of the sub-client (Default if null)
149
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
150
	 */
151
	public function getSubClient( $type, $name = null )
152
	{
153
		/** admin/jqadm/coupon/code/decorators/excludes
154
		 * Excludes decorators added by the "common" option from the coupon JQAdm client
155
		 *
156
		 * Decorators extend the functionality of a class by adding new aspects
157
		 * (e.g. log what is currently done), executing the methods of the underlying
158
		 * class only in certain conditions (e.g. only for logged in users) or
159
		 * modify what is returned to the caller.
160
		 *
161
		 * This option allows you to remove a decorator added via
162
		 * "admin/jqadm/common/decorators/default" before they are wrapped
163
		 * around the JQAdm client.
164
		 *
165
		 *  admin/jqadm/coupon/code/decorators/excludes = array( 'decorator1' )
166
		 *
167
		 * This would remove the decorator named "decorator1" from the list of
168
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
169
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
170
		 *
171
		 * @param array List of decorator names
172
		 * @since 2017.07
173
		 * @category Developer
174
		 * @see admin/jqadm/common/decorators/default
175
		 * @see admin/jqadm/coupon/code/decorators/global
176
		 * @see admin/jqadm/coupon/code/decorators/local
177
		 */
178
179
		/** admin/jqadm/coupon/code/decorators/global
180
		 * Adds a list of globally available decorators only to the coupon JQAdm client
181
		 *
182
		 * Decorators extend the functionality of a class by adding new aspects
183
		 * (e.g. log what is currently done), executing the methods of the underlying
184
		 * class only in certain conditions (e.g. only for logged in users) or
185
		 * modify what is returned to the caller.
186
		 *
187
		 * This option allows you to wrap global decorators
188
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
189
		 *
190
		 *  admin/jqadm/coupon/code/decorators/global = array( 'decorator1' )
191
		 *
192
		 * This would add the decorator named "decorator1" defined by
193
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
194
		 *
195
		 * @param array List of decorator names
196
		 * @since 2017.07
197
		 * @category Developer
198
		 * @see admin/jqadm/common/decorators/default
199
		 * @see admin/jqadm/coupon/code/decorators/excludes
200
		 * @see admin/jqadm/coupon/code/decorators/local
201
		 */
202
203
		/** admin/jqadm/coupon/code/decorators/local
204
		 * Adds a list of local decorators only to the coupon JQAdm client
205
		 *
206
		 * Decorators extend the functionality of a class by adding new aspects
207
		 * (e.g. log what is currently done), executing the methods of the underlying
208
		 * class only in certain conditions (e.g. only for logged in users) or
209
		 * modify what is returned to the caller.
210
		 *
211
		 * This option allows you to wrap local decorators
212
		 * ("\Aimeos\Admin\JQAdm\Coupon\Decorator\*") around the JQAdm client.
213
		 *
214
		 *  admin/jqadm/coupon/code/decorators/local = array( 'decorator2' )
215
		 *
216
		 * This would add the decorator named "decorator2" defined by
217
		 * "\Aimeos\Admin\JQAdm\Coupon\Decorator\Decorator2" only to the JQAdm client.
218
		 *
219
		 * @param array List of decorator names
220
		 * @since 2017.07
221
		 * @category Developer
222
		 * @see admin/jqadm/common/decorators/default
223
		 * @see admin/jqadm/coupon/code/decorators/excludes
224
		 * @see admin/jqadm/coupon/code/decorators/global
225
		 */
226
		return $this->createSubClient( 'coupon/code/' . $type, $name );
227
	}
228
229
230
	/**
231
	 * Checks if an error during upload occured
232
	 *
233
	 * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
234
	 * @throws \Aimeos\Admin\JQAdm\Exception If an error occured during upload
235
	 */
236
	protected function checkFileUpload( \Psr\Http\Message\UploadedFileInterface $file )
237
	{
238
		if( $file->getError() !== UPLOAD_ERR_OK )
239
		{
240
			switch( $file->getError() )
241
			{
242
				case UPLOAD_ERR_INI_SIZE:
243
				case UPLOAD_ERR_FORM_SIZE:
244
					throw new \Aimeos\Admin\JQAdm\Exception( 'The uploaded file exceeds the max. allowed filesize' );
245
				case UPLOAD_ERR_PARTIAL:
246
					throw new \Aimeos\Admin\JQAdm\Exception( 'The uploaded file was only partially uploaded' );
247
				case UPLOAD_ERR_NO_FILE:
248
					throw new \Aimeos\Admin\JQAdm\Exception( 'No file was uploaded' );
249
				case UPLOAD_ERR_NO_TMP_DIR:
250
					throw new \Aimeos\Admin\JQAdm\Exception( 'Temporary folder is missing' );
251
				case UPLOAD_ERR_CANT_WRITE:
252
					throw new \Aimeos\Admin\JQAdm\Exception( 'Failed to write file to disk' );
253
				case UPLOAD_ERR_EXTENSION:
254
					throw new \Aimeos\Admin\JQAdm\Exception( 'File upload stopped by extension' );
255
				default:
256
					throw new \Aimeos\Admin\JQAdm\Exception( 'Unknown upload error' );
257
			}
258
		}
259
	}
260
261
262
	/**
263
	 * Returns the coupon code items associated by the coupon item
264
	 *
265
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
266
	 * @param array $params Associative list of GET/POST parameters
267
	 * @param integer $total Value/result parameter that will contain the item total afterwards
268
	 * @return \Aimeos\MShop\Coupon\Item\Code\Iface[] Coupon code items associated to the coupon item
269
	 */
270
	protected function getCodeItems( \Aimeos\MShop\Coupon\Item\Iface $item, array $params = [], &$total )
271
	{
272
		$manager = \Aimeos\MShop::create( $this->getContext(), 'coupon/code' );
273
274
		$search = $manager->createSearch();
275
		$search->setSortations( [$search->sort( '+', 'coupon.code.code' )] );
276
277
		$search = $this->initCriteria( $search, $params );
278
		$expr = [
279
			$search->compare( '==', 'coupon.code.parentid', $item->getId() ),
280
			$search->getConditions(),
281
		];
282
		$search->setConditions( $search->combine( '&&', $expr ) );
283
284
		return $manager->searchItems( $search, [], $total );
285
	}
286
287
288
	/**
289
	 * Returns the list of sub-client names configured for the client.
290
	 *
291
	 * @return array List of JQAdm client names
292
	 */
293
	protected function getSubClientNames()
294
	{
295
		/** admin/jqadm/coupon/code/standard/subparts
296
		 * List of JQAdm sub-clients rendered within the coupon code section
297
		 *
298
		 * The output of the frontend is composed of the code generated by the JQAdm
299
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
300
		 * that are responsible for rendering certain sub-parts of the output. The
301
		 * sub-clients can contain JQAdm clients themselves and therefore a
302
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
303
		 * the output that is placed inside the container of its parent.
304
		 *
305
		 * At first, always the JQAdm code generated by the parent is printed, then
306
		 * the JQAdm code of its sub-clients. The code of the JQAdm sub-clients
307
		 * determines the code of the output of these sub-clients inside the parent
308
		 * container. If the configured list of clients is
309
		 *
310
		 *  array( "subclient1", "subclient2" )
311
		 *
312
		 * you can easily change the code of the output by recodeing the subparts:
313
		 *
314
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
315
		 *
316
		 * You can also remove one or more parts if they shouldn't be rendered:
317
		 *
318
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
319
		 *
320
		 * As the clients only generates structural JQAdm, the layout defined via CSS
321
		 * should support adding, removing or recodeing content by a fluid like
322
		 * design.
323
		 *
324
		 * @param array List of sub-client names
325
		 * @since 2017.07
326
		 * @category Developer
327
		 */
328
		return $this->getContext()->getConfig()->get( 'admin/jqadm/coupon/code/standard/subparts', [] );
329
	}
330
331
332
	/**
333
	 * Creates new and updates existing items using the data array
334
	 *
335
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
336
	 * @param string[] $data Data array
337
	 */
338
	protected function fromArray( \Aimeos\MShop\Coupon\Item\Iface $item, array $data )
339
	{
340
		$manager = \Aimeos\MShop::create( $this->getContext(), 'coupon/code' );
341
342
		foreach( $this->getValue( $data, 'coupon.code.id', [] ) as $idx => $id )
343
		{
344
			$citem = $manager->createItem();
345
346
			$citem->setId( $id );
347
			$citem->setParentId( $item->getId() );
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

347
			$citem->/** @scrutinizer ignore-call */ 
348
           setParentId( $item->getId() );

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...
348
			$citem->setCode( $this->getValue( $data, 'coupon.code.code/' . $idx ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getValue($data, '...pon.code.code/' . $idx) can also be of type string[]; however, parameter $code of Aimeos\MShop\Attribute\Item\Iface::setCode() 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

348
			$citem->setCode( /** @scrutinizer ignore-type */ $this->getValue( $data, 'coupon.code.code/' . $idx ) );
Loading history...
349
			$citem->setCount( $this->getValue( $data, 'coupon.code.count/' . $idx ) );
0 ignored issues
show
Bug introduced by
The method setCount() 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

349
			$citem->/** @scrutinizer ignore-call */ 
350
           setCount( $this->getValue( $data, 'coupon.code.count/' . $idx ) );

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...
350
			$citem->setDateStart( $this->getValue( $data, 'coupon.code.datestart/' . $idx ) );
0 ignored issues
show
Bug introduced by
The method setDateStart() 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

350
			$citem->/** @scrutinizer ignore-call */ 
351
           setDateStart( $this->getValue( $data, 'coupon.code.datestart/' . $idx ) );

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...
351
			$citem->setDateEnd( $this->getValue( $data, 'coupon.code.dateend/' . $idx ) );
0 ignored issues
show
Bug introduced by
The method setDateEnd() 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

351
			$citem->/** @scrutinizer ignore-call */ 
352
           setDateEnd( $this->getValue( $data, 'coupon.code.dateend/' . $idx ) );

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...
352
353
			$manager->saveItem( $citem, false );
354
		}
355
	}
356
357
358
	/**
359
	 * Stores the uploaded CSV file containing the coupon codes
360
	 *
361
	 * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object
362
	 * @param array $files File upload array including the PSR-7 file upload objects
363
	 */
364
	protected function storeFile( \Aimeos\MShop\Coupon\Item\Iface $item, array $files )
365
	{
366
		$file = $this->getValue( $files, 'code/file' );
367
368
		if( $file == null || $file->getError() === UPLOAD_ERR_NO_FILE ) {
369
			return;
370
		}
371
372
		$this->checkFileUpload( $file );
373
374
		$context = $this->getContext();
375
		$fs = $context->getFilesystemManager()->get( 'fs-import' );
376
		$dir = 'couponcode/' . $context->getLocale()->getSite()->getCode();
377
378
		if( $fs->isdir( $dir ) === false ) {
379
			$fs->mkdir( $dir );
380
		}
381
382
		$fs->writes( $dir . '/' . $item->getId() . '.csv', $file->getStream()->detach() );
383
	}
384
385
386
	/**
387
	 * Constructs the data array for the view from the given item
388
	 *
389
	 * @param \Aimeos\MShop\Coupon\Item\Code\Iface[] $items Coupon code items
390
	 * @return string[] Multi-dimensional associative list of item data
391
	 */
392
	protected function toArray( array $items )
393
	{
394
		$data = [];
395
396
		foreach( $items as $item )
397
		{
398
			foreach( $item->toArray( true ) as $key => $value ) {
399
				$data[$key][] = $value;
400
			}
401
		}
402
403
		return $data;
404
	}
405
406
407
	/**
408
	 * Returns the rendered template including the view data
409
	 *
410
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
411
	 * @return string HTML output
412
	 */
413
	protected function render( \Aimeos\MW\View\Iface $view )
414
	{
415
		/** admin/jqadm/coupon/code/template-item
416
		 * Relative path to the HTML body template of the code subpart for coupons.
417
		 *
418
		 * The template file contains the HTML code and processing instructions
419
		 * to generate the result shown in the body of the frontend. The
420
		 * configuration string is the path to the template file relative
421
		 * to the templates directory (usually in admin/jqadm/templates).
422
		 *
423
		 * You can overwrite the template file configuration in extensions and
424
		 * provide alternative templates. These alternative templates should be
425
		 * named like the default one but with the string "default" replaced by
426
		 * an unique name. You may use the name of your project for this. If
427
		 * you've implemented an alternative client class as well, "default"
428
		 * should be replaced by the name of the new class.
429
		 *
430
		 * @param string Relative path to the template creating the HTML code
431
		 * @since 2016.04
432
		 * @category Developer
433
		 */
434
		$tplconf = 'admin/jqadm/coupon/code/template-item';
435
		$default = 'coupon/item-code-standard';
436
437
		return $view->render( $view->config( $tplconf, $default ) );
438
	}
439
}
440