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\Currency; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
15
|
|
|
use Sylius\Component\Currency\Model\CurrencyInterface; |
16
|
|
|
use Sylius\Component\Resource\Storage\StorageInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kamil Kokot <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class CurrencyStorage implements CurrencyStorageInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var StorageInterface |
25
|
|
|
*/ |
26
|
|
|
private $storage; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param StorageInterface $storage |
30
|
|
|
*/ |
31
|
|
|
public function __construct(StorageInterface $storage) |
32
|
|
|
{ |
33
|
|
|
$this->storage = $storage; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function set(ChannelInterface $channel, $currencyCode) |
40
|
|
|
{ |
41
|
|
|
if ($this->isBaseCurrency($currencyCode, $channel) || !$this->isAvailableCurrency($currencyCode, $channel)) { |
42
|
|
|
$this->storage->remove($this->provideKey($channel)); |
43
|
|
|
|
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->storage->set($this->provideKey($channel), $currencyCode); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function get(ChannelInterface $channel) |
54
|
|
|
{ |
55
|
|
|
return $this->storage->get($this->provideKey($channel)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
private function provideKey(ChannelInterface $channel) |
62
|
|
|
{ |
63
|
|
|
return '_currency_' . $channel->getCode(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string$currencyCode |
68
|
|
|
* @param ChannelInterface $channel |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
private function isBaseCurrency($currencyCode, ChannelInterface $channel) |
73
|
|
|
{ |
74
|
|
|
return $currencyCode === $channel->getBaseCurrency()->getCode(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $currencyCode |
79
|
|
|
* @param ChannelInterface $channel |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
private function isAvailableCurrency($currencyCode, ChannelInterface $channel) |
84
|
|
|
{ |
85
|
|
|
$availableCurrencies = array_map( |
86
|
|
|
function (CurrencyInterface $currency) { |
87
|
|
|
return $currency->getCode(); |
88
|
|
|
}, |
89
|
|
|
$channel->getCurrencies()->toArray() |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return in_array($currencyCode, $availableCurrencies, true); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|