ListOptions::getDefaultLanguage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\i18n\Lists;
6
7
use Throwable;
8
use Nette\SmartObject;
9
use SixtyEightPublishers\i18n\ProfileProviderInterface;
10
11
/**
12
 * @property-read string $vendorDir
13
 * @property-read string $fallbackLanguage
14
 * @property-read string|NULL $defaultLanguage
15
 * @property-read string $resolvedLanguage
16
 */
17
final class ListOptions
18
{
19
	use SmartObject;
20
21
	/** @var \SixtyEightPublishers\i18n\ProfileProviderInterface  */
22
	private $profileProvider;
23
24
	/** @var string  */
25
	private $vendorDir;
26
27
	/** @var string  */
28
	private $fallbackLanguage;
29
30
	/** @var string|NULL  */
31
	private $defaultLanguage;
32
33
	/**
34
	 * @param \SixtyEightPublishers\i18n\ProfileProviderInterface $profileProvider
35
	 * @param string                                              $vendorDir
36
	 * @param string                                              $fallbackLanguage
37
	 * @param string|NULL                                         $defaultLanguage
38
	 */
39
	public function __construct(
40
		ProfileProviderInterface $profileProvider,
41
		string $vendorDir,
42
		string $fallbackLanguage,
43
		?string $defaultLanguage
44
	) {
45
		$this->profileProvider = $profileProvider;
46
		$this->vendorDir = $vendorDir;
0 ignored issues
show
Bug introduced by
The property vendorDir is declared read-only in SixtyEightPublishers\i18n\Lists\ListOptions.
Loading history...
47
		$this->fallbackLanguage = $fallbackLanguage;
0 ignored issues
show
Bug introduced by
The property fallbackLanguage is declared read-only in SixtyEightPublishers\i18n\Lists\ListOptions.
Loading history...
48
		$this->defaultLanguage = $defaultLanguage;
0 ignored issues
show
Bug introduced by
The property defaultLanguage is declared read-only in SixtyEightPublishers\i18n\Lists\ListOptions.
Loading history...
49
	}
50
51
	/**
52
	 * @return string
53
	 */
54
	public function getVendorDir(): string
55
	{
56
		return $this->vendorDir;
57
	}
58
59
	/**
60
	 * @return string
61
	 */
62
	public function getFallbackLanguage(): string
63
	{
64
		return $this->fallbackLanguage;
65
	}
66
67
	/**
68
	 * @return NULL|string
69
	 */
70
	public function getDefaultLanguage(): ?string
71
	{
72
		return $this->defaultLanguage;
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	public function getResolvedLanguage(): string
79
	{
80
		try {
81
			return $this->defaultLanguage ?? $this->profileProvider->getProfile()->language ?? $this->fallbackLanguage;
82
		} catch (Throwable $e) {
83
			trigger_error($e->getMessage(), E_USER_NOTICE);
84
85
			return $this->fallbackLanguage;
86
		}
87
	}
88
}
89