Passed
Push — master ( 57ed15...c8b0fe )
by Aimeos
24:08 queued 14:12
created

Standard::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package MShop
8
 * @subpackage Context
9
 */
10
11
12
namespace Aimeos\MShop\Context\Item;
13
14
15
/**
16
 * Common objects which must to be available for all manager objects.
17
 *
18
 * @package MShop
19
 * @subpackage Context
20
 */
21
class Standard implements \Aimeos\MShop\Context\Item\Iface
22
{
23
	private $cache;
24
	private $config;
25
	private $datetime;
26
	private $db;
27
	private $fs;
28
	private $locale;
29
	private $logger;
30
	private $mail;
31
	private $nonce;
32
	private $queue;
33
	private $password;
34
	private $process;
35
	private $session;
36
	private $view;
37
	private $user;
38
	private $groups;
39
	private $editor = '';
40
	private $i18n = [];
41
42
43
	/**
44
	 * Cleans up the stored resources
45
	 */
46
	public function __destruct()
47
	{
48
		$this->cache = null;
49
		$this->config = null;
50
		$this->db = null;
51
		$this->fs = null;
52
		$this->locale = null;
53
		$this->logger = null;
54
		$this->mail = null;
55
		$this->queue = null;
56
		$this->password = null;
57
		$this->process = null;
58
		$this->session = null;
59
		$this->view = null;
60
		$this->i18n = [];
61
	}
62
63
64
	/**
65
	 * Clones internal objects of the context item.
66
	 */
67
	public function __clone()
68
	{
69
		$this->cache = ( isset( $this->cache ) ? clone $this->cache : null );
70
		$this->config = ( isset( $this->config ) ? clone $this->config : null );
71
		$this->fs = ( isset( $this->fs ) ? clone $this->fs : null );
72
		$this->locale = ( isset( $this->locale ) ? clone $this->locale : null );
73
		$this->logger = ( isset( $this->logger ) ? clone $this->logger : null );
74
		$this->mail = ( isset( $this->mail ) ? clone $this->mail : null );
75
		$this->queue = ( isset( $this->queue ) ? clone $this->queue : null );
76
		$this->password = ( isset( $this->password ) ? clone $this->password : null );
77
		$this->process = ( isset( $this->process ) ? clone $this->process : null );
78
		$this->session = ( isset( $this->session ) ? clone $this->session : null );
79
		// view is always cloned
80
81
		foreach( $this->i18n as $locale => $object ) {
82
			$this->i18n[$locale] = clone $this->i18n[$locale];
83
		}
84
	}
85
86
87
	/**
88
	 * Cleans up internal objects of the context item
89
	 */
90
	public function __sleep() : array
91
	{
92
		$objects = array(
93
			$this->cache, $this->config, $this->db, $this->fs, $this->locale, $this->logger,
94
			$this->mail, $this->queue, $this->password, $this->process, $this->session, $this->view
95
		);
96
97
		foreach( $objects as $object )
98
		{
99
			if( is_object( $object ) && method_exists( $object, '__sleep' ) ) {
100
				$object->__sleep();
101
			}
102
		}
103
104
		return get_object_vars( $this );
105
	}
106
107
108
	/**
109
	 * Returns a hash identifying the context object.
110
	 *
111
	 * @return string Hash for identifying the context object
112
	 */
113
	public function __toString() : string
114
	{
115
		$objects = array(
116
			$this, $this->cache, $this->config, $this->db, $this->fs, $this->locale,
117
			$this->logger, $this->mail, $this->queue, $this->password, $this->process,
118
			$this->session, $this->view
119
		);
120
121
		return md5( $this->hash( $objects ) );
122
	}
123
124
125
	/**
126
	 * Sets the cache object.
127
	 *
128
	 * @param \Aimeos\MW\Cache\Iface $cache Cache object
129
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
130
	 */
131
	public function setCache( \Aimeos\MW\Cache\Iface $cache ) : \Aimeos\MShop\Context\Item\Iface
132
	{
133
		$this->cache = $cache;
134
135
		return $this;
136
	}
137
138
139
	/**
140
	 * Returns the cache object.
141
	 *
142
	 * @return \Aimeos\MW\Cache\Iface Cache object
143
	 */
144
	public function cache() : \Aimeos\MW\Cache\Iface
145
	{
146
		if( !isset( $this->cache ) ) {
147
			throw new \Aimeos\MShop\Exception( sprintf( 'Cache object not available' ) );
148
		}
149
150
		return $this->cache;
151
	}
152
153
154
	/**
155
	 * Sets the configuration object.
156
	 *
157
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
158
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
159
	 */
160
	public function setConfig( \Aimeos\MW\Config\Iface $config ) : \Aimeos\MShop\Context\Item\Iface
161
	{
162
		$this->config = $config;
163
164
		return $this;
165
	}
166
167
168
	/**
169
	 * Returns the configuration object.
170
	 *
171
	 * @return \Aimeos\MW\Config\Iface Configuration object
172
	 */
173
	public function config() : \Aimeos\MW\Config\Iface
174
	{
175
		if( !isset( $this->config ) ) {
176
			throw new \Aimeos\MShop\Exception( sprintf( 'Configuration object not available' ) );
177
		}
178
179
		return $this->config;
180
	}
181
182
183
	/**
184
	 * Sets the database connection manager object.
185
	 *
186
	 * @param \Aimeos\MW\DB\Manager\Iface $manager Database manager object
187
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
188
	 */
189
	public function setDatabaseManager( \Aimeos\MW\DB\Manager\Iface $manager ) : \Aimeos\MShop\Context\Item\Iface
190
	{
191
		$this->db = $manager;
192
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the database manager object.
199
	 *
200
	 * @return \Aimeos\MW\DB\Manager\Iface Database manager object
201
	 */
202
	public function db() : \Aimeos\MW\DB\Manager\Iface
203
	{
204
		if( !isset( $this->db ) ) {
205
			throw new \Aimeos\MShop\Exception( sprintf( 'Database manager object not available' ) );
206
		}
207
208
		return $this->db;
209
	}
210
211
212
	/**
213
	 * Sets the current date and time
214
	 *
215
	 * @param string $datetime Date and time as ISO string (YYYY-MM-DD HH:mm:ss)
216
	 */
217
	public function setDateTime( string $datetime ) : \Aimeos\MShop\Context\Item\Iface
218
	{
219
		$regex = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/';
220
221
		if( preg_match( $regex, (string) $datetime ) !== 1 ) {
222
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in date "%1$s". ISO format "YYYY-MM-DD hh:mm:ss" expected.', $datetime ) );
223
		}
224
225
		$this->datetime = $datetime;
226
227
		return $this;
228
	}
229
230
231
	/**
232
	 * Returns the current date and time
233
	 * This is especially useful to share the same request time or if applications
234
	 * allow to travel in time.
235
	 *
236
	 * @return string Current date and time as ISO string (YYYY-MM-DD HH:mm:ss)
237
	 */
238
	public function datetime() : string
239
	{
240
		if( $this->datetime === null ) {
241
			$this->datetime = date( 'Y-m-d H:i:00' );
242
		}
243
244
		return $this->datetime;
245
	}
246
247
248
	/**
249
	 * Sets the file system manager object.
250
	 *
251
	 * @param \Aimeos\MW\Filesystem\Manager\Iface $manager File system object
252
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
253
	 */
254
	public function setFilesystemManager( \Aimeos\MW\Filesystem\Manager\Iface $manager ) : \Aimeos\MShop\Context\Item\Iface
255
	{
256
		$this->fs = $manager;
257
		return $this;
258
	}
259
260
261
	/**
262
	 * Returns the file system object for the given resource name.
263
	 *
264
	 * @param string $resource Resource name, e.g. "fs-admin"
265
	 * @return \Aimeos\MW\Filesystem\Iface File system object
266
	 */
267
	public function fs( string $resource ) : \Aimeos\MW\Filesystem\Iface
268
	{
269
		if( !isset( $this->fs ) ) {
270
			throw new \Aimeos\MShop\Exception( sprintf( 'File system manager object not available' ) );
271
		}
272
273
		return $this->fs->get( $resource );
274
	}
275
276
277
	/**
278
	 * Sets the translation/internationalization objects.
279
	 *
280
	 * @param array $translations Associative list of internationalization objects implementing
281
	 * 	\Aimeos\MW\Translation\Iface with locale as key
282
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
283
	 */
284
	public function setI18n( array $translations ) : \Aimeos\MShop\Context\Item\Iface
285
	{
286
		$this->i18n = $translations;
287
288
		return $this;
289
	}
290
291
292
	/**
293
	 * Returns the translation/internationalization object for the given locale (null for default one).
294
	 *
295
	 * @param string|null $locale Two letter language ISO code for specific language instead of default one
296
	 * @return \Aimeos\MW\Translation\Iface Internationalization object
297
	 */
298
	public function i18n( string $locale = null ) : \Aimeos\MW\Translation\Iface
299
	{
300
		if( isset( $this->locale ) && $locale === null ) {
301
			$locale = $this->locale()->getLanguageId();
302
		}
303
304
		if( isset( $this->locale ) && $locale === null && reset( $this->i18n ) !== false ) {
305
			$locale = key( $this->i18n );
306
		}
307
308
		if( isset( $this->i18n[$locale] ) ) {
309
			return $this->i18n[$locale];
310
		}
311
312
		if( isset( $this->i18n['en'] ) ) {
313
			return $this->i18n['en'];
314
		}
315
316
		/// Locale ID %1$s
317
		throw new \Aimeos\MShop\Exception( sprintf( 'Internationalization object not available for "%1$s"', $locale ) );
318
	}
319
320
321
	/**
322
	 * Translates a string if possible
323
	 *
324
	 * @param string $name Name of the translation domain
325
	 * @param string $singular Singular string to translate
326
	 * @param string $plural Plural string to translate if count is not one
327
	 * @param int $number Number for plural translations
328
	 * @param string|null $locale Locale (e.g. en, en_US, de, etc.) or NULL for current locale
329
	 * @return string Translated string if possible
330
	 */
331
	public function translate( string $domain, string $singular, string $plural = null, int $number = 1, string $locale = null ) : string
332
	{
333
		if( empty( $this->i18n ) ) {
334
			return $number === 1 ? $singular : $plural;
335
		}
336
337
		if( $plural ) {
338
			return $this->i18n( $locale )->dn( $domain, $singular, $plural, $number );
339
		}
340
341
		return $this->i18n( $locale )->dt( $domain, $singular );
342
	}
343
344
345
	/**
346
	 * Sets the localization object.
347
	 *
348
	 * @param \Aimeos\MShop\Locale\Item\Iface $locale Localization object
349
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
350
	 */
351
	public function setLocale( \Aimeos\MShop\Locale\Item\Iface $locale ) : \Aimeos\MShop\Context\Item\Iface
352
	{
353
		$this->locale = $locale;
354
355
		return $this;
356
	}
357
358
359
	/**
360
	 * Returns the localization object.
361
	 *
362
	 * @return \Aimeos\MShop\Locale\Item\Iface Localization object
363
	 */
364
	public function locale() : \Aimeos\MShop\Locale\Item\Iface
365
	{
366
		if( !isset( $this->locale ) ) {
367
			throw new \Aimeos\MShop\Exception( sprintf( 'Locale object not available' ) );
368
		}
369
370
		return $this->locale;
371
	}
372
373
374
	/**
375
	 * Sets the logger object.
376
	 *
377
	 * @param \Aimeos\MW\Logger\Iface $logger Logger object
378
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
379
	 */
380
	public function setLogger( \Aimeos\MW\Logger\Iface $logger ) : \Aimeos\MShop\Context\Item\Iface
381
	{
382
		$this->logger = $logger;
383
384
		return $this;
385
	}
386
387
388
	/**
389
	 * Returns the logger object.
390
	 *
391
	 * @return \Aimeos\MW\Logger\Iface Logger object
392
	 */
393
	public function logger() : \Aimeos\MW\Logger\Iface
394
	{
395
		if( !isset( $this->logger ) ) {
396
			throw new \Aimeos\MShop\Exception( sprintf( 'Log manager object not available' ) );
397
		}
398
399
		return $this->logger;
400
	}
401
402
403
	/**
404
	 * Sets the mail object.
405
	 *
406
	 * @param \Aimeos\MW\Mail\Iface $mail Mail object
407
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
408
	 */
409
	public function setMail( \Aimeos\MW\Mail\Iface $mail ) : \Aimeos\MShop\Context\Item\Iface
410
	{
411
		$this->mail = $mail;
412
413
		return $this;
414
	}
415
416
417
	/**
418
	 * Returns the mail object.
419
	 *
420
	 * @return \Aimeos\MW\Mail\Iface Mail object
421
	 */
422
	public function mail() : \Aimeos\MW\Mail\Iface
423
	{
424
		if( !isset( $this->mail ) ) {
425
			throw new \Aimeos\MShop\Exception( sprintf( 'Mail object not available' ) );
426
		}
427
428
		return $this->mail;
429
	}
430
431
432
	/**
433
	 * Sets the message queue manager object.
434
	 *
435
	 * @param \Aimeos\MW\MQueue\Manager\Iface $mqManager Message queue manager object
436
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
437
	 */
438
	public function setMessageQueueManager( \Aimeos\MW\MQueue\Manager\Iface $mqManager ) : \Aimeos\MShop\Context\Item\Iface
439
	{
440
		$this->queue = $mqManager;
441
442
		return $this;
443
	}
444
445
446
	/**
447
	 * Returns the message queue object.
448
	 *
449
	 * @param string $resource Resource name, e.g. "mq-email"
450
	 * @param string $queue Message queue name, e.g. "order/email/payment"
451
	 * @return \Aimeos\MW\MQueue\Queue\Iface Message queue object
452
	 */
453
	public function queue( string $resource, string $queue ) : \Aimeos\MW\MQueue\Queue\Iface
454
	{
455
		if( !isset( $this->queue ) ) {
456
			throw new \Aimeos\MShop\Exception( sprintf( 'Message queue object not available' ) );
457
		}
458
459
		return $this->queue->get( $resource )->getQueue( $queue );
460
	}
461
462
463
	/**
464
	 * Returns the nonce value for inline Javascript
465
	 *
466
	 * @return string|null Nonce value
467
	 */
468
	public function nonce() : ?string
469
	{
470
		return $this->nonce;
471
	}
472
473
474
	/**
475
	 * Sets the nonce value for inline Javascript
476
	 *
477
	 * @param string $value Nonce value
478
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
479
	 */
480
	public function setNonce( ?string $value ) : \Aimeos\MShop\Context\Item\Iface
481
	{
482
		$this->nonce = $value;
483
		return $this;
484
	}
485
486
487
	/**
488
	 * Returns the password adapter object.
489
	 *
490
	 * @return \Aimeos\MW\Password\Iface Password adapter
491
	 */
492
	public function password() : \Aimeos\MW\Password\Iface
493
	{
494
		if( !isset( $this->password ) ) {
495
			throw new \Aimeos\MShop\Exception( sprintf( 'Password object not available' ) );
496
		}
497
498
		return $this->password;
499
	}
500
501
502
	/**
503
	 * Sets the password adapter object.
504
	 *
505
	 * @param \Aimeos\MW\Password\Iface $password Password adapter
506
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
507
	 */
508
	public function setPassword( \Aimeos\MW\Password\Iface $password ) : \Aimeos\MShop\Context\Item\Iface
509
	{
510
		$this->password = $password;
511
		return $this;
512
	}
513
514
515
	/**
516
	 * Sets the process object.
517
	 *
518
	 * @param \Aimeos\MW\Process\Iface $process Process object
519
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
520
	 */
521
	public function setProcess( \Aimeos\MW\Process\Iface $process ) : \Aimeos\MShop\Context\Item\Iface
522
	{
523
		$this->process = $process;
524
525
		return $this;
526
	}
527
528
529
	/**
530
	 * Returns the process object.
531
	 *
532
	 * @return \Aimeos\MW\Process\Iface Process object
533
	 */
534
	public function process() : \Aimeos\MW\Process\Iface
535
	{
536
		if( !isset( $this->process ) ) {
537
			throw new \Aimeos\MShop\Exception( sprintf( 'Process object not available' ) );
538
		}
539
540
		return $this->process;
541
	}
542
543
544
	/**
545
	 * Sets the session object.
546
	 *
547
	 * @param \Aimeos\MW\Session\Iface $session Session object
548
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
549
	 */
550
	public function setSession( \Aimeos\MW\Session\Iface $session ) : \Aimeos\MShop\Context\Item\Iface
551
	{
552
		$this->session = $session;
553
554
		return $this;
555
	}
556
557
558
	/**
559
	 * Returns the session object.
560
	 *
561
	 * @return \Aimeos\MW\Session\Iface Session object
562
	 */
563
	public function session() : \Aimeos\MW\Session\Iface
564
	{
565
		if( !isset( $this->session ) ) {
566
			throw new \Aimeos\MShop\Exception( sprintf( 'Session object not available' ) );
567
		}
568
569
		return $this->session;
570
	}
571
572
573
	/**
574
	 * Sets the view object.
575
	 *
576
	 * @param \Aimeos\MW\View\Iface $view View object
577
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
578
	 */
579
	public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\MShop\Context\Item\Iface
580
	{
581
		$this->view = $view;
582
583
		return $this;
584
	}
585
586
587
	/**
588
	 * Returns the view object.
589
	 *
590
	 * @return \Aimeos\MW\View\Iface View object
591
	 */
592
	public function view() : \Aimeos\MW\View\Iface
593
	{
594
		if( !isset( $this->view ) ) {
595
			throw new \Aimeos\MShop\Exception( sprintf( 'View object not available' ) );
596
		}
597
598
		return clone $this->view;
599
	}
600
601
602
	/**
603
	 * Sets the account name of the user/editor.
604
	 *
605
	 * @param string $name Account name of the user/editor
606
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
607
	 */
608
	public function setEditor( string $name ) : \Aimeos\MShop\Context\Item\Iface
609
	{
610
		$this->editor = $name;
611
612
		return $this;
613
	}
614
615
616
	/**
617
	 * Returns the account name of the user/editor.
618
	 *
619
	 * @return string Account name of the user/editor
620
	 */
621
	public function editor() : string
622
	{
623
		return $this->editor;
624
	}
625
626
627
	/**
628
	 * Sets the user ID of the logged in user.
629
	 *
630
	 * @param \Closure|string|null $user User ID of the logged in user or closure to retrieve them
631
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
632
	 */
633
	public function setUserId( $user ) : \Aimeos\MShop\Context\Item\Iface
634
	{
635
		$this->user = $user;
636
637
		return $this;
638
	}
639
640
641
	/**
642
	 * Returns the user ID of the logged in user.
643
	 *
644
	 * @return string|null User ID of the logged in user
645
	 */
646
	public function user() : ?string
647
	{
648
		if( $this->user instanceof \Closure )
649
		{
650
			$fcn = $this->user;
651
			$this->user = $fcn();
652
		}
653
654
		return $this->user;
655
	}
656
657
658
	/**
659
	 * Sets the group IDs of the logged in user.
660
	 *
661
	 * @param \Closure|array $groupIds Group IDs of the logged in user or closure to retrieve them
662
	 * @return \Aimeos\MShop\Context\Item\Iface Context item for chaining method calls
663
	 */
664
	public function setGroupIds( $groupIds ) : \Aimeos\MShop\Context\Item\Iface
665
	{
666
		$this->groups = $groupIds;
667
668
		return $this;
669
	}
670
671
672
	/**
673
	 * Returns the group IDs of the logged in user.
674
	 *
675
	 * @return array Group IDs of the logged in user
676
	 */
677
	public function groups() : array
678
	{
679
		if( $this->groups instanceof \Closure )
680
		{
681
			$fcn = $this->groups;
682
			$this->groups = $fcn();
683
		}
684
685
		return (array) $this->groups;
686
	}
687
688
689
	/**
690
	 * Returns a hash for the given objects
691
	 *
692
	 * @param array $list List of objects
693
	 * @return string Hash for the objects
694
	 */
695
	private function hash( array $list ) : string
696
	{
697
		$hash = '';
698
699
		foreach( $list as $item )
700
		{
701
			if( is_object( $item ) ) {
702
				$hash .= spl_object_hash( $item );
703
			}
704
		}
705
706
		return $hash;
707
	}
708
}
709