1 | <?php |
||
24 | class CurrencyContext extends BaseCurrencyContext |
||
25 | { |
||
26 | const STORAGE_KEY = '_sylius_currency_%s'; |
||
27 | |||
28 | /** |
||
29 | * @var CustomerContextInterface |
||
30 | */ |
||
31 | protected $customerContext; |
||
32 | |||
33 | /** |
||
34 | * @var SettingsManagerInterface |
||
35 | */ |
||
36 | protected $settingsManager; |
||
37 | |||
38 | /** |
||
39 | * @var ObjectManager |
||
40 | */ |
||
41 | protected $customerManager; |
||
42 | |||
43 | /** |
||
44 | * @var ChannelContextInterface |
||
45 | */ |
||
46 | protected $channelContext; |
||
47 | |||
48 | /** |
||
49 | * @param StorageInterface $storage |
||
50 | * @param CustomerContextInterface $customerContext |
||
51 | * @param SettingsManagerInterface $settingsManager |
||
52 | * @param ObjectManager $customerManager |
||
53 | * @param ChannelContextInterface $channelContext |
||
54 | */ |
||
55 | public function __construct( |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function getDefaultCurrencyCode() |
||
74 | { |
||
75 | return $this->settingsManager->load('sylius_general')->get('currency'); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function getCurrencyCode() |
||
82 | { |
||
83 | if ((null !== $customer = $this->customerContext->getCustomer()) && null !== $customer->getCurrencyCode()) { |
||
84 | return $customer->getCurrencyCode(); |
||
85 | } |
||
86 | |||
87 | $channel = $this->channelContext->getChannel(); |
||
88 | |||
89 | return $this->storage->getData($this->getStorageKey($channel->getCode()), $this->defaultCurrencyCode); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function setCurrencyCode($currencyCode) |
||
96 | { |
||
97 | if (null === $customer = $this->customerContext->getCustomer()) { |
||
98 | $channel = $this->channelContext->getChannel(); |
||
99 | |||
100 | return $this->storage->setData($this->getStorageKey($channel->getCode()), $currencyCode); |
||
101 | } |
||
102 | |||
103 | $customer->setCurrencyCode($currencyCode); |
||
104 | |||
105 | $this->customerManager->persist($customer); |
||
106 | $this->customerManager->flush(); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param string $channelCode |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | private function getStorageKey($channelCode) |
||
118 | } |
||
119 |