Passed
Push — master ( ea0b61...bdd0d7 )
by Aimeos
03:40
created

Base::import()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm;
12
13
sprintf( 'type' ); // for translation
14
15
16
/**
17
 * Common abstract class for all admin client classes.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
abstract class Base
23
	implements \Aimeos\Admin\JQAdm\Iface, \Aimeos\Macro\Iface
24
{
25
	use \Aimeos\Macro\Macroable;
26
27
28
	private \Aimeos\MShop\ContextIface $context;
29
	private ?\Aimeos\Admin\JQAdm\Iface $object = null;
30
	private ?\Aimeos\Base\View\Iface $view = null;
31
	private ?\Aimeos\Bootstrap $aimeos = null;
32
	private ?array $subclients = null;
33
34
35
	/**
36
	 * Initializes the class instance.
37
	 *
38
	 * @param \Aimeos\MShop\ContextIface $context Context object
39
	 */
40
	public function __construct( \Aimeos\MShop\ContextIface $context )
41
	{
42
		$this->context = $context;
43
	}
44
45
46
	/**
47
	 * Adds the required data used in the attribute template
48
	 *
49
	 * @param \Aimeos\Base\View\Iface $view View object
50
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
51
	 */
52
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
53
	{
54
		return $view;
55
	}
56
57
58
	/**
59
	 * Returns the Aimeos bootstrap object
60
	 *
61
	 * @return \Aimeos\Bootstrap The Aimeos bootstrap object
62
	 */
63
	public function getAimeos() : \Aimeos\Bootstrap
64
	{
65
		if( !isset( $this->aimeos ) ) {
66
			throw new \Aimeos\Admin\JQAdm\Exception( $this->context->translate( 'admin', 'Aimeos object not available' ) );
67
		}
68
69
		return $this->aimeos;
70
	}
71
72
73
	/**
74
	 * Sets the Aimeos bootstrap object
75
	 *
76
	 * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object
77
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
78
	 */
79
	public function setAimeos( \Aimeos\Bootstrap $aimeos ) : \Aimeos\Admin\JQAdm\Iface
80
	{
81
		$this->aimeos = $aimeos;
82
		return $this;
83
	}
84
85
86
	/**
87
	 * Makes the outer decorator object available to inner objects
88
	 *
89
	 * @param \Aimeos\Admin\JQAdm\Iface $object Outmost object
90
	 * @return \Aimeos\Admin\JQAdm\Iface Same object for fluent interface
91
	 */
92
	public function setObject( \Aimeos\Admin\JQAdm\Iface $object ) : \Aimeos\Admin\JQAdm\Iface
93
	{
94
		$this->object = $object;
95
		return $this;
96
	}
97
98
99
	/**
100
	 * Returns the view object that will generate the admin output.
101
	 *
102
	 * @return \Aimeos\Base\View\Iface The view object which generates the admin output
103
	 */
104
	protected function view() : \Aimeos\Base\View\Iface
105
	{
106
		if( !isset( $this->view ) ) {
107
			throw new \Aimeos\Admin\JQAdm\Exception( $this->context->translate( 'admin', 'No view available' ) );
108
		}
109
110
		return $this->view;
111
	}
112
113
114
	/**
115
	 * Sets the view object that will generate the admin output.
116
	 *
117
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the admin output
118
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
119
	 */
120
	public function setView( \Aimeos\Base\View\Iface $view ) : \Aimeos\Admin\JQAdm\Iface
121
	{
122
		$this->view = $view;
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Batch update of a resource
129
	 *
130
	 * @return string|null Output to display
131
	 */
132
	public function batch() : ?string
133
	{
134
		foreach( $this->getSubClients() as $client ) {
135
			$client->batch();
136
		}
137
138
		return null;
139
	}
140
141
142
	/**
143
	 * Copies a resource
144
	 *
145
	 * @return string|null Output to display
146
	 */
147
	public function copy() : ?string
148
	{
149
		$body = null;
150
		$view = $this->view();
151
152
		foreach( $this->getSubClients() as $idx => $client )
153
		{
154
			$view->tabindex = ++$idx + 1;
155
			$body .= $client->copy();
156
		}
157
158
		return $body;
159
	}
160
161
162
	/**
163
	 * Creates a new resource
164
	 *
165
	 * @return string|null Output to display
166
	 */
167
	public function create() : ?string
168
	{
169
		$body = null;
170
		$view = $this->view();
171
172
		foreach( $this->getSubClients() as $idx => $client )
173
		{
174
			$view->tabindex = ++$idx + 1;
175
			$body .= $client->create();
176
		}
177
178
		return $body;
179
	}
180
181
182
	/**
183
	 * Deletes a resource
184
	 *
185
	 * @return string|null Output to display
186
	 */
187
	public function delete() : ?string
188
	{
189
		$body = null;
190
191
		foreach( $this->getSubClients() as $client ) {
192
			$body .= $client->delete();
193
		}
194
195
		return $body;
196
	}
197
198
199
	/**
200
	 * Exports a resource
201
	 *
202
	 * @return string|null Output to display
203
	 */
204
	public function export() : ?string
205
	{
206
		$body = null;
207
208
		foreach( $this->getSubClients() as $client ) {
209
			$body .= $client->export();
210
		}
211
212
		return $body;
213
	}
214
215
216
	/**
217
	 * Returns a resource
218
	 *
219
	 * @return string|null Output to display
220
	 */
221
	public function get() : ?string
222
	{
223
		$body = null;
224
		$view = $this->view();
225
226
		foreach( $this->getSubClients() as $idx => $client )
227
		{
228
			$view->tabindex = ++$idx + 1;
229
			$body .= $client->get();
230
		}
231
232
		return $body;
233
	}
234
235
236
	/**
237
	 * Saves the data
238
	 *
239
	 * @return string|null Output to display
240
	 */
241
	public function save() : ?string
242
	{
243
		$body = null;
244
245
		foreach( $this->getSubClients() as $client ) {
246
			$body .= $client->save();
247
		}
248
249
		return $body;
250
	}
251
252
253
	/**
254
	 * Returns a list of resource according to the conditions
255
	 *
256
	 * @return string|null Output to display
257
	 */
258
	public function search() : ?string
259
	{
260
		$body = null;
261
262
		foreach( $this->getSubClients() as $client ) {
263
			$body .= $client->search();
264
		}
265
266
		return $body;
267
	}
268
269
270
	/**
271
	 * Returns the PSR-7 response object for the request
272
	 *
273
	 * @return \Psr\Http\Message\ResponseInterface Response object
274
	 */
275
	public function response() : \Psr\Http\Message\ResponseInterface
276
	{
277
		return $this->view()->response();
278
	}
279
280
281
	/**
282
	 * Adds the decorators to the client object
283
	 *
284
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
285
	 * @param array $decorators List of decorator name that should be wrapped around the client
286
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\"
287
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
288
	 * @throws \LogicException If class can't be instantiated
289
	 */
290
	protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Admin\JQAdm\Iface
291
	{
292
		$interface = \Aimeos\Admin\JQAdm\Common\Decorator\Iface::class;
293
294
		foreach( $decorators as $name )
295
		{
296
			$classname = $classprefix . $name;
297
298
			if( ctype_alnum( $name ) === false )
299
			{
300
				$msg = $this->context->translate( 'admin', 'Invalid class name "%1$s"' );
301
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $classname ) );
302
			}
303
304
			$client = \Aimeos\Utils::create( $classname, [$client, $this->context], $interface );
305
		}
306
307
		return $client;
308
	}
309
310
311
	/**
312
	 * Adds the decorators to the client object
313
	 *
314
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
315
	 * @param string $path Admin string in lower case, e.g. "catalog/detail/basic"
316
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
317
	 */
318
	protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, string $path ) : \Aimeos\Admin\JQAdm\Iface
319
	{
320
		if( !is_string( $path ) || $path === '' )
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
321
		{
322
			$msg = $this->context->translate( 'admin', 'Invalid domain "%1$s"' );
323
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $path ) );
324
		}
325
326
		$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) );
327
		$config = $this->context->config();
328
329
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
330
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] );
331
		$client = $this->addDecorators( $client, $decorators, $classprefix );
332
333
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\';
334
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] );
335
		$client = $this->addDecorators( $client, $decorators, $classprefix );
336
337
		return $client;
338
	}
339
340
341
	/**
342
	 * Modifiy several items at once
343
	 *
344
	 * @param string $domain Data domain of the items
345
	 * @param string|null $resource Resource name or null for domain name
346
	 * @return string|null Output to display
347
	 */
348
	protected function batchBase( string $domain, string $resource = null ) : ?string
349
	{
350
		$view = $this->view();
351
352
		if( !empty( $ids = $view->param( 'id' ) ) )
353
		{
354
			$manager = \Aimeos\MShop::create( $this->context(), $domain );
355
			$filter = $manager->filter()->add( [str_replace( '/', '.', $domain ) . '.id' => $ids] )->slice( 0, count( $ids ) );
356
			$items = $manager->search( $filter, $this->getDomains() );
357
358
			$data = $view->param( 'item', [] );
359
360
			foreach( $items as $item ) {
361
				$temp = $data; $item->fromArray( $temp, true );
362
			}
363
364
			$view->items = $items;
365
366
			foreach( $this->getSubClients() as $client ) {
367
				$client->batch();
368
			}
369
370
			$manager->save( $items );
371
		}
372
373
		return $this->redirect( $resource ?: $domain, 'search', null, 'save' );
374
	}
375
376
377
	/**
378
	 * Returns the sub-client given by its name.
379
	 *
380
	 * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree)
381
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
382
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-part object
383
	 * @throws \LogicException If class can't be instantiated
384
	 */
385
	protected function createSubClient( string $path, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
386
	{
387
		$path = strtolower( $path );
388
		$name = $name ?: $this->context->config()->get( 'admin/jqadm/' . $path . '/name', 'Standard' );
389
390
		if( empty( $name ) || ctype_alnum( $name ) === false ) {
391
			throw new \LogicException( sprintf( 'Invalid characters in client name "%1$s"', $name ), 400 );
392
		}
393
394
		$subnames = str_replace( '/', '\\', ucwords( $path, '/' ) );
395
		$classname = '\\Aimeos\\Admin\\JQAdm\\' . $subnames . '\\' . $name;
396
		$interface = \Aimeos\Admin\JQAdm\Iface::class;
397
398
		$object = \Aimeos\Utils::create( $classname, [$this->context], $interface );
399
		$object = $this->addClientDecorators( $object, $path );
400
401
		return $object->setObject( $object )->setAimeos( $this->aimeos )->setView( $this->view );
0 ignored issues
show
Bug introduced by
The method setObject() does not exist on Aimeos\Admin\JQAdm\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface or Aimeos\Admin\JQAdm\Common\Decorator\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

401
		return $object->/** @scrutinizer ignore-call */ setObject( $object )->setAimeos( $this->aimeos )->setView( $this->view );
Loading history...
402
	}
403
404
405
	/**
406
	 * Returns the value for the given key in the array
407
	 *
408
	 * @param array $values Multi-dimensional associative list of key/value pairs
409
	 * @param string $key Parameter key like "name" or "list/test" for associative arrays
410
	 * @param mixed $default Returned value if no one for key is available
411
	 * @return mixed Value from the array or default value if not present in array
412
	 */
413
	protected function val( array $values, $key, $default = null )
414
	{
415
		foreach( explode( '/', trim( $key, '/' ) ) as $part )
416
		{
417
			if( is_array( $values ) && isset( $values[$part] ) ) {
418
				$values = $values[$part];
419
			} else {
420
				return $default;
421
			}
422
		}
423
424
		return $values;
425
	}
426
427
428
	/**
429
	 * Returns the known client parameters and their values
430
	 *
431
	 * @param array $names List of parameter names
432
	 * @return array Associative list of parameters names as key and their values
433
	 */
434
	protected function getClientParams( $names = ['id', 'resource', 'site', 'locale'] ) : array
435
	{
436
		$list = [];
437
438
		foreach( $names as $name )
439
		{
440
			if( ( $val = $this->view->param( $name ) ) !== null && !is_array( $val ) ) {
0 ignored issues
show
Bug introduced by
The method param() does not exist on null. ( Ignorable by Annotation )

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

440
			if( ( $val = $this->view->/** @scrutinizer ignore-call */ param( $name ) ) !== null && !is_array( $val ) ) {

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...
441
				$list[$name] = $val;
442
			}
443
		}
444
445
		return $list;
446
	}
447
448
449
	/**
450
	 * Returns the domain names whose items should be fetched too
451
	 *
452
	 * @return string[] List of domain names
453
	 */
454
	protected function getDomains() : array
455
	{
456
		return [];
457
	}
458
459
460
	/**
461
	 * Returns the context object.
462
	 *
463
	 * @return \Aimeos\MShop\ContextIface Context object
464
	 */
465
	protected function context() : \Aimeos\MShop\ContextIface
466
	{
467
		return $this->context;
468
	}
469
470
471
	/**
472
	 * Returns the available class names without namespace that are stored in the given path
473
	 *
474
	 * @param string $relpath Path relative to the include paths
475
	 * @param string[] $excludes List of file names to execlude
476
	 * @return string[] List of available class names
477
	 */
478
	protected function getClassNames( string $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] ) : array
479
	{
480
		$list = [];
481
482
		foreach( $this->getAimeos()->getIncludePaths() as $path )
483
		{
484
			$path .= DIRECTORY_SEPARATOR . $relpath;
485
486
			if( is_dir( $path ) )
487
			{
488
				foreach( new \DirectoryIterator( $path ) as $entry )
489
				{
490
					if( $entry->isFile() && !in_array( $entry->getFileName(), $excludes ) ) {
491
						$list[] = pathinfo( $entry->getFileName(), PATHINFO_FILENAME );
492
					}
493
				}
494
			}
495
		}
496
497
		sort( $list );
498
		return $list;
499
	}
500
501
502
	/**
503
	 * Returns the array of criteria conditions based on the given parameters
504
	 *
505
	 * @param array $params List of criteria data with condition, sorting and paging
506
	 * @return array Multi-dimensional associative list of criteria conditions
507
	 */
508
	protected function getCriteriaConditions( array $params ) : array
509
	{
510
		$expr = [];
511
512
		if( isset( $params['key'] ) )
513
		{
514
			foreach( (array) $params['key'] as $idx => $key )
515
			{
516
				if( $key != '' && isset( $params['op'][$idx] ) && $params['op'][$idx] != ''
517
					&& isset( $params['val'][$idx] ) && $params['val'][$idx] != ''
518
				) {
519
					$expr[] = [$params['op'][$idx] => [$key => $params['val'][$idx]]];
520
				}
521
			}
522
523
			if( !empty( $expr ) ) {
524
				$expr = ['&&' => $expr];
525
			}
526
		}
527
528
		return $expr;
529
	}
530
531
532
	/**
533
	 * Returns the outer decoratorator of the object
534
	 *
535
	 * @return \Aimeos\Admin\JQAdm\Iface Outmost object
536
	 */
537
	protected function object() : Iface
538
	{
539
		if( isset( $this->object ) ) {
540
			return $this->object;
541
		}
542
543
		return $this;
544
	}
545
546
547
	/**
548
	 * Returns the sub-client given by its name.
549
	 *
550
	 * @param string $type Name of the client type
551
	 * @param string|null $name Name of the sub-client (Default if null)
552
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
553
	 */
554
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
555
	{
556
		$msg = $this->context()->translate( 'admin', 'Not implemented' );
557
		throw new \Aimeos\Admin\JQAdm\Exception( $msg );
558
	}
559
560
561
	/**
562
	 * Returns the configured sub-clients or the ones named in the default parameter if none are configured.
563
	 *
564
	 * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names
565
	 */
566
	protected function getSubClients() : array
567
	{
568
		if( !isset( $this->subclients ) )
569
		{
570
			$this->subclients = [];
571
572
			foreach( $this->getSubClientNames() as $name ) {
573
				$this->subclients[] = $this->getSubClient( $name );
574
			}
575
		}
576
577
		return $this->subclients;
578
	}
579
580
581
	/**
582
	 * Returns the list of sub-client names configured for the client.
583
	 *
584
	 * @return array List of admin client names
585
	 */
586
	protected function getSubClientNames() : array
587
	{
588
		return [];
589
	}
590
591
592
	/**
593
	 * Initializes the criteria object based on the given parameter
594
	 *
595
	 * @param \Aimeos\Base\Criteria\Iface $criteria Criteria object
596
	 * @param array $params List of criteria data with condition, sorting and paging
597
	 * @return \Aimeos\Base\Criteria\Iface Initialized criteria object
598
	 */
599
	protected function initCriteria( \Aimeos\Base\Criteria\Iface $criteria, array $params ) : \Aimeos\Base\Criteria\Iface
600
	{
601
		if( isset( $params['sort'] ) && !empty( $params['sort'] ) ) {
602
			$criteria->order( $params['sort'] );
603
		}
604
605
		return $criteria->slice( $params['page']['offset'] ?? 0, $params['page']['limit'] ?? 25 )
606
			->add( $criteria->parse( $this->getCriteriaConditions( $params['filter'] ?? [] ) ) );
607
	}
608
609
610
	/**
611
	 * Flattens the nested configuration array
612
	 *
613
	 * @param array $config Multi-dimensional list of key/value pairs
614
	 * @param string $path Path of keys separated by slashes (/) to add new values for
615
	 * @return array List of arrays with "key" and "val" keys
616
	 */
617
	protected function flatten( array $config, string $path = '' ) : array
618
	{
619
		$list = [];
620
621
		foreach( $config as $key => $val )
622
		{
623
			if( is_array( $val ) ) {
624
				$list = array_merge( $list, $this->flatten( $val, $path . '/' . $key ) );
625
			} else {
626
				$list[] = ['key' => trim( $path . '/' . $key, '/' ), 'val' => $val];
627
			}
628
		}
629
630
		return $list;
631
	}
632
633
634
	/**
635
	 * Writes the exception details to the log
636
	 *
637
	 * @param \Exception $e Exception object
638
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
639
	 */
640
	protected function log( \Exception $e ) : Iface
641
	{
642
		$msg = $e->getMessage() . PHP_EOL;
643
644
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception ) {
645
			$msg .= print_r( $e->getDetails(), true ) . PHP_EOL;
0 ignored issues
show
Bug introduced by
Are you sure print_r($e->getDetails(), true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

645
			$msg .= /** @scrutinizer ignore-type */ print_r( $e->getDetails(), true ) . PHP_EOL;
Loading history...
646
		}
647
648
		$this->context->logger()->error( $msg . $e->getTraceAsString(), 'admin/jqadm' );
649
650
		return $this;
651
	}
652
653
654
	/**
655
	 * Adds a redirect to the response for the next action
656
	 *
657
	 * @param string $resource Resource name
658
	 * @param string|null $action Next action
659
	 * @param string|null $id ID of the next resource item
660
	 * @param string|null $method Current method name
661
	 * @param array $params URL parameters to use
662
	 * @return string|null Returns value for the actions
663
	 */
664
	protected function redirect( string $resource, ?string $action, string $id = null,
665
		string $method = null, array $params = [] ) : ?string
666
	{
667
		$params += $this->getClientParams();
668
		$context = $this->context();
669
		$view = $this->view();
670
671
		$params['resource'] = $resource;
672
		unset( $params['id'] );
673
674
		switch( $action )
675
		{
676
			case 'search':
677
				$url = $view->link( 'admin/jqadm/url/search', $params ); break;
678
			case 'create':
679
				$url = $view->link( 'admin/jqadm/url/create', ['parentid' => $id] + $params ); break;
680
			case 'copy':
681
				$url = $view->link( 'admin/jqadm/url/copy', ['id' => $id] + $params ); break;
682
			default:
683
				$url = $view->link( 'admin/jqadm/url/get', ['id' => $id] + $params );
684
		}
685
686
		switch( $method )
687
		{
688
			case 'save':
689
				$context->session()->set( 'info', [$context->translate( 'admin', 'Item saved successfully' )] ); break;
690
			case 'delete':
691
				$context->session()->set( 'info', [$context->translate( 'admin', 'Item deleted successfully' )] ); break;
692
		}
693
694
		$view->response()->withStatus( 302 );
695
		$view->response()->withHeader( 'Location', $url );
696
		$view->response()->withHeader( 'Cache-Control', 'no-store' );
697
698
		return null;
699
	}
700
701
702
	/**
703
	 * Writes the exception details to the log
704
	 *
705
	 * @param \Exception $e Exception object
706
	 * @param string $method Method it's called from
707
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
708
	 */
709
	protected function report( \Exception $e, string $method ) : Iface
710
	{
711
		$view = $this->view;
712
		$i18n = $this->context->i18n();
713
714
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception )
715
		{
716
			$view->errors = array_merge( $view->get( 'errors', [] ), [$e->getMessage()] );
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

716
			$view->errors = array_merge( $view->/** @scrutinizer ignore-call */ get( 'errors', [] ), [$e->getMessage()] );

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...
717
			return $this->log( $e );
718
		}
719
		elseif( $e instanceof \Aimeos\MShop\Exception )
720
		{
721
			$view->errors = array_merge( $view->get( 'errors', [] ), [$i18n->dt( 'mshop', $e->getMessage() )] );
722
			return $this->log( $e );
723
		}
724
725
		switch( $method )
726
		{
727
			case 'save': $msg = $i18n->dt( 'admin', 'Error saving data' ); break;
728
			case 'delete': $msg = $i18n->dt( 'admin', 'Error deleting data' ); break;
729
			default: $msg = $i18n->dt( 'admin', 'Error retrieving data' ); break;
730
		}
731
732
		$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
733
734
		return $this->log( $e );
735
	}
736
737
738
	/**
739
	 * Checks and returns the request parameter for the given name
740
	 *
741
	 * @param string $name Name of the request parameter, can be a path like 'page/limit'
742
	 * @return mixed Parameter value
743
	 * @throws \Aimeos\Admin\JQAdm\Exception If the parameter is missing
744
	 */
745
	protected function require( string $name )
746
	{
747
		if( ( $value = $this->view()->param( $name ) ) !== null ) {
748
			return $value;
749
		}
750
751
		$msg = $this->context->translate( 'admin', 'Required parameter "%1$s" is missing' );
752
		throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, $name ) );
753
	}
754
755
756
	/**
757
	 * Stores and returns the parameters used for searching items
758
	 *
759
	 * @param array $params GET/POST parameter set
760
	 * @param string $name Name of the panel/subpanel
761
	 * @return array Associative list of parameters for searching items
762
	 */
763
	protected function storeFilter( array $params, string $name ) : array
764
	{
765
		$key = 'aimeos/admin/jqadm/' . $name;
766
		$session = $this->context()->session();
767
768
		foreach( ['fields', 'filter', 'page', 'sort'] as $part )
769
		{
770
			if( isset( $params[$part] ) ) {
771
				$session->set( $key . '/' . $part, $params[$part] );
772
			}
773
		}
774
775
		return [
776
			'fields' => $session->get( $key . '/fields' ),
777
			'filter' => $session->get( $key . '/filter' ),
778
			'page' => $session->get( $key . '/page' ),
779
			'sort' => $session->get( $key . '/sort' ),
780
		];
781
	}
782
783
784
	/**
785
	 * Throws an exception with given details
786
	 *
787
	 * @param array $errors List of key/message pairs of errors
788
	 * @throws \Aimeos\Admin\JQAdm\Exception Exception with error details
789
	 */
790
	protected function notify( array $errors ) : Iface
791
	{
792
		$list = [];
793
		$i18n = $this->context->i18n();
794
795
		foreach( $errors as $key => $error )
796
		{
797
			if( $error ) {
798
				$list[] = $key . ': ' . $i18n->dt( 'mshop', $error );
799
			}
800
		}
801
802
		if( !empty( $list ) ) {
803
			throw new \Aimeos\Admin\JQAdm\Exception( join( "\n", $list ) );
804
		}
805
806
		return $this;
807
	}
808
}
809