Passed
Push — master ( 5a84de...2d6aa3 )
by Aimeos
05:36
created

Context::editor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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