|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\Locale\Context; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
|
15
|
|
|
use Sylius\Component\Channel\Context\ChannelNotFoundException; |
|
16
|
|
|
use Sylius\Component\Core\Locale\LocaleStorageInterface; |
|
17
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
|
18
|
|
|
use Sylius\Component\Locale\Context\LocaleNotFoundException; |
|
19
|
|
|
use Sylius\Component\Locale\Provider\LocaleProviderInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Kamil Kokot <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class StorageBasedLocaleContext implements LocaleContextInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ChannelContextInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $channelContext; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var LocaleStorageInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $localeStorage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var LocaleProviderInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $localeProvider; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param ChannelContextInterface $channelContext |
|
43
|
|
|
* @param LocaleStorageInterface $localeStorage |
|
44
|
|
|
* @param LocaleProviderInterface $localeProvider |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( |
|
47
|
|
|
ChannelContextInterface $channelContext, |
|
48
|
|
|
LocaleStorageInterface $localeStorage, |
|
49
|
|
|
LocaleProviderInterface $localeProvider |
|
50
|
|
|
) { |
|
51
|
|
|
$this->channelContext = $channelContext; |
|
52
|
|
|
$this->localeStorage = $localeStorage; |
|
53
|
|
|
$this->localeProvider = $localeProvider; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getLocaleCode() |
|
60
|
|
|
{ |
|
61
|
|
|
$availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes(); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$localeCode = $this->localeStorage->get($this->channelContext->getChannel()); |
|
65
|
|
|
} catch (ChannelNotFoundException $exception) { |
|
66
|
|
|
throw new LocaleNotFoundException(null, $exception); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!in_array($localeCode, $availableLocalesCodes, true)) { |
|
70
|
|
|
throw LocaleNotFoundException::notAvailable($localeCode, $availableLocalesCodes); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $localeCode; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.