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\Bundle\CoreBundle\Context; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
15
|
|
|
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
16
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
17
|
|
|
use Sylius\Component\Currency\Context\CurrencyContext as BaseCurrencyContext; |
18
|
|
|
use Sylius\Component\Storage\StorageInterface; |
19
|
|
|
use Sylius\Component\User\Context\CustomerContextInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
23
|
|
|
*/ |
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( |
56
|
|
|
StorageInterface $storage, |
57
|
|
|
CustomerContextInterface $customerContext, |
58
|
|
|
SettingsManagerInterface $settingsManager, |
59
|
|
|
ObjectManager $customerManager, |
60
|
|
|
ChannelContextInterface $channelContext |
61
|
|
|
) { |
62
|
|
|
$this->customerContext = $customerContext; |
63
|
|
|
$this->settingsManager = $settingsManager; |
64
|
|
|
$this->customerManager = $customerManager; |
65
|
|
|
$this->channelContext = $channelContext; |
66
|
|
|
|
67
|
|
|
parent::__construct($storage, $this->getDefaultCurrencyCode()); |
68
|
|
|
} |
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) |
115
|
|
|
{ |
116
|
|
|
return sprintf(self::STORAGE_KEY, $channelCode); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|