Completed
Push — layout-caching ( bff40d...dc5564 )
by Kamil
67:32 queued 46:25
created

CurrencyStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 74
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 10 3
A get() 0 4 1
A provideKey() 0 4 1
A isBaseCurrency() 0 4 1
A isAvailableCurrency() 0 11 1
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